APScheduler重启后作业未运行

时间:2016-02-29 19:13:11

标签: python mongodb threadpool jobs apscheduler

我在尝试APS的MongoJobStore example。示例代码是:

import logging
import os
import sys
from datetime import datetime, timedelta

from apscheduler.schedulers.blocking import BlockingScheduler

logging.basicConfig()


def alarm(time):
    print('Alarm! This alarm was scheduled at %s.' % time)


if __name__ == '__main__':
    scheduler = BlockingScheduler()
    scheduler.add_jobstore('mongodb', collection='example_jobs', host='192.168.0.108', port=27017)

    if len(sys.argv) > 1 and sys.argv[1] == '--clear':
        scheduler.remove_all_jobs()

    alarm_time = datetime.now() + timedelta(seconds=10)
    # scheduler.add_job(alarm, 'date', run_date=alarm_time, args=[datetime.now()], id="la")
    print('To clear the alarms, run this example with the --clear argument.')
    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

    scheduler.start()

首先测试我运行代码并在控制台上打印alarm方法的输出。然后为了测试mongo的使用,我做了以下几点:

  1. 运行示例并在执行作业之前将其停止。
  2. 这在db

    中创建了以下条目
    > db.example_jobs.find().pretty()
    {
        "_id" : "la",
        "next_run_time" : 1456771825.792437,
        "job_state" : BinData(0,"gAJ9cQEoVQRhcmdzcQJjZGF0ZXRpbWUKZGF0ZXRpbWUKcQNVCgfgAwEAFA8MF4SFUnEEhXEFVQhleGVjdXRvcnEGVQdkZWZhdWx0cQdVDW1heF9pbnN0YW5jZXNxCEsBVQRmdW5jcQlVDl9fbWFpbl9fOmFsYXJtcQpVAmlkcQtVAmxhcQxVDW5leHRfcnVuX3RpbWVxDWgDVQoH4AMBABQZDBd1Y3B5dHoKX3AKcQ4oVQxBc2lhL0tvbGthdGFxD01YTUsAVQNJU1RxEHRScRGGUnESVQRuYW1lcRNVBWFsYXJtcRRVEm1pc2ZpcmVfZ3JhY2VfdGltZXEVSwFVB3RyaWdnZXJxFmNhcHNjaGVkdWxlci50cmlnZ2Vycy5kYXRlCkRhdGVUcmlnZ2VyCnEXKYFxGH1xGX1xGlUIcnVuX2RhdGVxG2gSc4ZiVQhjb2FsZXNjZXEciFUHdmVyc2lvbnEdSwFVBmt3YXJnc3EefXEfdS4=")
    }
    
  3. 然后我评论了添加作业的行:# scheduler.add_job(alarm, 'date', run_date=alarm_time, args=[datetime.now()], id="la")

  4. 然后我再次运行脚本并得到以下输出:

    To clear the alarms, run this example with the --clear argument.
    Press Ctrl+C to exit
    WARNING:apscheduler.executors.default:Run time of job "alarm (trigger: date[2016-03-01 00:35:27 IST], next run at: 2016-03-01 00:35:27 IST)" was missed by 0:00:19.290825
    
  5. 不打印alarm函数的输出,并从数据库中删除记录。

  6. 我尝试调试第二次运行,代码正在从数据库中读取作业,然后将其提交给threadpool。但是没有打印输出。

    为什么会发生这种情况?

1 个答案:

答案 0 :(得分:2)

问题是misfire_grace_timedoc)。我没有设置它。实际上,即使在示例中也没有设置它。

工作代码:

import logging
import os
import sys
from datetime import datetime, timedelta

from apscheduler.schedulers.blocking import BlockingScheduler

logging.basicConfig()


def alarm(time):
    print('Alarm! This alarm was scheduled at %s.' % time)


if __name__ == '__main__':
    scheduler = BlockingScheduler()
    scheduler.add_jobstore('mongodb', collection='example_jobs', host='192.168.0.108', port=27017)

    if len(sys.argv) > 1 and sys.argv[1] == '--clear':
        scheduler.remove_all_jobs()

    alarm_time = datetime.now() + timedelta(seconds=10)
    scheduler.add_job(alarm, 'date', run_date=alarm_time, args=[datetime.now()], id="la", misfire_grace_time=1000)
    print('To clear the alarms, run this example with the --clear argument.')
    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

    scheduler.start()

我从APS的IRC频道#apscheduler得到了这个答案。 @agronholm帮助了我。