Ejemplo n.º 1
0
 def __init__(self):
     self.task_queue = TaskQueue()
     self.task_worker = TaskWorker()
     self.redis_connection = redis.StrictRedis(
         host=Config.FRONTEND_IP,
         port=Config.REDIS_PORT,
         password=Config.REDIS_PASSWORD,
         encoding="utf-8",
         decode_responses=True)
     self.wait_task()
Ejemplo n.º 2
0
 def test_get_pending_task_size(self):
     task_queue = TaskQueue()
     task = {
         'emulation_time': 10,
         'dasl_filename': 'dsal2.yaml',
         'data_setting': '',
         'report_name': 'report3.json'
     }
     task_queue.queue.put(task)
     task_queue.queue.put(task)
     self.assertEqual(task_queue.get_pending_task_size(), 2)
Ejemplo n.º 3
0
 def test_add_task(self):
     task_queue = TaskQueue()
     task = {
         'emulation_time': 10,
         'dasl_filename': 'dsal1.yaml',
         'data_setting': '',
         'report_name': 'report1.json'
     }
     response = task_queue.add_task(task)
     self.assertTrue('emulation_task_id' in response)
     self.assertEqual(response['emulation_time'], task['emulation_time'])
     self.assertEqual(response['dasl_filename'], task['dasl_filename'])
     self.assertEqual(response['data_setting'], task['data_setting'])
     self.assertEqual(response['report_name'], task['report_name'])
Ejemplo n.º 4
0
 def test_get_first_task(self):
     task_queue = TaskQueue()
     task = {
         'emulation_time': 10,
         'dasl_filename': 'dsal2.yaml',
         'data_setting': '',
         'report_name': 'report3.json'
     }
     task_queue.queue.put(task)
     first_task = task_queue.get_first_task()
     self.assertEqual(first_task['emulation_time'], task['emulation_time'])
     self.assertEqual(first_task['dasl_filename'], task['dasl_filename'])
     self.assertEqual(first_task['data_setting'], task['data_setting'])
     self.assertEqual(first_task['report_name'], task['report_name'])
Ejemplo n.º 5
0
 def test_get_all_tasks(self):
     task_queue = TaskQueue()
     task1 = {
         'emulation_time': 10,
         'dasl_filename': 'dsal1.yaml',
         'data_setting': '',
         'report_name': 'report1.json'
     }
     task2 = {
         'emulation_time': 10,
         'dasl_filename': 'dsal1.yaml',
         'data_setting': '',
         'report_name': 'report1.json'
     }
     task_queue.queue.put(task1)
     task_queue.queue.put(task2)
     
     expected_tasks = task_queue.get_all_tasks()
     correct_tasks = [task1, task2]
     self.assertEqual(expected_tasks, correct_tasks)
Ejemplo n.º 6
0
 def test_cancel_pending_task(self):
     task_queue = TaskQueue()
     task1 = {
         'emulation_task_id': '1',
         'emulation_time': 10,
         'dasl_filename': 'dsal1.yaml',
         'data_setting': '',
         'report_name': 'report1.json'
     }
     task2 = {
         'emulation_task_id': '2',
         'emulation_time': 10,
         'dasl_filename': 'dsal2.yaml',
         'data_setting': '',
         'report_name': 'report3.json'
     }
     task_queue.queue.put(task1)
     task_queue.queue.put(task2)
     response = task_queue.cancel_pending_task(task2['emulation_task_id'])
     self.assertEqual(response, task2)
     self.assertEqual(task_queue.queue.qsize(), 1)
Ejemplo n.º 7
0
class TaskManager:
    def __init__(self):
        self.task_queue = TaskQueue()
        self.task_worker = TaskWorker()
        self.redis_connection = redis.StrictRedis(
            host=Config.FRONTEND_IP,
            port=Config.REDIS_PORT,
            password=Config.REDIS_PASSWORD,
            encoding="utf-8",
            decode_responses=True)
        self.wait_task()

    def wait_task(self):
        self.execution_thread = threading.Thread(target=self._exectue_task,
                                                 name='execution_thread',
                                                 args=())
        self.execution_thread.daemon = True
        self.execution_thread.start()

    def get_available_device(self):
        def check_heartbeat(ip_address):
            TIME_LIMIT = 2
            current_time = float(self.redis_connection.time()[0])
            worker_time = float(self.redis_connection.hget(ip_address, "time"))
            return current_time - worker_time < TIME_LIMIT

        time.sleep(1)
        avaliable_ip_address = []
        for ip in self.redis_connection.scan_iter("ip:*"):
            if check_heartbeat(ip):
                avaliable_ip_address.append(ip)

        return avaliable_ip_address

    def get_all_tasks(self):
        pending_tasks = self.task_queue.get_all_tasks()
        executing_task = self.task_worker.executing_task
        executing_task['emulation_status'] = self.get_executing_task_status()
        return [executing_task] + pending_tasks

    def get_task_size(self):
        return self.task_queue.get_pending_task_size() + int(
            self.task_worker.get_executing_task_id() != '0')

    def add_task_into_queue(self, task: dict):
        new_task = self.task_queue.add_task(task)
        return new_task

    def _manager_is_running(self):
        """
        This function is used to testting.
        """
        return True

    def _exectue_task(self):
        while self._manager_is_running():
            if self.task_queue.get_pending_task_size() > 0:
                print('execute task')
                task = self.task_queue.get_first_task()
                self.task_worker.execute_task(task)
                print('finish task')
            time.sleep(1)

    def _abort_executing_task(self):
        aborted_task = self.task_worker.abort_executing_task()
        return aborted_task

    def _cancel_task_from_queue(self, task_id):
        canceled_task = self.task_queue.cancel_pending_task(task_id)
        return canceled_task

    def delete_task(self, task_id):
        if self.task_worker.get_executing_task_id() == task_id:
            print('abort')
            deleted_task = self._abort_executing_task()
        else:
            deleted_task = self._cancel_task_from_queue(task_id)

        return deleted_task

    def get_executing_task(self):
        return self.task_worker.executing_task

    def get_executing_task_status(self):
        return self.task_worker.get_executing_task_status()