def _run_action(self, liveaction_db): extra = {'liveaction_db': liveaction_db} try: result = self.container.dispatch(liveaction_db) LOG.debug('Runner dispatch produced result: %s', result) if not result: raise ActionRunnerException('Failed to execute action.') except: _, ex, tb = sys.exc_info() extra['error'] = str(ex) LOG.info('Action "%s" failed: %s' % (liveaction_db.action, str(ex)), extra=extra) liveaction_db = action_utils.update_liveaction_status( status=action_constants.LIVEACTION_STATUS_FAILED, liveaction_id=liveaction_db.id, result={ 'error': str(ex), 'traceback': ''.join(traceback.format_tb(tb, 20)) }) executions.update_execution(liveaction_db) raise return result
def execute_action(self, liveaction): try: liveaction_db = get_liveaction_by_id(liveaction.id) except StackStormDBObjectNotFoundError: LOG.exception('Failed to find liveaction %s in the database.', liveaction.id) raise # Update liveaction status to "running" liveaction_db = update_liveaction_status(status=LIVEACTION_STATUS_RUNNING, liveaction_id=liveaction_db.id) # Launch action LOG.audit('Launching action execution.', extra={'liveaction': liveaction_db.to_serializable_dict()}) try: result = self.container.dispatch(liveaction_db) LOG.debug('Runner dispatch produced result: %s', result) except Exception: liveaction_db = update_liveaction_status(status=LIVEACTION_STATUS_FAILED, liveaction_id=liveaction_db.id) raise if not result: raise ActionRunnerException('Failed to execute action.') return result
def execute_action(self, actionexecution): try: actionexec_db = get_actionexec_by_id(actionexecution.id) except StackStormDBObjectNotFoundError: LOG.exception('Failed to find ActionExecution %s in the database.', actionexecution.id) raise # Update ActionExecution status to "running" actionexec_db = update_actionexecution_status(ACTIONEXEC_STATUS_RUNNING, actionexec_db.id) # Launch action LOG.audit('Launching Action command with actionexec_db="%s"', actionexec_db) try: result = self.container.dispatch(actionexec_db) LOG.debug('Runner dispatch produced result: %s', result) except Exception: actionexec_db = update_actionexecution_status(ACTIONEXEC_STATUS_FAILED, actionexec_db.id) raise if not result: raise ActionRunnerException('Failed to execute action.') return result
def _run_action(self, liveaction_db): # stamp liveaction with process_info runner_info = system_info.get_process_info() # Update liveaction status to "running" liveaction_db = action_utils.update_liveaction_status( status=action_constants.LIVEACTION_STATUS_RUNNING, runner_info=runner_info, liveaction_id=liveaction_db.id) self._running_liveactions.add(liveaction_db.id) action_execution_db = executions.update_execution(liveaction_db) # Launch action extra = { 'action_execution_db': action_execution_db, 'liveaction_db': liveaction_db } LOG.audit('Launching action execution.', extra=extra) # the extra field will not be shown in non-audit logs so temporarily log at info. LOG.info( 'Dispatched {~}action_execution: %s / {~}live_action: %s with "%s" status.', action_execution_db.id, liveaction_db.id, liveaction_db.status) extra = {'liveaction_db': liveaction_db} try: result = self.container.dispatch(liveaction_db) LOG.debug('Runner dispatch produced result: %s', result) if not result: raise ActionRunnerException('Failed to execute action.') except: _, ex, tb = sys.exc_info() extra['error'] = str(ex) LOG.info('Action "%s" failed: %s' % (liveaction_db.action, str(ex)), extra=extra) liveaction_db = action_utils.update_liveaction_status( status=action_constants.LIVEACTION_STATUS_FAILED, liveaction_id=liveaction_db.id, result={ 'error': str(ex), 'traceback': ''.join(traceback.format_tb(tb, 20)) }) executions.update_execution(liveaction_db) raise finally: # In the case of worker shutdown, the items are removed from _running_liveactions. # As the subprocesses for action executions are terminated, this finally block # will be executed. Set remove will result in KeyError if item no longer exists. # Use set discard to not raise the KeyError. self._running_liveactions.discard(liveaction_db.id) return result
def _run_action(self, liveaction_db): # stamp liveaction with process_info runner_info = system_info.get_process_info() # Update liveaction status to "running" liveaction_db = action_utils.update_liveaction_status( status=action_constants.LIVEACTION_STATUS_RUNNING, runner_info=runner_info, liveaction_id=liveaction_db.id) action_execution_db = executions.update_execution(liveaction_db) # Launch action extra = { 'action_execution_db': action_execution_db, 'liveaction_db': liveaction_db } LOG.audit('Launching action execution.', extra=extra) # the extra field will not be shown in non-audit logs so temporarily log at info. LOG.info( 'Dispatched {~}action_execution: %s / {~}live_action: %s with "%s" status.', action_execution_db.id, liveaction_db.id, liveaction_db.status) extra = {'liveaction_db': liveaction_db} try: result = self.container.dispatch(liveaction_db) LOG.debug('Runner dispatch produced result: %s', result) if not result: raise ActionRunnerException('Failed to execute action.') except: _, ex, tb = sys.exc_info() extra['error'] = str(ex) LOG.info('Action "%s" failed: %s' % (liveaction_db.action, str(ex)), extra=extra) liveaction_db = action_utils.update_liveaction_status( status=action_constants.LIVEACTION_STATUS_FAILED, liveaction_id=liveaction_db.id, result={ 'error': str(ex), 'traceback': ''.join(traceback.format_tb(tb, 20)) }) executions.update_execution(liveaction_db) raise return result
def execute_action(self, liveaction): # Note: We only want to execute actions which haven't completed yet if liveaction.status == LIVEACTION_STATUS_CANCELED: LOG.info('Not executing liveaction %s. User canceled execution.', liveaction.id) if not liveaction.result: update_liveaction_status(status=LIVEACTION_STATUS_CANCELED, result={'message': 'Action execution canceled by user.'}, liveaction_id=liveaction.id) return if liveaction.status in [LIVEACTION_STATUS_SUCCEEDED, LIVEACTION_STATUS_FAILED]: LOG.info('Ignoring liveaction %s which has already finished', liveaction.id) return try: liveaction_db = get_liveaction_by_id(liveaction.id) except StackStormDBObjectNotFoundError: LOG.exception('Failed to find liveaction %s in the database.', liveaction.id) raise # stamp liveaction with process_info runner_info = system_info.get_process_info() # Update liveaction status to "running" liveaction_db = update_liveaction_status(status=LIVEACTION_STATUS_RUNNING, runner_info=runner_info, liveaction_id=liveaction_db.id) action_execution_db = executions.update_execution(liveaction_db) # Launch action extra = {'action_execution_db': action_execution_db, 'liveaction_db': liveaction_db} LOG.audit('Launching action execution.', extra=extra) # the extra field will not be shown in non-audit logs so temporarily log at info. LOG.info('{~}action_execution: %s / {~}live_action: %s', action_execution_db.id, liveaction_db.id) try: result = self.container.dispatch(liveaction_db) LOG.debug('Runner dispatch produced result: %s', result) if not result: raise ActionRunnerException('Failed to execute action.') except Exception: liveaction_db = update_liveaction_status(status=LIVEACTION_STATUS_FAILED, liveaction_id=liveaction_db.id) raise return result
def process(self, liveaction): """Dispatches the LiveAction to appropriate action runner. LiveAction in statuses other than "scheduled" are ignored. If LiveAction is already canceled and result is empty, the LiveAction is updated with a generic exception message. :param liveaction: Scheduled action execution request. :type liveaction: ``st2common.models.db.liveaction.LiveActionDB`` :rtype: ``dict`` """ if liveaction.status == action_constants.LIVEACTION_STATUS_CANCELED: LOG.info('%s is not executing %s (id=%s) with "%s" status.', self.__class__.__name__, type(liveaction), liveaction.id, liveaction.status) if not liveaction.result: action_utils.update_liveaction_status( status=liveaction.status, result={'message': 'Action execution canceled by user.'}, liveaction_id=liveaction.id) return if liveaction.status != action_constants.LIVEACTION_STATUS_SCHEDULED: LOG.info('%s is not executing %s (id=%s) with "%s" status.', self.__class__.__name__, type(liveaction), liveaction.id, liveaction.status) return try: liveaction_db = action_utils.get_liveaction_by_id(liveaction.id) except StackStormDBObjectNotFoundError: LOG.exception('Failed to find liveaction %s in the database.', liveaction.id) raise # stamp liveaction with process_info runner_info = system_info.get_process_info() # Update liveaction status to "running" liveaction_db = action_utils.update_liveaction_status( status=action_constants.LIVEACTION_STATUS_RUNNING, runner_info=runner_info, liveaction_id=liveaction_db.id) action_execution_db = executions.update_execution(liveaction_db) # Launch action extra = {'action_execution_db': action_execution_db, 'liveaction_db': liveaction_db} LOG.audit('Launching action execution.', extra=extra) # the extra field will not be shown in non-audit logs so temporarily log at info. LOG.info('Dispatched {~}action_execution: %s / {~}live_action: %s with "%s" status.', action_execution_db.id, liveaction_db.id, liveaction.status) try: result = self.container.dispatch(liveaction_db) LOG.debug('Runner dispatch produced result: %s', result) if not result: raise ActionRunnerException('Failed to execute action.') except Exception as e: extra['error'] = str(e) LOG.info('Action "%s" failed: %s' % (liveaction_db.action, str(e)), extra=extra) liveaction_db = action_utils.update_liveaction_status( status=action_constants.LIVEACTION_STATUS_FAILED, liveaction_id=liveaction_db.id) raise return result