cron 事件的 chalice @app.schedule 语法是什么?

What is the chalice @app.schedule syntax for cron events?

我正在尝试遵循 https://chalice.readthedocs.io/en/latest/topics/events.html

中的文档

我试过了

@app.schedule('0 0 * * ? *')
def dataRefresh(event):
    print(event.to_dict())

并收到此错误:

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the PutRule operation: Parameter ScheduleExpression is not valid.

所以尝试了这个:

@app.schedule(Cron('0 0 * * ? *'))
def dataRefresh(event):
    print(event.to_dict())

并得到另一个错误:

NameError: name 'Cron' is not defined

没有任何效果...正确的语法是什么?

如果你想使用 Cron 对象,你必须从 chalice 包中导入它,然后每个值都是 Cron 对象的位置参数:

from chalice import Chalice, Cron

app = Chalice(app_name='sched')


@app.schedule(Cron(0, 0, '*', '*', '?', '*'))
def my_schedule():
    return {'hello': 'world'}

Crondocs 有更多信息。

或者使用此语法,无需额外导入即可工作:

@app.schedule('cron(0 0 * * ? *)')
def dataRefresh(event):
    print(event.to_dict())

由于调度程序不工作,我在代码中犯了一个错误。 这不是语法错误。这是压倒一切的问题。 希望对您或其他人有所帮助。

@app.schedule(Cron(50, 6, '*', '*', '?', '*'))
def reminder_mail_8AM_2DAYS_BEFORE(event):  ## testing with scheduler
    print("Inside Mail Reminder 8 AM & 2 days before")
    sendReminderMailsToUsersAt8AM()
    sendReminderMailsToUsers2DaysBefore()

@app.route('/reminderMailSend', methods=['GET'], cors=cors)
def reminder_mail_8AM_2DAYS_BEFORE():  ## Testing by get call
   print("Inside Mail Reminder 8 AM & 2 days before")
   sendReminderMailsToUsersAt8AM()
   sendReminderMailsToUsers2DaysBefore()

在这种情况下,您的调度程序将不起作用。请确保您是否出于某些测试目的为相同功能提供路由。 稍微更改方法名称,因为 chalice 覆盖了 app.py.

中定义的方法