# Path: automlk/controller.py
# def controller_loop():
#     """
#     controller process: manages the search strategy and send instruction to workers
#     :return:
#     """
#     i_dataset = -1
#     # controls the optimization rounds and sends instructions to the workers
#     while True:
#         # check the list of datasets to search
#         active = [id for id in get_dataset_ids() if get_key_store('dataset:%s:status' % id) == 'searching']
# 
#         if len(active) == 0:
#             heart_beep('controller', {})
#             time.sleep(1)
#         else:
#             # sends work to the workers when their queue is empty
#             if llen_key_store(SEARCH_QUEUE) == 0:
#                 # get next dataset to search
#                 i_dataset += 1
#                 if i_dataset > len(active) - 1:
#                     i_dataset = 0
# 
#                 # retrieves dataset and status of search
#                 dataset_id = active[i_dataset]
# 
#                 # find search job
#                 msg_search = __create_search_round(dataset_id)
# 
#                 # send queue the next job to do
#                 if msg_search != {}:
#                     log.info('sending %s' % msg_search)
#                     lpush_key_store(SEARCH_QUEUE, msg_search)
#                     heart_beep('controller', msg_search)
# 
#         # then read the duplicate ROUND queue
#         while llen_key_store(DUPLICATE_QUEUE) > 0:
#             msg = brpop_key_store(DUPLICATE_QUEUE)
#             msg_search = __duplicate_search_round(msg['round'], msg['dataset'])
# 
#             # send queue the next job to do
#             log.info('sending %s' % msg_search)
#             lpush_key_store(SEARCH_QUEUE, msg_search)
#             heart_beep('controller', msg_search)
# 
#         # then read the results queue
#         while llen_key_store(RESULTS_QUEUE) > 0:
#             msg_result = brpop_key_store(RESULTS_QUEUE)
#             if msg_result is not None:
#                 __process_result(msg_result)
# 
#         # then wait 1 second
#         time.sleep(1)
# 
# 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 logging
import sys
import os
from automlk.controller import controller_loop
from automlk.context import get_data_folder


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

logging.info('starting controller')

while True:
    try:

# Path: automlk/controller.py
# def controller_loop():
#     """
#     controller process: manages the search strategy and send instruction to workers
#     :return:
#     """
#     i_dataset = -1
#     # controls the optimization rounds and sends instructions to the workers
#     while True:
#         # check the list of datasets to search
#         active = [id for id in get_dataset_ids() if get_key_store('dataset:%s:status' % id) == 'searching']
# 
#         if len(active) == 0:
#             heart_beep('controller', {})
#             time.sleep(1)
#         else:
#             # sends work to the workers when their queue is empty
#             if llen_key_store(SEARCH_QUEUE) == 0:
#                 # get next dataset to search
#                 i_dataset += 1
#                 if i_dataset > len(active) - 1:
#                     i_dataset = 0
# 
#                 # retrieves dataset and status of search
#                 dataset_id = active[i_dataset]
# 
#                 # find search job
#                 msg_search = __create_search_round(dataset_id)
# 
#                 # send queue the next job to do
#                 if msg_search != {}:
#                     log.info('sending %s' % msg_search)
#                     lpush_key_store(SEARCH_QUEUE, msg_search)
#                     heart_beep('controller', msg_search)
# 
#         # then read the duplicate ROUND queue
#         while llen_key_store(DUPLICATE_QUEUE) > 0:
#             msg = brpop_key_store(DUPLICATE_QUEUE)
#             msg_search = __duplicate_search_round(msg['round'], msg['dataset'])
# 
#             # send queue the next job to do
#             log.info('sending %s' % msg_search)
#             lpush_key_store(SEARCH_QUEUE, msg_search)
#             heart_beep('controller', msg_search)
# 
#         # then read the results queue
#         while llen_key_store(RESULTS_QUEUE) > 0:
#             msg_result = brpop_key_store(RESULTS_QUEUE)
#             if msg_result is not None:
#                 __process_result(msg_result)
# 
#         # then wait 1 second
#         time.sleep(1)
# 
# 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_controller.py
import logging
import sys
import os
from automlk.controller import controller_loop
from automlk.context import get_data_folder



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

logging.info('starting controller')

while True:
    try:

        controller_loop()