예제 #1
0
 def __init__(self, auth_folder, thread_id, project_id, bq_folder, scripts, date_target, period):
     threading.Thread.__init__(self)
     self.logger = logging.getLogger(__name__)
     self.thread_id = thread_id
     self.project_id = project_id
     self.scripts = scripts
     self.date_target = date_target
     self.period = period
     self.bq_folder = bq_folder
     self.exec_scripts = []
     self.auth_folder = auth_folder
     self.svc = get_service(auth_folder)
     
     if (self.period == 'W'):
         self.date_start = date_target
         self.date_end = util.add_days(date_target, 6)
         self.date_start_prev = util.add_days(date_target, -7)
     else:
         self.date_start = util.getDateMonthStart(date_target)
         self.date_end = util.getDateMonthEnd(date_target)
         self.date_start_prev = util.getDatePrevMonthStart(date_target)
예제 #2
0
def main(argv):
    
    # example calling: main W|M DATE_START [DATE_END]
    if (len(argv) < 3):
        print('example calling: main W|M DATE_START [DATE_END]')
        logger.warn('example calling: main W|M DATE_START [DATE_END]')
        sys.exit(9)
    
    
    logger.debug('argv: %s', argv)
    logger.debug('argv len: %d', len(argv))
    
    conf = config.load_config()
    project_id = conf['ProjectId']
    period = argv[1]
    make_full_trans = True;
    date_start = parse(argv[2])
    if (len(argv) == 4):
        date_end = parse(argv[3])
    else:
        date_end = date_start
    
    bq_folder = conf['ScriptFolder']
    file_path_full_trans = os.path.join(bq_folder, conf['Scripts']['JoinFullTransaction']['Script'])
    auth_folder = config.get_auth_folder()
    
    logger.info('project_id: %s', project_id)
    logger.info('make_full_trans: %s', make_full_trans)
    logger.info('period: %s', period)
    logger.info('date_start: %s', date_start)
    logger.info('date_end  : %s', date_end)
    
    bq_full_trans = read_txt_file(file_path_full_trans)
    
    if (period == 'M'):
        date_start = util.getDateMonthStart(date_start)
    
    todo_dates = []
    date_current = date_start    
    while date_end >= date_current:
        todo_dates.append(date_current)
        if (period == 'M'):
            date_current = util.getDateNextMonthStart(date_current)
        else:
            date_current = util.add_days(date_current, 1)

    days_waiting = []
    workers_doing = []
    workers_done = []
    max_concurrent = 15
            
    for d in todo_dates:
        logger.info('todo date: %s', d)
        days_waiting.append(d)
        # threadName = 'worker-nes-{:%Y%m%d}'.format(d)
#         workerNes = NesWorker(threadName, project_id, bq_folder=bq_folder, scripts=conf['Scripts']['CalculateNes'], date_target=d, period=period);
#         workers_waiting.append(workerNes)
        
    logger.info('count_days_waiting=%s', len(days_waiting))
    
    while(len(days_waiting) > 0):
        if (len(workers_doing) < max_concurrent):
            day_todo = days_waiting[0]
            threadName = 'worker-nes-{:%Y%m%d}'.format(day_todo)
            worker = NesWorker(auth_folder, threadName, project_id, bq_folder=bq_folder, scripts=conf['Scripts']['CalculateNes'], date_target=day_todo, period=period);
            workers_doing.append(worker)
            days_waiting.remove(day_todo)
            worker.start()
            logger.info('start worker:%s', worker.thread_id)
        else:
            logger.info('concurrent worker limit reached.')
            
        for w in workers_doing:    
            if (not w.is_alive()):
                workers_done.append(w)
                workers_doing.remove(w)
                
        logger.info('worker waiting: %s, doing: %s, done: %s', len(days_waiting), len(workers_doing), len(workers_done))    
        
        time.sleep(0.5)
    
    logger.info('all worker started. count_worker_doing=%s', len(workers_doing))
    
    for w in workers_doing:
        w.join()
        logger.info('worker joined. id=%s', w.thread_id)
    
    logger.info("Exiting Main Thread")