NicolasLM / spinach

Modern Redis task queue for Python 3
https://spinach.readthedocs.io
BSD 2-Clause "Simplified" License
63 stars 4 forks source link

Periodic task definition is not idempotent and can cause multiple jobs to run #2

Closed juledwar closed 5 years ago

juledwar commented 5 years ago

I'm using Flask and Spinach with some periodic tasks. When starting the local debug version of Flask, it always restarts to start using the reloader, which causes the tasks file to get re-imported. This results in any periodic jobs seemingly defined twice, and indeed they will run twice when the timer fires.

This behaviour disappears when app.run(use_reloader=False) is used and the job correctly only fires once.

It looks like that the double import is causing the same periodic task to get defined again, and there is no check to see if it was already defined. The non-periodic tasks don't seem to exhibit any problematic behaviour from the double import, however.

NicolasLM commented 5 years ago

Could you tell me which broker you use during development? It seems that only the MemoryBroker is affected by this behavior.

juledwar commented 5 years ago

Yes, it's memorybroker. I can test with Redis to make sure, if you didn't already?

Thanks.

NicolasLM commented 5 years ago

The Flask reloader restarts the interpreter whenever it detects a change in the code, as the memory broker keeps everything in memory, when the interpreter is restarted all state is lost. What you're experiencing is actually the expected behavior.

It should be possible to modify the memory broker so that it plays nicely with the Flask reloader but I'm not sure it makes sense. It would require to reinvent much of what the Redis broker already is and Redis itself is so easy to run locally that it doesn't look worth it.

juledwar commented 5 years ago

That's fair, thanks for replying. Maybe add something to the docs to describe this behaviour? I'll just keep my reloader turned off for now, until I switch my dev environment to use Redis.

Thanks!