pythonamazon-web-serviceschalice

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


I am trying to follow the documentation from https://chalice.readthedocs.io/en/latest/topics/events.html

I tried this

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

and got this error:

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

and so tried this:

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

and got this other error:

NameError: name 'Cron' is not defined

Nothing works... what's the correct syntax?


Solution

  • If you want to use the Cron object you have to import it from the chalice package, and then each value is a positional parameter to the Cron object:

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

    Here's the docs for Cron that has more info.

    Or alternatively use this syntax, which works without the extra import:

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