{"id":1341,"date":"2016-10-05T00:00:00","date_gmt":"2016-10-05T00:00:00","guid":{"rendered":"http:\/\/www-staging.carta.com\/sg\/blog\/why-you-shouldnt-use-cron-jobs\/"},"modified":"2021-03-05T06:58:52","modified_gmt":"2021-03-05T06:58:52","slug":"why-you-shouldnt-use-cron-jobs","status":"publish","type":"post","link":"https:\/\/www-staging.carta.com\/sg\/blog\/why-you-shouldnt-use-cron-jobs\/","title":{"rendered":"Why you shouldn’t use “Cron Jobs?”"},"content":{"rendered":"\r\n

Executing a task on an interval or at a specific time is a common problem with application developers.<\/p>\r\n

Many software developers think, I know how to solve this, I’ll use cron.<\/p>\r\n

A few problems with cron:<\/h2>\r\n
    \r\n
  1. Smallest resolution is 1 minute<\/strong>—If a task needs to run every 30 seconds, you can’t do it with cron.<\/li>\r\n
  2. Error handling<\/strong>—If a job fails, what should happen? Solutions have been built to solve this single problem. Developers love adding more band-aids rather than admitting there is a better way. Deja Vu?<\/li>\r\n
  3. Logging<\/strong>—Crons don’t log, unless you tell them too. It is rare to see people log output of their cron jobs.<\/li>\r\n
  4. Working with cron pulls you out of the application<\/strong>—cron is a system level process. Not an application process. It’s challenging to give application developers access to anything at a system level. Developers shouldn’t care where their application runs\u00e2\u20ac\u00a6 A good example of this is timezones. If a system person changes the timezone of a server, the cron may run at a different time than expected. The less the app developers have to worry about what they run on, the better.<\/li>\r\n
  5. Not enough reasons? You can read more about how cron doesn’t work at this StackExchange thread<\/a>.<\/li>\r\n<\/ol>\r\n

    There is a better way.<\/p>\r\n

    Background jobs and a scheduler<\/h2>\r\n

    Developers of Web Applications are aware of a common problem of offloading a task that can hold up a web request by putting it into a background job. Python’s most famous background task worker is Celery. Celery has a built in feature called Beat, which is a scheduler.<\/p>\r\n

    Here is how it works.<\/p>\r\n

    Create a celery task. This is the same type of task you create when doing background work.<\/p>\r\n