stackstorm 27. 源碼分析之----stackstorm的notifier服務分析

目標:
弄清楚st2notifier服務原理

1 主入口
st2/st2actions/st2actions/notifier/scheduler.py

def get_rescheduler():
    timer = BlockingScheduler()

    time_spec = {
        'seconds': cfg.CONF.scheduler.rescheduling_interval,
        'timezone': aps_utils.astimezone('UTC')
    }

    timer.add_job(recover_delayed_executions,
                  trigger=IntervalTrigger(**time_spec),
                  max_instances=1,
                  misfire_grace_time=60,
                  next_run_time=date_utils.get_datetime_utc_now(),
                  replace_existing=True)

    return timer

def recover_delayed_executions():
    coordinator = coordination.get_coordinator()
    dt_now = date_utils.get_datetime_utc_now()
    dt_delta = datetime.timedelta(seconds=cfg.CONF.scheduler.delayed_execution_recovery)
    dt_timeout = dt_now - dt_delta

    with coordinator.get_lock('st2-rescheduling-delayed-executions'):
        liveactions = LiveAction.query(status=action_constants.LIVEACTION_STATUS_DELAYED,
                                       start_timestamp__lte=dt_timeout,
                                       order_by=['start_timestamp'])

        if not liveactions:
            return

        LOG.info('There are %d liveactions that have been delayed for longer than %d seconds.',
                 len(liveactions), cfg.CONF.scheduler.delayed_execution_recovery)

        # Update status to requested and publish status for each liveactions.
        rescheduled = 0
        for instance in liveactions:
            try:
                action_service.update_status(instance,
                                             action_constants.LIVEACTION_STATUS_REQUESTED,
                                             publish=True)
                rescheduled += 1
            except:
                LOG.exception('Unable to reschedule liveaction. <LiveAction.id=%s>', instance.id)

        LOG.info('Rescheduled %d out of %d delayed liveactions.', len(liveactions), rescheduled)

其中get_scheduler方法被如下代碼調用
st2/st2actions/st2actions/cmd/st2notifier.py

def _run_worker():
    LOG.info('(PID=%s) Actions notifier started.', os.getpid())
    actions_notifier = notifier.get_notifier()
    actions_rescheduler = None
    try:
        if cfg.CONF.scheduler.enable:
            actions_rescheduler = scheduler.get_rescheduler()
            eventlet.spawn(actions_rescheduler.start)
            LOG.info(SCHEDULER_ENABLED_LOG_LINE)
        else:
            LOG.info(SCHEDULER_DISABLED_LOG_LINE)
        actions_notifier.start(wait=True)
    except (KeyboardInterrupt, SystemExit):
        LOG.info('(PID=%s) Actions notifier stopped.', os.getpid())
        if actions_rescheduler:
            actions_rescheduler.shutdown()
        actions_notifier.shutdown()
    return 0


分析:
st2resultstracker通過調用Mstral API端點,跟蹤長時間運行的工作流的執行情況。
st2notifier在ActionExecution執行完成時TriggerInstances產生st2.core.actiontrigger和st2.core.notifytrigger。另外一個目的是充當action的備份調度器,處理哪些可能未列入計劃的action。

 

/opt/stackstorm/st2/bin/st2notifier --config-file /etc/st2/st2.conf


參考:
stackstorm 2.6代碼

https://blog.csdn.net/qingyuanluofeng/article/details/86653975

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章