3

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?

Alex R
  • 11,364
  • 15
  • 100
  • 180

2 Answers2

7

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())
Alex R
  • 11,364
  • 15
  • 100
  • 180
jamesls
  • 5,428
  • 1
  • 28
  • 17
0

I did one mistake in my code due to scheduler was not working. It was not syntax error. It was overriding issue. I hope it will be helpful for you or other.

@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()

In this case your scheduler will not work. Please ensure that if you are giving route for same functionality for some testing purpose. Change the method name little bit because chalice override methods which are defined in app.py.