import sys import time import datetime import argparse from subprocess import Popen, PIPE, CalledProcessError pythonpath = '/home/lyin/anaconda3/bin/python' airflowpath = '/home/lyin/anaconda3/bin/airflow' dag_path = 'yt_reports_{}' days_ago = 3 logfile = '/home/lyin/airflow/dags/projects/youtube_reports_api/log/{}/{}.log' start_date = (datetime.datetime.now() - datetime.timedelta(days=days_ago)).strftime( "%Y-%m-%d") end_date = False delta = False content_owners = ['ENT', 'IODA', 'ORCH'] exit_codes = ["INFO - Backfill done. Exiting.", "BackfillJob is deadlocked."] # argument flags parser = argparse.ArgumentParser(description='Retrieve reports from a date') parser.add_argument("log", nargs='?', default=False) parser.add_argument("start_date", nargs='?', default=False) parser.add_argument("end_date", nargs='?', default=False) args = parser.parse_args() if args.start_date != False: start_date = args.start_date if args.end_date != False: end_date = args.end_date def execute(cmd): """ Executes a line """ popen = Popen(cmd,stdout=PIPE, stderr=PIPE) stdout_lines = iter(popen.stdout.readline, "") for stdout_line in stdout_lines: # return the output yield stdout_line.decode() if any(exit in stdout_line.decode() for exit in exit_codes): # This return means we're finished. popen.kill() return popen.stdout.close() return_code = popen.wait() if return_code != 0: raise CalledProcessError(return_code, cmd) def run_dags(start_date): """ Backfills job for each content owner. """ for content_owner in content_owners: # this is what goes into the CL command = [airflowpath, 'backfill', dag_path.format(content_owner), '-s', start_date] with open(logfile.format(content_owner, start_date), 'a+') as f: print(' '.join(command)) f.write(79 * '=' + '\n') f.write(' '.join(command)) for path in execute(command): if args.log != False: print(path,end="") f.write(path) time.sleep(5) if end_date: start_datetime = datetime.datetime.strptime(start_date, '%Y-%m-%d') end_dateime = datetime.datetime.strptime(end_date, '%Y-%m-%d') delta = end_dateime - start_datetime for date_diff in range(0, delta.days + 1): execution_datetime = start_datetime + datetime.timedelta(days=date_diff) run_dags( execution_datetime.strftime('%Y-%m-%d') ) else: run_dags( start_date )