예제 #1
0
파일: base.py 프로젝트: bsyk/st2
 def publish_state(cls, payload, state):
     try:
         if isinstance(payload, LiveActionDB):
             worker.get_worker().process(payload)
     except Exception:
         traceback.print_exc()
         print(payload)
예제 #2
0
 def publish_state(cls, payload, state):
     try:
         if isinstance(payload, LiveActionDB):
             if state == action_constants.LIVEACTION_STATUS_REQUESTED:
                 cls.process(payload)
             else:
                 worker.get_worker().process(payload)
     except Exception:
         traceback.print_exc()
         print(payload)
예제 #3
0
파일: liveaction.py 프로젝트: nzlosh/st2
 def publish_state(cls, payload, state):
     try:
         if isinstance(payload, LiveActionDB):
             if state == action_constants.LIVEACTION_STATUS_REQUESTED:
                 cls.process(payload)
             else:
                 worker.get_worker().process(payload)
     except Exception:
         traceback.print_exc()
         print(payload)
예제 #4
0
def _run_worker():
    LOG.info('(PID=%s) Worker started.', os.getpid())

    action_worker = worker.get_worker()

    try:
        action_worker.start()
        action_worker.wait()
    except (KeyboardInterrupt, SystemExit):
        LOG.info('(PID=%s) Worker stopped.', os.getpid())

        errors = False

        try:
            action_worker.shutdown()
        except:
            LOG.exception('Unable to shutdown worker.')
            errors = True

        if errors:
            return 1
    except:
        LOG.exception('(PID=%s) Worker unexpectedly stopped.', os.getpid())
        return 1

    return 0
def _run_worker():
    LOG.info('(PID=%s) Worker started.', os.getpid())

    components = [
        scheduler.get_scheduler(),
        worker.get_worker()
    ]

    try:
        for component in components:
            component.start()

        for component in components:
            component.wait()
    except (KeyboardInterrupt, SystemExit):
        LOG.info('(PID=%s) Worker stopped.', os.getpid())

        errors = False

        for component in components:
            try:
                component.shutdown()
            except:
                LOG.exception('Unable to shutdown %s.', component.__class__.__name__)
                errors = True

        if errors:
            return 1
    except:
        LOG.exception('(PID=%s) Worker unexpectedly stopped.', os.getpid())
        return 1

    return 0
예제 #6
0
def _run_worker():
    LOG.info("(PID=%s) Worker started.", os.getpid())

    action_worker = worker.get_worker()
    _setup_sigterm_handler(action_worker)
    try:
        action_worker.start()
        action_worker.wait()
    except (KeyboardInterrupt, SystemExit):
        LOG.info("(PID=%s) Worker stopped.", os.getpid())

        errors = False

        try:
            deregister_service(service=ACTIONRUNNER)
            action_worker.shutdown()
        except:
            LOG.exception("Unable to shutdown worker.")
            errors = True

        if errors:
            return 1
    except:
        LOG.exception("(PID=%s) Worker unexpectedly stopped.", os.getpid())
        return 1

    return 0
예제 #7
0
def _run_worker():
    LOG.info('(PID=%s) Worker started.', os.getpid())

    components = [
        scheduler.get_scheduler(),
        worker.get_worker()
    ]

    try:
        for component in components:
            component.start()

        for component in components:
            component.wait()
    except (KeyboardInterrupt, SystemExit):
        LOG.info('(PID=%s) Worker stopped.', os.getpid())

        errors = False

        for component in components:
            try:
                component.shutdown()
            except:
                LOG.exception('Unable to shutdown %s.', component.__class__.__name__)
                errors = True

        if errors:
            return 1
    except:
        LOG.exception('(PID=%s) Worker unexpectedly stopped.', os.getpid())
        return 1

    return 0
예제 #8
0
    def test_worker_shutdown(self):
        cfg.CONF.set_override(name="graceful_shutdown",
                              override=False,
                              group="actionrunner")
        action_worker = actions_worker.get_worker()
        temp_file = None

        # Create a temporary file that is deleted when the file is closed and then set up an
        # action to wait for this file to be deleted. This allows this test to run the action
        # over a separate thread, run the shutdown sequence on the main thread, and then let
        # the local runner to exit gracefully and allow _run_action to finish execution.
        with tempfile.NamedTemporaryFile() as fp:
            temp_file = fp.name
            self.assertIsNotNone(temp_file)
            self.assertTrue(os.path.isfile(temp_file))

            # Launch the action execution in a separate thread.
            params = {
                "cmd": "while [ -e '%s' ]; do sleep 0.1; done" % temp_file
            }
            liveaction_db = self._get_liveaction_model(
                WorkerTestCase.local_action_db, params)
            liveaction_db = LiveAction.add_or_update(liveaction_db)
            executions.create_execution_object(liveaction_db)
            runner_thread = eventlet.spawn(action_worker._run_action,
                                           liveaction_db)

            # Wait for the worker up to 10s to add the liveaction to _running_liveactions.
            for i in range(0, int(10 / 0.1)):
                eventlet.sleep(0.1)
                if len(action_worker._running_liveactions) > 0:
                    break

            self.assertEqual(len(action_worker._running_liveactions), 1)

            # Shutdown the worker to trigger the abandon process.
            action_worker.shutdown()
            liveaction_db = LiveAction.get_by_id(liveaction_db.id)

            # Verify that _running_liveactions is empty and the liveaction is abandoned.
            self.assertEqual(len(action_worker._running_liveactions), 0)
            self.assertEqual(
                liveaction_db.status,
                action_constants.LIVEACTION_STATUS_ABANDONED,
                str(liveaction_db),
            )

        # Make sure the temporary file has been deleted.
        self.assertFalse(os.path.isfile(temp_file))

        # Wait for the local runner to complete. This will activate the finally block in
        # _run_action but will not result in KeyError because the discard method is used to
        # to remove the liveaction from _running_liveactions.
        runner_thread.wait()
예제 #9
0
파일: liveaction.py 프로젝트: nzlosh/st2
 def publish_state(cls, payload, state):
     try:
         if isinstance(payload, LiveActionDB):
             if state == action_constants.LIVEACTION_STATUS_REQUESTED:
                 thread = eventlet.spawn(cls.process, payload)
                 cls.threads.append(thread)
             else:
                 thread = eventlet.spawn(worker.get_worker().process, payload)
                 cls.threads.append(thread)
     except Exception:
         traceback.print_exc()
         print(payload)
예제 #10
0
 def publish_state(cls, payload, state):
     try:
         if isinstance(payload, LiveActionDB):
             if state == action_constants.LIVEACTION_STATUS_REQUESTED:
                 thread = eventlet.spawn(cls.process, payload)
                 cls.threads.append(thread)
             else:
                 thread = eventlet.spawn(worker.get_worker().process, payload)
                 cls.threads.append(thread)
     except Exception:
         traceback.print_exc()
         print(payload)
예제 #11
0
    def test_non_utf8_action_result_string(self):
        action_worker = actions_worker.get_worker()
        params = {"cmd": "python -c 'print \"\\x82\"'"}
        liveaction_db = self._get_liveaction_model(WorkerTestCase.local_action_db, params)
        liveaction_db = LiveAction.add_or_update(liveaction_db)
        execution_db = executions.create_execution_object(liveaction_db)

        try:
            action_worker._run_action(liveaction_db)
        except InvalidStringData:
            liveaction_db = LiveAction.get_by_id(liveaction_db.id)
            self.assertEqual(liveaction_db.status, "failed")
            self.assertTrue("error" in liveaction_db.result)
            self.assertTrue("traceback" in liveaction_db.result)
            execution_db = ActionExecution.get_by_id(execution_db.id)
            self.assertEqual(liveaction_db.status, "failed")
예제 #12
0
    def test_non_utf8_action_result_string(self):
        action_worker = actions_worker.get_worker()
        params = {'cmd': "python -c 'print \"\\x82\"'"}
        liveaction_db = self._get_liveaction_model(
            WorkerTestCase.local_action_db, params)
        liveaction_db = LiveAction.add_or_update(liveaction_db)
        execution_db = executions.create_execution_object(liveaction_db)

        try:
            action_worker._run_action(liveaction_db)
        except InvalidStringData:
            liveaction_db = LiveAction.get_by_id(liveaction_db.id)
            self.assertEqual(liveaction_db.status, "failed")
            self.assertTrue('error' in liveaction_db.result)
            self.assertTrue('traceback' in liveaction_db.result)
            execution_db = ActionExecution.get_by_id(execution_db.id)
            self.assertEqual(liveaction_db.status, "failed")
예제 #13
0
파일: test_worker.py 프로젝트: nzlosh/st2
    def test_non_utf8_action_result_string(self):
        action_worker = actions_worker.get_worker()
        params = {
            'cmd': "python -c 'print \"\\x82\"'"
        }
        liveaction_db = self._get_liveaction_model(WorkerTestCase.local_action_db, params)
        liveaction_db = LiveAction.add_or_update(liveaction_db)
        execution_db = executions.create_execution_object(liveaction_db)

        try:
            action_worker._run_action(liveaction_db)
        except InvalidStringData:
            liveaction_db = LiveAction.get_by_id(liveaction_db.id)
            self.assertEqual(liveaction_db.status, action_constants.LIVEACTION_STATUS_FAILED)
            self.assertTrue('error' in liveaction_db.result)
            self.assertTrue('traceback' in liveaction_db.result)
            execution_db = ActionExecution.get_by_id(execution_db.id)
            self.assertEqual(liveaction_db.status, action_constants.LIVEACTION_STATUS_FAILED)
예제 #14
0
파일: test_worker.py 프로젝트: nzlosh/st2
    def test_worker_shutdown(self):
        action_worker = actions_worker.get_worker()
        temp_file = None

        # Create a temporary file that is deleted when the file is closed and then set up an
        # action to wait for this file to be deleted. This allows this test to run the action
        # over a separate thread, run the shutdown sequence on the main thread, and then let
        # the local runner to exit gracefully and allow _run_action to finish execution.
        with tempfile.NamedTemporaryFile() as fp:
            temp_file = fp.name
            self.assertIsNotNone(temp_file)
            self.assertTrue(os.path.isfile(temp_file))

            # Launch the action execution in a separate thread.
            params = {'cmd': 'while [ -e \'%s\' ]; do sleep 0.1; done' % temp_file}
            liveaction_db = self._get_liveaction_model(WorkerTestCase.local_action_db, params)
            liveaction_db = LiveAction.add_or_update(liveaction_db)
            executions.create_execution_object(liveaction_db)
            runner_thread = eventlet.spawn(action_worker._run_action, liveaction_db)

            # Wait for the worker up to 10s to add the liveaction to _running_liveactions.
            for i in range(0, int(10 / 0.1)):
                eventlet.sleep(0.1)
                if len(action_worker._running_liveactions) > 0:
                    break

            self.assertEqual(len(action_worker._running_liveactions), 1)

            # Shutdown the worker to trigger the abandon process.
            action_worker.shutdown()
            liveaction_db = LiveAction.get_by_id(liveaction_db.id)

            # Verify that _running_liveactions is empty and the liveaction is abandoned.
            self.assertEqual(len(action_worker._running_liveactions), 0)
            self.assertEqual(liveaction_db.status, action_constants.LIVEACTION_STATUS_ABANDONED,
                             str(liveaction_db))

        # Make sure the temporary file has been deleted.
        self.assertFalse(os.path.isfile(temp_file))

        # Wait for the local runner to complete. This will activate the finally block in
        # _run_action but will not result in KeyError because the discard method is used to
        # to remove the liveaction from _running_liveactions.
        runner_thread.wait()
예제 #15
0
    def test_non_utf8_action_result_string(self):
        action_worker = actions_worker.get_worker()
        params = {"cmd": "python -c 'print \"\\x82\"'"}
        liveaction_db = self._get_liveaction_model(
            WorkerTestCase.local_action_db, params)
        liveaction_db = LiveAction.add_or_update(liveaction_db)
        execution_db = executions.create_execution_object(liveaction_db)

        try:
            action_worker._run_action(liveaction_db)
        except InvalidStringData:
            liveaction_db = LiveAction.get_by_id(liveaction_db.id)
            self.assertEqual(liveaction_db.status,
                             action_constants.LIVEACTION_STATUS_FAILED)
            self.assertIn("error", liveaction_db.result)
            self.assertIn("traceback", liveaction_db.result)
            execution_db = ActionExecution.get_by_id(execution_db.id)
            self.assertEqual(liveaction_db.status,
                             action_constants.LIVEACTION_STATUS_FAILED)
예제 #16
0
파일: test_worker.py 프로젝트: ekhavana/st2
    def test_worker_shutdown(self):
        action_worker = actions_worker.get_worker()

        # Create a temporary file that is deleted when the file is closed and then set up an
        # action to wait for this file to be deleted. This allows this test to run the action
        # over a separate thread, run the shutdown sequence on the main thread, and then let
        # the local runner to exit gracefully and allow _run_action to finish execution.
        with tempfile.NamedTemporaryFile() as fp:
            params = {
                'cmd': 'while [ -e \'%s\' ]; do sleep 0.1; done' % fp.name
            }
            liveaction_db = self._get_liveaction_model(
                WorkerTestCase.local_action_db, params)
            liveaction_db = LiveAction.add_or_update(liveaction_db)
            executions.create_execution_object(liveaction_db)
            runner_thread = eventlet.spawn(action_worker._run_action,
                                           liveaction_db)

            # Wait for the worker to add the liveaction to _running_liveactions.
            while len(action_worker._running_liveactions) <= 0:
                eventlet.sleep(0.1)

            self.assertEqual(len(action_worker._running_liveactions), 1)

            # Shutdown the worker to trigger the abandon process.
            action_worker.shutdown()
            liveaction_db = LiveAction.get_by_id(liveaction_db.id)

            # Verify that _running_liveactions is empty and the liveaction is abandoned.
            self.assertEqual(len(action_worker._running_liveactions), 0)
            self.assertEqual(liveaction_db.status,
                             action_constants.LIVEACTION_STATUS_ABANDONED)

        # Wait for the local runner to complete. This will activate the finally block in
        # _run_action but will not result in KeyError because the discard method is used to
        # to remove the liveaction from _running_liveactions.
        runner_thread.wait()
예제 #17
0
 def __init__(self, *args, **kwargs):
     super(QueueConsumerTest, self).__init__(*args, **kwargs)
     self.scheduler = scheduler.get_scheduler()
     self.dispatcher = worker.get_worker()
예제 #18
0
 def __init__(self, *args, **kwargs):
     super(QueueConsumerTest, self).__init__(*args, **kwargs)
     self.scheduler = scheduling.get_scheduler_entrypoint()
     self.scheduling_queue = scheduling_queue.get_handler()
     self.dispatcher = worker.get_worker()
예제 #19
0
 def __init__(self, *args, **kwargs):
     super(QueueConsumerTest, self).__init__(*args, **kwargs)
     self.scheduler = scheduler.get_scheduler()
     self.dispatcher = worker.get_worker()
예제 #20
0
 def __init__(self, *args, **kwargs):
     super(QueueConsumerTest, self).__init__(*args, **kwargs)
     self.scheduler = scheduling.get_scheduler_entrypoint()
     self.scheduling_queue = scheduling_queue.get_handler()
     self.dispatcher = worker.get_worker()