【问题标题】:What is the chalice @app.schedule syntax for cron events?cron 事件的 chalice @app.schedule 语法是什么?
【发布时间】:2020-03-04 06:38:35
【问题描述】:

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

我试过了

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

得到了这个错误:

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

于是尝试了这个:

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

并得到另一个错误:

NameError:名称“Cron”未定义

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

【问题讨论】:

    标签: python amazon-web-services chalice


    【解决方案1】:

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

    from chalice import Chalice, Cron
    
    app = Chalice(app_name='sched')
    
    
    @app.schedule(Cron(0, 0, '*', '*', '?', '*'))
    def my_schedule():
        return {'hello': 'world'}
    

    这里是docsCron 有更多信息。

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

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

    【讨论】:

    • 是的,这取决于偏好。如果你使用Cron 对象,你会得到一个类型化的对象,它会在你的编辑器/IDE 中为你提供自动完成和类型检查等功能。
    • ? 上使用* 也让我来到这里??‍♀️
    【解决方案2】:

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

    @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 中定义的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-27
      • 2013-12-22
      • 2011-08-16
      相关资源
      最近更新 更多