Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

timroush's avatar

Task Scheduling Questions

Hi folks, I've been building a task scheduling suite with 5.4 lately and I'm wondering about some features I'd like to integrate.

1- I'd like the ability to dynamically schedule tasks, as opposed to defining their interval in Kernel.php, probably from a DB somewhere. Has anyone tried this/got any tips for good practices?

2- I want to ensure that for certain tasks, only one possible instance of that task is running, whether that's from the scheduler or the command line. I know I can use withoutOverlapping, I just haven't tested how robust it is.

3- I'd like the ability to list which tasks are currently running and, if necessary, terminate a running task. Aside from killing the actual process (since I'd like to build a GUI for this), has anyone tried anything like this?

I'm curious to hear people's experience with this kind of a project, and if anyone has any tips. Thanks everyone!

0 likes
4 replies
timroush's avatar

Just giving this a bump to see if anyone has anything to weigh in with regarding Laravel's task scheduler. I know it's not entirely in its infancy, but a lack of documentation regarding some of the more robust features like what I've described above make me wonder if I'm just treading into somewhat uncharted waters. Then again, I'm also a bit new to this stuff so maybe there's just something I'm missing.

afrayedknot's avatar

1- I'd like the ability to dynamically schedule tasks, as opposed to defining their interval in Kernel.php, probably from a DB somewhere. Has anyone tried this/got any tips for good practices?

If you wanted to - you could have a database list of cron schedules. Then you would have a command like mycustomscheduler:run which runs every minute.

That custom scheduler would check the database, see if any cron schedules match the current time (using https://github.com/mtdowling/cron-expression).

If a cron matches - you can fire it off with its own artisan command:

Artisan::call($result->command)

2- I want to ensure that for certain tasks, only one possible instance of that task is running, whether that's from the scheduler or the command line. I know I can use withoutOverlapping, I just haven't tested how robust it is.

->withoutOverlapping() definitely works. It mutexs a hash in your cache, which is removed when the command finishes.

The only issue I've experienced is if your command/server crashes, the hash remains in your cache for sometime before it goes away, preventing the cron from running again. But that is fairly rare.

3- I'd like the ability to list which tasks are currently running and, if necessary, terminate a running task. Aside from killing the actual process (since I'd like to build a GUI for this), has anyone tried anything like this?

It probably depends on the task. Most jobs I run take <30seconds - so by the time you realised you want to kill it - it's already done.

If you know you have a very long job, you could build a "poll" into it, where it periodically checks if it should die (perhaps with a cache hash) - and let the job handle it itself.

1 like
timroush's avatar

Thanks @laurence there are some good ideas here. I had a feeling some of these features would need the kind of custom integration you'd mentioned, especially since the task scheduler I'm looking to build is going to be fairly robust, managing possibly hundreds of tasks with many running simultaneously, and some running for a long duration. I also might have it run as a distributed application across a few servers, which would mean having a centralized way for a job to know it is being run. And though the Laravel scheduler is rad, it also seems somewhat new.

afrayedknot's avatar

Depending what the jobs are - you could push some/all of them to be queued jobs - allowing for easy distributed processes using queue servers.

The alternative is use your database, and add a column like isRunning. Using pessimistic locking - you could run the scheduler on a few servers, and each should only grab the one job. When the job is finished, just set isRunning to false so other servers know they could run it if needed.

Please or to participate in this conversation.