I've built an app that uses cron jobs through Cloudways' built-in cron manager. While it works, it's a bit clunky and changes are tough since I have to log into the panel to edit jobs manually. I'm thinking about switching to a heartbeat pattern: having one cron job running every minute that triggers a script. This script would check a database or configuration for tasks, log activities, and execute any tasks that are due. This would allow me a more user-friendly way to manage the schedule through a GUI in my app. Is this heartbeat-style cron setup considered bad practice, or is there a more effective method for managing scheduled jobs in a flexible way?
6 Answers
This method is similar to how the Laravel scheduler operates. It's not necessarily bad practice, but there are downsides like issues with overlapping tasks and failure management. From my experience, it works best when the scheduler sends jobs to a queue like Redis or SQS.
The real question is why you want to do it this way when cron jobs already exist?
You could just set up the cron jobs directly on the server instead of using a hosted control panel. It might make things cleaner and easier to manage without the extra layer.
Using a heartbeat approach can work, but keep an eye out for race conditions or missed jobs during database downtimes. You might want to look into tools like Hangfire or Sidekiq for managing and scaling your scheduled tasks effectively.
Many frameworks already offer these types of facilities. In some cases, you can launch checks from a URL, allowing you to utilize whatever scheduler suits your needs best.
Yes, it's often considered bad practice because it can lead to unnecessary operations when nothing needs to run, wasting computing resources. Of course, whether that's a concern for you depends on your specific situation.
Related Questions
Can't Load PhpMyadmin On After Server Update
Redirect www to non-www in Apache Conf
How To Check If Your SSL Cert Is SHA 1
Windows TrackPad Gestures