【问题标题】:How do you schedule cron jobs using APScheduler on Heroku?您如何在 Heroku 上使用 APScheduler 安排 cron 作业?
【发布时间】:2016-09-11 22:39:48
【问题描述】:

我正在尝试使用 Heroku 上的 APScheduler 和 SendGrid 来创建用于发送电子邮件的 cron 作业。

尽管 add_job 方法调用似乎正确执行,但我收到以下错误。

以下是heroku的日志

2016-09-11T22:33:37.776867+00:00 heroku[clock.1]: State changed from crashed to starting
2016-09-11T22:33:40.672563+00:00 heroku[clock.1]: Starting process with command `python clock.py`
2016-09-11T22:33:41.353373+00:00 heroku[clock.1]: State changed from starting to up
2016-09-11T22:33:43.527949+00:00 app[clock.1]: created background scheduler
2016-09-11T22:33:43.528848+00:00 app[clock.1]: started background scheduler
2016-09-11T22:33:43.572751+00:00 app[clock.1]: added cron job
2016-09-11T22:33:43.585801+00:00 app[clock.1]: Exception in thread APScheduler (most likely raised during interpreter shutdown):
2016-09-11T22:33:43.585807+00:00 app[clock.1]: Traceback (most recent call last):
2016-09-11T22:33:43.585808+00:00 app[clock.1]:   File "/app/.heroku/python/lib/python2.7/threading.py", line 801, in __bootstrap_inner
2016-09-11T22:33:43.585810+00:00 app[clock.1]:   File "/app/.heroku/python/lib/python2.7/threading.py", line 754, in run
2016-09-11T22:33:43.585827+00:00 app[clock.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/apscheduler/schedulers/blocking.py", line 29, in _main_loop
2016-09-11T22:33:43.585829+00:00 app[clock.1]:   File "/app/.heroku/python/lib/python2.7/threading.py", line 614, in wait
2016-09-11T22:33:43.585848+00:00 app[clock.1]:   File "/app/.heroku/python/lib/python2.7/threading.py", line 364, in wait
2016-09-11T22:33:43.585851+00:00 app[clock.1]: <type 'exceptions.ValueError'>: list.remove(x): x not in list
2016-09-11T22:33:43.695569+00:00 heroku[clock.1]: Process exited with status 0
2016-09-11T22:33:43.719265+00:00 heroku[clock.1]: State changed from up to crashed

我正在运行一个时钟进程,它在下面的clock.py文件中。

from apscheduler.schedulers.background import BackgroundScheduler
import sendgrid
import os
from sendgrid.helpers.mail import *

def send_email():
  try:
    sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
    print("created send grid api client")
    from_email = Email("ohta.g@husky.neu.edu")
    print("created from email")
    subject = "Weekly update"
    to_email = Email("ohta.g@husky.neu.edu")
    print("created to email")
    content = Content("text/plain", "Hello, Email!")
    print("created content")
    mail = Mail(from_email, subject, to_email, content)
    print("created mail")
    response = sg.client.mail.send.post(request_body=mail.get())
  except Exception as e:
    return e

try:
  sched = BackgroundScheduler()
  print("created background scheduler")
  sched.start()
  print("started background scheduler")
  sched.add_job(send_email, 'cron', day_of_week=6, hour=22, minute=20)
  print("added cron job")
except Exception as e:
  print e.message

这是我的 Procfile。

clock: python clock.py

这是我的 requirements.txt 文件。

APScheduler==3.1.0
sendgrid==3.4.0

谁能告诉我我做错了什么?

【问题讨论】:

    标签: python heroku cron sendgrid apscheduler


    【解决方案1】:

    您启动了一个后台调度程序,但随后允许您的主线程退出,这也退出了时钟进程。这就是BlockingScheduler 存在的全部原因。你没看过 Heroku 的APScheduler instructions吗?

    【讨论】:

    • 感谢您向我推荐这些说明。当我第一次尝试这些指令时,我的时钟进程无缘无故地以 SIGTERM 退出。我认为这意味着我做错了什么,这就是为什么我想改变调度程序的类型。现在我知道时钟进程在不执行 cron 作业时退出。
    • 你确定吗?我没有过多地使用 Heroku,但这听起来有点奇怪,至少在涉及 APScheduler 的情况下。该进程不应自行终止。
    • 使用 BlockingScheduler 真的可以防止 dyno 休眠吗?
    猜你喜欢
    • 2015-10-28
    • 2018-03-09
    • 2023-03-05
    • 2015-05-27
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    相关资源
    最近更新 更多