本文整理汇总了Python中st2common.services.executions.update_execution函数的典型用法代码示例。如果您正苦于以下问题:Python update_execution函数的具体用法?Python update_execution怎么用?Python update_execution使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了update_execution函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _update_status
def _update_status(self, liveaction_id, status, result, context):
try:
LOG.debug('Setting status: %s for liveaction: %s', status, liveaction_id)
liveaction_db, state_changed = self._update_live_action_db(
liveaction_id, status, result, context)
except Exception as e:
LOG.exception(
'Cannot update liveaction '
'(id: %s, status: %s, result: %s).' % (
liveaction_id, status, result)
)
raise e
try:
executions.update_execution(liveaction_db, publish=state_changed)
extra = {'liveaction_db': liveaction_db}
LOG.debug('Updated liveaction after run', extra=extra)
except Exception as e:
LOG.exception(
'Cannot update action execution for liveaction '
'(id: %s, status: %s, result: %s).' % (
liveaction_id, status, result)
)
raise e
return liveaction_db
示例2: _mark_inquiry_complete
def _mark_inquiry_complete(self, inquiry_id, result):
"""Mark Inquiry as completed
This function updates the local LiveAction and Execution with a successful
status as well as call the "post_run" function for the Inquirer runner so that
the appropriate callback function is executed
:param inquiry: The Inquiry for which the response is given
:param requester_user: The user providing the response
:rtype: bool - True if requester_user is able to respond. False if not.
"""
# Update inquiry's execution result with a successful status and the validated response
liveaction_db = action_utils.update_liveaction_status(
status=action_constants.LIVEACTION_STATUS_SUCCEEDED,
runner_info=system_info.get_process_info(),
result=result,
liveaction_id=inquiry_id)
executions.update_execution(liveaction_db)
# Call Inquiry runner's post_run to trigger callback to workflow
runner_container = get_runner_container()
action_db = get_action_by_ref(liveaction_db.action)
runnertype_db = get_runnertype_by_name(action_db.runner_type['name'])
runner = runner_container._get_runner(runnertype_db, action_db, liveaction_db)
runner.post_run(status=action_constants.LIVEACTION_STATUS_SUCCEEDED, result=result)
return liveaction_db
示例3: _do_cancel
def _do_cancel(self, runner, runnertype_db, action_db, liveaction_db):
try:
extra = {"runner": runner}
LOG.debug("Performing cancel for runner: %s", (runner.runner_id), extra=extra)
runner.cancel()
liveaction_db = update_liveaction_status(
status=action_constants.LIVEACTION_STATUS_CANCELED,
end_timestamp=date_utils.get_datetime_utc_now(),
liveaction_db=liveaction_db,
)
executions.update_execution(liveaction_db)
LOG.debug("Performing post_run for runner: %s", runner.runner_id)
result = {"error": "Execution canceled by user."}
runner.post_run(status=liveaction_db.status, result=result)
runner.container_service = None
except:
_, ex, tb = sys.exc_info()
# include the error message and traceback to try and provide some hints.
result = {"error": str(ex), "traceback": "".join(traceback.format_tb(tb, 20))}
LOG.exception("Failed to cancel action %s." % (liveaction_db.id), extra=result)
finally:
# Always clean-up the auth_token
status = liveaction_db.status
self._clean_up_auth_token(runner=runner, status=status)
return liveaction_db
示例4: purge_inquiries
def purge_inquiries(logger):
"""Purge Inquiries that have exceeded their configured TTL
At the moment, Inquiries do not have their own database model, so this function effectively
is another, more specialized GC for executions. It will look for executions with a 'pending'
status that use the 'inquirer' runner, which is the current definition for an Inquiry.
Then it will mark those that have a nonzero TTL have existed longer than their TTL as
"timed out". It will then request that the parent workflow(s) resume, where the failure
can be handled as the user desires.
"""
# Get all existing Inquiries
filters = {'runner__name': 'inquirer', 'status': action_constants.LIVEACTION_STATUS_PENDING}
inquiries = list(ActionExecution.query(**filters))
gc_count = 0
# Inspect each Inquiry, and determine if TTL is expired
for inquiry in inquiries:
ttl = int(inquiry.result.get('ttl'))
if ttl <= 0:
logger.debug("Inquiry %s has a TTL of %s. Skipping." % (inquiry.id, ttl))
continue
min_since_creation = int(
(get_datetime_utc_now() - inquiry.start_timestamp).total_seconds() / 60
)
logger.debug("Inquiry %s has a TTL of %s and was started %s minute(s) ago" % (
inquiry.id, ttl, min_since_creation))
if min_since_creation > ttl:
gc_count += 1
logger.info("TTL expired for Inquiry %s. Marking as timed out." % inquiry.id)
liveaction_db = action_utils.update_liveaction_status(
status=action_constants.LIVEACTION_STATUS_TIMED_OUT,
result=inquiry.result,
liveaction_id=inquiry.liveaction.get('id'))
executions.update_execution(liveaction_db)
# Call Inquiry runner's post_run to trigger callback to workflow
action_db = get_action_by_ref(liveaction_db.action)
invoke_post_run(liveaction_db=liveaction_db, action_db=action_db)
if liveaction_db.context.get("parent"):
# Request that root workflow resumes
root_liveaction = action_service.get_root_liveaction(liveaction_db)
action_service.request_resume(
root_liveaction,
UserDB(cfg.CONF.system_user.user)
)
logger.info('Marked %s ttl-expired Inquiries as "timed out".' % (gc_count))
示例5: respond
def respond(inquiry, response, requester=None):
# Set requester to system user is not provided.
if not requester:
requester = cfg.CONF.system_user.user
# Retrieve the liveaction from the database.
liveaction_db = lv_db_access.LiveAction.get_by_id(inquiry.liveaction.get('id'))
# Resume the parent workflow first. If the action execution for the inquiry is updated first,
# it triggers handling of the action execution completion which will interact with the paused
# parent workflow. The resuming logic that is executed here will then race with the completion
# of the inquiry action execution, which will randomly result in the parent workflow stuck in
# paused state.
if liveaction_db.context.get('parent'):
LOG.debug('Resuming workflow parent(s) for inquiry "%s".' % str(inquiry.id))
# For action execution under Action Chain and Mistral workflows, request the entire
# workflow to resume. Orquesta handles resume differently and so does not require root
# to resume. Orquesta allows for specifc branches to resume while other is paused. When
# there is no other paused branches, the conductor will resume the rest of the workflow.
resume_target = (
action_service.get_parent_liveaction(liveaction_db)
if workflow_service.is_action_execution_under_workflow_context(liveaction_db)
else action_service.get_root_liveaction(liveaction_db)
)
if resume_target.status in action_constants.LIVEACTION_PAUSE_STATES:
action_service.request_resume(resume_target, requester)
# Succeed the liveaction and update result with the inquiry response.
LOG.debug('Updating response for inquiry "%s".' % str(inquiry.id))
result = copy.deepcopy(inquiry.result)
result['response'] = response
liveaction_db = action_utils.update_liveaction_status(
status=action_constants.LIVEACTION_STATUS_SUCCEEDED,
end_timestamp=date_utils.get_datetime_utc_now(),
runner_info=sys_info_utils.get_process_info(),
result=result,
liveaction_id=str(liveaction_db.id)
)
# Sync the liveaction with the corresponding action execution.
execution_service.update_execution(liveaction_db)
# Invoke inquiry post run to trigger a callback to parent workflow.
LOG.debug('Invoking post run for inquiry "%s".' % str(inquiry.id))
runner_container = container.get_runner_container()
action_db = action_utils.get_action_by_ref(liveaction_db.action)
runnertype_db = action_utils.get_runnertype_by_name(action_db.runner_type['name'])
runner = runner_container._get_runner(runnertype_db, action_db, liveaction_db)
runner.post_run(status=action_constants.LIVEACTION_STATUS_SUCCEEDED, result=result)
return liveaction_db
示例6: process
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:
updated_liveaction = action_utils.update_liveaction_status(
status=liveaction.status,
result={'message': 'Action execution canceled by user.'},
liveaction_id=liveaction.id)
executions.update_execution(updated_liveaction)
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)
return self._run_action(liveaction_db)
示例7: _update_action_results
def _update_action_results(self, execution_id, status, results):
liveaction_db = LiveAction.get_by_id(execution_id)
if not liveaction_db:
raise Exception('No DB model for liveaction_id: %s' % execution_id)
liveaction_db.result = results
liveaction_db.status = status
# update liveaction, update actionexecution and then publish update.
updated_liveaction = LiveAction.add_or_update(liveaction_db, publish=False)
executions.update_execution(updated_liveaction)
LiveAction.publish_update(updated_liveaction)
return updated_liveaction
示例8: process
def process(self, liveaction):
"""Dispatches the LiveAction to appropriate action runner.
LiveAction in statuses other than "scheduled" and "canceling" are ignored. If
LiveAction is already canceled and result is empty, the LiveAction
is updated with a generic exception message.
:param liveaction: 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:
updated_liveaction = action_utils.update_liveaction_status(
status=liveaction.status,
result={"message": "Action execution canceled by user."},
liveaction_id=liveaction.id,
)
executions.update_execution(updated_liveaction)
return
if liveaction.status not in [
action_constants.LIVEACTION_STATUS_SCHEDULED,
action_constants.LIVEACTION_STATUS_CANCELING,
]:
LOG.info(
'%s is not dispatching %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
return (
self._run_action(liveaction_db)
if liveaction.status == action_constants.LIVEACTION_STATUS_SCHEDULED
else self._cancel_action(liveaction_db)
)
示例9: process
def process(self, liveaction):
"""Dispatches the LiveAction to appropriate action runner.
LiveAction in statuses other than "scheduled" and "canceling" are ignored. If
LiveAction is already canceled and result is empty, the LiveAction
is updated with a generic exception message.
:param liveaction: 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:
updated_liveaction = action_utils.update_liveaction_status(
status=liveaction.status,
result={'message': 'Action execution canceled by user.'},
liveaction_id=liveaction.id)
executions.update_execution(updated_liveaction)
return
if liveaction.status not in ACTIONRUNNER_DISPATCHABLE_STATES:
LOG.info('%s is not dispatching %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
if liveaction.status != liveaction_db.status:
LOG.warning(
'The status of liveaction %s has changed from %s to %s '
'while in the queue waiting for processing.',
liveaction.id,
liveaction.status,
liveaction_db.status
)
dispatchers = {
action_constants.LIVEACTION_STATUS_SCHEDULED: self._run_action,
action_constants.LIVEACTION_STATUS_CANCELING: self._cancel_action,
action_constants.LIVEACTION_STATUS_PAUSING: self._pause_action,
action_constants.LIVEACTION_STATUS_RESUMING: self._resume_action
}
return dispatchers[liveaction.status](liveaction)
示例10: test_execution_update
def test_execution_update(self):
liveaction = self.MODELS['liveactions']['liveaction1.yaml']
executions_util.create_execution_object(liveaction)
liveaction.status = 'running'
pre_update_timestamp = date_utils.get_datetime_utc_now()
executions_util.update_execution(liveaction)
post_update_timestamp = date_utils.get_datetime_utc_now()
execution = self._get_action_execution(liveaction__id=str(liveaction.id),
raise_exception=True)
self.assertEquals(len(execution.log), 2)
self.assertEquals(execution.log[1]['status'], liveaction.status)
self.assertGreater(execution.log[1]['timestamp'], pre_update_timestamp)
self.assertLess(execution.log[1]['timestamp'], post_update_timestamp)
示例11: update_status
def update_status(liveaction, new_status, publish=True):
if liveaction.status == new_status:
return liveaction
old_status = liveaction.status
liveaction = action_utils.update_liveaction_status(
status=new_status, liveaction_id=liveaction.id, publish=False)
action_execution = executions.update_execution(liveaction)
msg = ('The status of action execution is changed from %s to %s. '
'<LiveAction.id=%s, ActionExecution.id=%s>' % (old_status,
new_status, liveaction.id, action_execution.id))
extra = {
'action_execution_db': action_execution,
'liveaction_db': liveaction
}
LOG.audit(msg, extra=extra)
LOG.info(msg)
if publish:
LiveAction.publish_status(liveaction)
return liveaction
示例12: update_status
def update_status(liveaction, new_status, result=None, publish=True):
if liveaction.status == new_status:
return liveaction
old_status = liveaction.status
liveaction = action_utils.update_liveaction_status(
status=new_status, result=result, liveaction_id=liveaction.id, publish=False
)
action_execution = executions.update_execution(liveaction)
msg = "The status of action execution is changed from %s to %s. " "<LiveAction.id=%s, ActionExecution.id=%s>" % (
old_status,
new_status,
liveaction.id,
action_execution.id,
)
extra = {"action_execution_db": action_execution, "liveaction_db": liveaction}
LOG.audit(msg, extra=extra)
LOG.info(msg)
if publish:
LiveAction.publish_status(liveaction)
return liveaction
示例13: _run_action
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 and not liveaction_db.action_is_workflow:
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
示例14: _run_action
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:
self._running_liveactions.remove(liveaction_db.id)
return result
示例15: _update_action_results
def _update_action_results(self, execution_id, status, results):
liveaction_db = LiveAction.get_by_id(execution_id)
if not liveaction_db:
raise Exception('No DB model for liveaction_id: %s' % execution_id)
liveaction_db.result = results
liveaction_db.status = status
done = status in DONE_STATES
if done and not liveaction_db.end_timestamp:
# Action has completed, record end_timestamp
liveaction_db.end_timestamp = date_utils.get_datetime_utc_now()
# update liveaction, update actionexecution and then publish update.
updated_liveaction = LiveAction.add_or_update(liveaction_db, publish=False)
executions.update_execution(updated_liveaction)
LiveAction.publish_update(updated_liveaction)
return updated_liveaction