'Finish writing JSON files, upload to cloudstorage and bigquery.'

def Flush(self):

    self.bigquery = bigquery.GetBigQueryClient()
    urn_str = self.state.source_urn.RelativeName('aff4:/').replace('/', '_').replace(':', '').replace('.', '-')
    for tracker in self.temp_output_trackers.values():
        tracker.gzip_filehandle.write('\n')
        tracker.gzip_filehandle.close()
        tracker.gzip_filehandle_parent.seek(0)
        job_id = '{0}_{1}_{2}'.format(urn_str, tracker.output_type, rdfvalue.RDFDatetime.Now().AsSecondsFromEpoch())
        if (tracker.output_type in self.state.output_jobids):
            job_id = self.state.output_jobids[tracker.output_type]
        else:
            self.state.output_jobids[tracker.output_type] = job_id
        if (self.state.failure_count >= config.CONFIG['BigQuery.max_upload_failures']):
            logging.error('Exceeded    BigQuery.max_upload_failures    for    %s.    Giving    up    on    BigQuery    and    writing    to    AFF4.', self.state.source_urn)
            self._WriteToAFF4(job_id, tracker.schema, tracker.gzip_filehandle_parent, self.token)
        else:
            try:
                self.bigquery.InsertData(tracker.output_type, tracker.gzip_filehandle_parent, tracker.schema, job_id)
                self.state.failure_count = max(0, (self.state.failure_count - 1))
                del self.state.output_jobids[tracker.output_type]
            except bigquery.BigQueryJobUploadError:
                self.state.failure_count += 1
                self._WriteToAFF4(job_id, tracker.schema, tracker.gzip_filehandle_parent, self.token)
    self.temp_output_trackers = {}
