首页 > 解决方案 > cron 事件的 chalice @app.schedule 语法是什么?

问题描述

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

我试过这个

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

并得到这个错误:

botocore.exceptions.ClientError:调用 PutRule 操作时发生错误 (ValidationException):参数 ScheduleExpression 无效。

所以尝试了这个:

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

并得到了另一个错误:

NameError:未定义名称“Cron”

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

标签: pythonamazon-web-serviceschalice

解决方案


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

from chalice import Chalice, Cron

app = Chalice(app_name='sched')


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

这是包含更多信息的文档。Cron

或者使用这种语法,它不需要额外的导入:

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

推荐阅读