def check_and_get_async_jobs(self, async_jobs): self.async_requests = [] for job in async_jobs: ar = AdReportRun(job['id']) report = self.get_report(ar) percent = report['async_percent_completion'] logging.info('FB async_job #{} percent done ' '{}%'.format(job['id'], percent)) if percent == 100 and (report['async_status'] == 'Job Completed'): try: complete_job = list(ar.get_result()) except FacebookRequestError as e: self.request_error(e) self.async_requests.append(job) complete_job = None except FacebookBadObjectError as e: logging.warning('Facebook Bad Object Error: {}'.format(e)) self.async_requests.append(job) complete_job = None if complete_job: self.df = self.df.append(complete_job, ignore_index=True) else: self.async_requests.append(job) if self.async_requests: time.sleep(30) self.check_and_get_async_jobs(self.async_requests)
def check_and_get_async_jobs(self, async_jobs): self.async_requests = [] for fb_request in async_jobs: try: job = fb_request.insights except AttributeError as e: logging.warning( 'A FB async_job does not contain insights and will ' 'be requested again. This is request #{} Error: {}'. format(fb_request.times_requested, e)) self.reset_report_request(fb_request) continue ar = AdReportRun(job['id']) report = self.get_report(ar) percent = report['async_percent_completion'] need_reset = fb_request.check_last_percent(percent) if need_reset: logging.warning( 'FB async_job #{} has been stuck for {} attempts and will ' 'be requested again. This is request #{}'.format( job['id'], fb_request.times_requested * 10, fb_request.times_requested)) self.reset_report_request(fb_request) continue logging.info('FB async_job #{} percent done ' '{}%'.format(job['id'], percent)) if percent == 100 and (report['async_status'] == 'Job Completed'): try: complete_job = list(ar.get_result()) except FacebookRequestError as e: self.request_error(e) self.async_requests.append(job) complete_job = None except FacebookBadObjectError as e: logging.warning('Facebook Bad Object Error: {}'.format(e)) self.async_requests.append(job) complete_job = None if complete_job: self.df = self.df.append(complete_job, ignore_index=True) fb_request.complete = True else: self.async_requests.append(fb_request) if self.async_requests: time.sleep(30) self.check_and_get_async_jobs(self.async_requests)