예제 #1
0
    def testReadWorkMessageAndProcess(self):
        worker = worker_module.Worker(DEFAULT_LOG_PATH,
                                      DEFAULT_SUB_MODULES_TO_SCAN)
        old_stdin = sys.stdin
        old_stdout = sys.__stdout__

        worker_module.CONNECTION_HOLDER = create_autospec(ConnectionHolder)
        processor_mock = create_autospec(BaseProcessor)
        work_message = WorkMessage("triggered task 1", [
            WorkMessageArg("arg11", "arg11 value"),
            WorkMessageArg("arg12", "arg12 value")
        ],
                                   lowPriority=True)
        processor_mock.process.return_value = [work_message]
        mock_processor_provider = create_autospec(ProcessorsProvider)
        mock_processor_provider.get_processor.return_value = processor_mock
        worker.processor_provider = mock_processor_provider

        sys.stdin = StringIO('{"%s":"indaqua","%s":{"%s":"Test","%s":[{"%s":"arg1","%s":"arg1 value"},{"%s":"arg2","%s":"arg2 value"}],"lowPriority":true}}\n%s\n'%\
                             (java_python_tokens.TENANT_ID, java_python_tokens.WORK_MESSAGE, java_python_tokens.TASK, java_python_tokens.ARGS, java_python_tokens.NAME, java_python_tokens.VALUE,\
                             java_python_tokens.NAME, java_python_tokens.VALUE, java_python_tokens.END))
        out = StringIO()
        sys.__stdout__ = out

        worker._readWorkMessageAndProcess()

        sys.stdin = old_stdin
        sys.__stdout__ = old_stdout

        wm_as_json = json_encode([work_message])
        expected_output = '{"%s":%s,"%s":"%s"}\n%s\n'%\
            (java_python_tokens.TRIGGERED_TASKS, wm_as_json, java_python_tokens.COMMAND, java_python_tokens.ACK, java_python_tokens.END)

        self.assertEquals(expected_output, out.getvalue())
예제 #2
0
    def test_json_decode_no_join_message(self, uuid_mock):
        uuid4_mock = Mock()
        uuid4_mock.get_hex.return_value = "1"
        uuid_mock.uuid4.return_value = uuid4_mock

        worker = worker_module.Worker(DEFAULT_LOG_PATH,
                                      DEFAULT_SUB_MODULES_TO_SCAN)

        worker_module.CONNECTION_HOLDER = create_autospec(ConnectionHolder)
        processor_mock = create_autospec(BaseProcessor)
        processor_mock.process.return_value = []
        mock_processor_provider = create_autospec(ProcessorsProvider)
        mock_processor_provider.get_processor.return_value = processor_mock
        worker.processor_provider = mock_processor_provider

        wm = WorkMessage(
            "Test",
            [WorkMessageArg("arg3", 1.0),
             WorkMessageArg("arg4", "bla")])
        work_message_as_json = json_encode(wm)
        input_as_json = '{"%s":"indaqua","%s":%s}\n%s\n'%\
                             (java_python_tokens.TENANT_ID,  java_python_tokens.WORK_MESSAGE, work_message_as_json, java_python_tokens.END)
        try:
            old_stdin = sys.stdin
            sys.stdin = StringIO(input_as_json)

            worker._readWorkMessageAndProcess()
        finally:
            sys.stdin = old_stdin

        work_message_arg = processor_mock.process.call_args[0][0]

        self.assertEqual(work_message_arg, wm)
예제 #3
0
 def process(self, work_message):
     sink_message = WorkMessage("bla", [WorkMessageArg("name", 1)])
     response_message = WorkMessage(
         "TestWithSink", [WorkMessageArg("name", 1)],
         WorkMessagesJoinState("join-id", sink_message))
     response_message.lowPriority = True
     return [response_message]
 def process(self, work_message):
     low_priority = work_message.lowPriority
     it_num = work_message.args[
         0].value if work_message.args is not None and len(
             work_message.args) >= 1 else 2
     join_state = None
     if it_num > 0:
         next_it_num = it_num - 1
         sink_message = WorkMessage("TriggerTaskChainReqursiveProcessor",
                                    [WorkMessageArg("it_num", next_it_num)],
                                    lowPriority=low_priority)
         join_state = WorkMessagesJoinState(
             "SleepJoinState" + str(int(time.time())), sink_message)
     new_message1 = WorkMessage(
         "Sleeping", [WorkMessageArg("time", 1),
                      WorkMessageArg("id", 1)],
         join_state,
         lowPriority=low_priority)
     new_message2 = WorkMessage(
         "Sleeping", [WorkMessageArg("time", 1),
                      WorkMessageArg("id", 2)],
         join_state,
         lowPriority=low_priority)
     return [new_message1, new_message2]
예제 #5
0
 def _parse_work_message_from_json(self, wm_as_dict):
     wm_ctor_dict = {}
     if java_python_tokens.ARGS in wm_as_dict:
         wm_ctor_dict['args'] = [
             WorkMessageArg(arg[java_python_tokens.NAME],
                            arg[java_python_tokens.VALUE])
             for arg in wm_as_dict[java_python_tokens.ARGS]
         ]
     if java_python_tokens.JOIN_STATE in wm_as_dict:
         join_state_as_dict = wm_as_dict[java_python_tokens.JOIN_STATE]
         if join_state_as_dict is not None:
             joinId = join_state_as_dict[java_python_tokens.JOIN_ID]
             sinkMessage = self._parse_work_message_from_json(
                 join_state_as_dict[java_python_tokens.SINK_MESSAGE])
             wm_ctor_dict['joinState'] = WorkMessagesJoinState(
                 joinId, sinkMessage)
     if java_python_tokens.LOW_PRIORITY in wm_as_dict:
         wm_ctor_dict['lowPriority'] = wm_as_dict[
             java_python_tokens.LOW_PRIORITY]
     return WorkMessage(wm_as_dict[java_python_tokens.TASK], **wm_ctor_dict)
예제 #6
0
 def process(self, work_message):
     sinkMessage = WorkMessage("TriggerTaskChainReqursiveProcessor",
                               lowPriority=work_message.lowPriority)
     joinState = WorkMessagesJoinState(
         "SleepingJoinState" + str(int(time.time())), sinkMessage)
     new_message1 = WorkMessage(
         "Sleeping", [WorkMessageArg("time", 1),
                      WorkMessageArg("id", 1)],
         joinState,
         lowPriority=work_message.lowPriority)
     new_message2 = WorkMessage(
         "Sleeping", [WorkMessageArg("time", 1),
                      WorkMessageArg("id", 2)],
         joinState,
         lowPriority=work_message.lowPriority)
     new_message3 = WorkMessage(
         "Sleeping", [WorkMessageArg("time", 1),
                      WorkMessageArg("id", 3)],
         joinState,
         lowPriority=work_message.lowPriority)
     return [new_message1, new_message2, new_message3]
예제 #7
0
 def process(self, work_message):
     my_number = int(work_message.args[0].value)
     tree_height = int(
         work_message.args[1].value) if len(work_message.args) >= 2 else 4
     i_need_to_fail = int(work_message.args[2].value) if len(
         work_message.args) >= 3 else my_number - 1
     if (i_need_to_fail == my_number):
         raise ValueError("i need to fail")
     time.sleep(1)
     print ",".join([str(my_number), str(tree_height), str(i_need_to_fail)])
     if my_number <= pow(2, tree_height - 1):
         print 'producing children ',
         left_child = WorkMessage(work_message.task, [
             WorkMessageArg('my_number', 2 * my_number),
             WorkMessageArg('tree_height', tree_height),
             WorkMessageArg('i_need_to_fail', i_need_to_fail)
         ])
         right_child = WorkMessage(work_message.task, [
             WorkMessageArg('my_number', 2 * my_number + 1),
             WorkMessageArg('tree_height', tree_height),
             WorkMessageArg('i_need_to_fail', i_need_to_fail)
         ])
         return [left_child, right_child]
     return []
예제 #8
0
                                                     sub_modules_to_scan)

    def run(self, tenantId, db_url, wm):
        try:
            self.processor_provider.load()
            CONNECTION_HOLDER.set_connection_urls({tenantId: db_url})
            processor_name = wm.task
            if not self.processor_provider.has_processor(processor_name):
                raise UnsupportedProcessorException(processor_name)
            CONNECTION_HOLDER.update_current_tenant(tenantId)
            return self.processor_provider.get_processor(
                processor_name).process(wm)
        except Exception, e:
            self._logger.error(traceback.format_exc(e))


if __name__ == '__main__':
    """
    Used for debug purposes
    """
    worker = ManualWorker(DEFAULT_SUB_MODULES_TO_SCAN)
    tenantId = 'sww'
    db_url = "postgresql://%s:%s@%s/%s_db" % (tenantId, tenantId, "localhost",
                                              tenantId)
    args = [
        WorkMessageArg(name="network_id", value=62400622),
        WorkMessageArg(name="end_time_bound", value=1410595320000)
    ]
    joinState = None
    wm = WorkMessage(task, args, joinState)
    worker.run(tenantId, db_url, wm)