예제 #1
0
    def add_new_task(self, task: Task, estimated_fee: int = 0) -> None:
        task_id = task.header.task_id
        if task_id in self.tasks:
            raise RuntimeError("Task {} has been already added".format(
                task.header.task_id))
        if not self.key_id:
            raise ValueError("'key_id' is not set")

        task.header.fixed_header.task_owner = self.node
        task.header.signature = self.sign_task_header(task.header)

        task.create_reference_data_for_task_validation()
        task.register_listener(self)

        ts = TaskState()
        ts.status = TaskStatus.notStarted
        ts.outputs = task.get_output_names()
        ts.subtasks_count = task.get_total_tasks()
        ts.time_started = time.time()
        ts.estimated_cost = task.price
        ts.estimated_fee = estimated_fee

        self.tasks[task_id] = task
        self.tasks_states[task_id] = ts
        logger.info("Task %s added", task_id)

        self.notice_task_updated(task_id, op=TaskOp.CREATED, persist=False)
예제 #2
0
    def test_task_simple_serializer(self):
        with self.assertRaises(TypeError):
            Task.build_task("Not Task Builder")
        with self.assertRaises(TypeError):
            Task.register_listener("Not Listener")
        t = Task(Mock(), "")
        self.assertIsInstance(t, Task)
        self.assertEqual(t.get_stdout("abc"), "")
        self.assertEqual(t.get_stderr("abc"), "")
        self.assertEqual(t.get_results("abc"), [])

        t = Task(
            TaskHeader("ABC", "xyz", "10.10.10.10", 1023, "key", "DEFAULT",
                       Node()), "print 'Hello world'")

        tl1 = TaskEventListener()
        tl2 = TaskEventListener()
        t.register_listener(tl1)
        t.register_listener(tl2)
        assert len(t.listeners) == 2
        p = SimpleSerializer.dumps(t)
        u = SimpleSerializer.loads(p)
        assert t.src_code == u.src_code
        assert t.header.task_id == u.header.task_id
        assert t.header.task_owner.node_name == u.header.task_owner.node_name
        assert u.get_results("abc") == []
        assert len(t.listeners) == 2
        assert len(u.listeners) == 0
        t.unregister_listener(tl2)
        assert len(t.listeners) == 1
        assert t.listeners[0] == tl1
        t.listeners[0].notify_update_task("abc")
        t.unregister_listener(tl1)
        assert len(t.listeners) == 0
        with self.assertLogs(logger, level="WARNING"):
            t.unregister_listener(tl1)