Tivix / django-cron

Write cron business logic as a Python class and let this app do the rest! It enables Django projects to schedule cron tasks, tracks their success / failures, manages contention (via a cache) etc. Basically takes care of all the boring work for you :-)
www.tivix.com
MIT License
900 stars 193 forks source link

Cache Using Unique Code as Key #141

Closed msopko81 closed 2 years ago

msopko81 commented 6 years ago

When setting the lock for the cron job, the cron class name is used instead of the code. The code should be a unique code as should be used instead of the class name. False positive locking errors can occur if 2 crons have the same cron class name, but are different. For example, app1.crons.ProcessInvoice and app2.crons.ProcessInvoice. If these crons are scheduled at the same time, then only 1 will run and the other will say that a lock was found since the lock key will be ProcessInvoice.

gregschmit commented 5 years ago

I guess just using the code rather than the class name should fix this, yes?

gregschmit commented 5 years ago

So, since the code is a user-provided string and users may have production code running that puts characters in that string that cannot exist in a filename, I vote to combine the __module__ property of the CronJob class with the __name__ property.

Old (cron_class.__name__): CronJob

New ('.'.join(cron_class.__module__, cron_class.__name__)): my_app_name.cron.CronJob

This could be implemented in the base class to avoid the same issue with the file-based locking backend.

If someone can get back to me and tell me if this makes sense I'll submit a PR.