Miserlou / Zappa

Serverless Python
https://blog.zappa.io/
MIT License
11.89k stars 1.21k forks source link

Multiple event cron expressions are ignored; only last cron expression applies #1727

Open kylegibson opened 5 years ago

kylegibson commented 5 years ago

I'm attempting to schedule a function to run according to multiple cron expressions. Only a single CloudWatch Rule is created using the last cron expression.

zappa 0.47 settings.yaml

production:
  events:
  - function: project.awesome_event_function_is_really_long_so_it_needs_to_be_hashed
    expressions:
    - cron(0 12 * * ? *)
    - cron(0 20 * * ? *)
$ zappa schedule production
Calling schedule for stage production..
Scheduling..
Scheduled 7b90c794e411-project.awesome_event_function_is_really_long_so_it_needs_to_be_hashed with expression cron(0 12 * * ? *)!
Scheduled 7b90c794e411-project.awesome_event_function_is_really_long_so_it_needs_to_be_hashed with expression cron(0 20 * * ? *)!
Scheduled foo-production-zappa-keep-warm-handler.keep_warm_callback with expression cron(0/5 * * * ? *)!

Looks correct so far, but not quite:

$ zappa status production
...
        Num. Event Rules:     2
        Event Rule Name:      7b90c794e411-project.awesome_event
        Event Rule Schedule:  cron(0 20 * * ? *)
        Event Rule State:     Enabled
        Event Rule ARN:       arn:aws:events:xxxxxxxxxxxx:rule/7b90c794e411-project.awesome_event_function_is_really_long_so_it_needs_to_be_hashed
        Event Rule Name:      foo-production-zappa-keep-warm-handler.keep_warm_callback
        Event Rule Schedule:  cron(0/5 * * * ? *)
        Event Rule State:     Enabled
        Event Rule ARN:       arn:aws:events:xxxxxxxxxxxxx:rule/foo-production-zappa-keep-warm-handler.keep_warm_callback

The CloudWatch Rules console confirms that there is only 1 rule for "awesome_event_function_is_really_long_so_it_needs_to_be_hashed"

kylegibson commented 5 years ago

I think I've identified the cause.

971 added rule name shortening using a hashing technique

1051 added a numeric index prefix to ensure unique function names

A conflict occurs when both of these code paths are hit: you have a function name causes the rule name to be > 64 characters and there are multiple cron expressions.

https://github.com/Miserlou/Zappa/blob/master/zappa/core.py#L2420 does not take into consideration the index, or the name that was generated on line 2416

kingtut commented 5 years ago

Any progress on this?

Edit: A workaround is to name your events.

{
    "name": "my-scheduled-task",
    "function": "path.to.lambda_handler",
    "expression": "cron(0 * * * ? *)"
}
edudobay commented 4 years ago

I've made some changes to make this work without external workarounds. It seems like the current behavior is a real bug — it breaks logical user expectations. I'll be shortly sending a PR with my proposed fix.