# Path: automlk/worker.py
# def worker_loop(worker_id, gpu=False):
#     """
#     periodically pool the receiver queue for a search job
#     :param worker_id: index of the worker on this machine
#     :param gpu: can use gpu on this machine
#     :return:
#     """
#     global __worker_timer_start
#     global __worker_timer_limit
#     global __worker_dataset
#     __worker_dataset = ''
#     __worker_timer_start = 0
#     __worker_timer_limit = 0
#     f_stop = threading.Event()
#     # start calling f now and every 60 sec thereafter
#     __timer_control(f_stop)
#     while True:
#         try:
#             # poll queue
#             msg_search = brpop_key_store('controller:search_queue')
#             heart_beep('worker', msg_search, worker_id, gpu)
#             __worker_timer_start = time.time()
#             __worker_timer_limit = 0
#             __worker_dataset = ''
#             if msg_search is not None:
#                 __worker_dataset = msg_search['dataset_id']
#                 __worker_timer_limit = msg_search['time_limit']
#                 log.info('received %s' % msg_search)
#                 msg_search = {**msg_search, **{'start_time': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
#                                                'host_name': socket.gethostname()}}
#                 job_search(msg_search)
#         except KeyboardInterrupt:
#             log.info('Keyboard interrupt: exiting')
#             # stop the timer thread
#             f_stop.set()
#             exit()
#         except Exception as e:
#             exc_type, exc_obj, exc_tb = sys.exc_info()
#             fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
#             log.error('%s in %s line:%s error: %s' % (exc_type.__name__, fname, str(exc_tb.tb_lineno), str(e)))
#             with open(get_data_folder() + '/errors.txt', 'a') as f:
#                 f.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + str(msg_search) + '\n')
#                 f.write('%s in %s line:%s error: %s' % (exc_type.__name__, fname, str(exc_tb.tb_lineno), str(e)) + '\n')
#                 f.write('-'*80 + '\n')
# 
# Path: automlk/context.py
# def get_data_folder():
#     """
#     retrieves root folder from 'automlk.json' configuration file
#     :return: storage folder name of the data
#     """
#     return get_config()['data']


import sys
import logging
from automlk.worker import worker_loop
from automlk.context import get_data_folder


logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)5s [%(module)s %(lineno)3d] %(message)s',
                    handlers=[
                        logging.FileHandler(get_data_folder() + '/worker.log'),
                        logging.StreamHandler()
                    ])

logging.info('starting worker')

if len(sys.argv) > 1:
    wid = sys.argv[1]
else:
    wid = 1

if len(sys.argv) > 2:
    gpu = sys.argv[2]
else:
    gpu = False


# Path: automlk/worker.py
# def worker_loop(worker_id, gpu=False):
#     """
#     periodically pool the receiver queue for a search job
#     :param worker_id: index of the worker on this machine
#     :param gpu: can use gpu on this machine
#     :return:
#     """
#     global __worker_timer_start
#     global __worker_timer_limit
#     global __worker_dataset
#     __worker_dataset = ''
#     __worker_timer_start = 0
#     __worker_timer_limit = 0
#     f_stop = threading.Event()
#     # start calling f now and every 60 sec thereafter
#     __timer_control(f_stop)
#     while True:
#         try:
#             # poll queue
#             msg_search = brpop_key_store('controller:search_queue')
#             heart_beep('worker', msg_search, worker_id, gpu)
#             __worker_timer_start = time.time()
#             __worker_timer_limit = 0
#             __worker_dataset = ''
#             if msg_search is not None:
#                 __worker_dataset = msg_search['dataset_id']
#                 __worker_timer_limit = msg_search['time_limit']
#                 log.info('received %s' % msg_search)
#                 msg_search = {**msg_search, **{'start_time': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
#                                                'host_name': socket.gethostname()}}
#                 job_search(msg_search)
#         except KeyboardInterrupt:
#             log.info('Keyboard interrupt: exiting')
#             # stop the timer thread
#             f_stop.set()
#             exit()
#         except Exception as e:
#             exc_type, exc_obj, exc_tb = sys.exc_info()
#             fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
#             log.error('%s in %s line:%s error: %s' % (exc_type.__name__, fname, str(exc_tb.tb_lineno), str(e)))
#             with open(get_data_folder() + '/errors.txt', 'a') as f:
#                 f.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + str(msg_search) + '\n')
#                 f.write('%s in %s line:%s error: %s' % (exc_type.__name__, fname, str(exc_tb.tb_lineno), str(e)) + '\n')
#                 f.write('-'*80 + '\n')
# 
# Path: automlk/context.py
# def get_data_folder():
#     """
#     retrieves root folder from 'automlk.json' configuration file
#     :return: storage folder name of the data
#     """
#     return get_config()['data']


# Path: run/run_worker.py
import sys
import logging
from automlk.worker import worker_loop
from automlk.context import get_data_folder



logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)5s [%(module)s %(lineno)3d] %(message)s',
                    handlers=[
                        logging.FileHandler(get_data_folder() + '/worker.log'),
                        logging.StreamHandler()
                    ])

logging.info('starting worker')

if len(sys.argv) > 1:
    wid = sys.argv[1]
else:
    wid = 1

if len(sys.argv) > 2:
    gpu = sys.argv[2]
else:
    gpu = False


worker_loop(wid, gpu)