The SchedulerUtil in the LaraUtilX package provides functionality for managing and monitoring scheduled tasks.
getScheduleSummary(): array: Retrieves a summary of the scheduled tasks.hasOverdueTasks(): bool: Checks if any scheduled tasks are overdue.Get Schedule Summary:
To retrieve a summary of the scheduled tasks, use the getScheduleSummary method:
use LaraUtilX\Utilities\SchedulerUtil;
class MyController extends Controller
{
protected $schedulerUtil;
public function __construct(SchedulerUtil $schedulerUtil)
{
$this->schedulerUtil = $schedulerUtil;
}
public function getScheduleSummary()
{
$summary = $this->schedulerUtil->getScheduleSummary();
// Use the retrieved schedule summary
}
}
Check Overdue Tasks:
To check if any scheduled tasks are overdue, use the hasOverdueTasks method:
use LaraUtilX\Utilities\SchedulerUtil;
class MyController extends Controller
{
protected $schedulerUtil;
public function __construct(SchedulerUtil $schedulerUtil)
{
$this->schedulerUtil = $schedulerUtil;
}
public function checkOverdueTasks()
{
$hasOverdueTasks = $this->schedulerUtil->hasOverdueTasks();
// Use the result to handle overdue tasks
}
}
The utility provides methods for retrieving a summary of scheduled tasks and checking if any tasks are overdue.
Success Result: A summary of scheduled tasks can be retrieved, including details such as command, expression, next run date, and status. Overdue tasks can also be detected using the provided method.
Error Result: If an error occurs during retrieval or detection of scheduled tasks, appropriate error handling mechanisms should be implemented based on application requirements.
You can no longer publish this class, and you do not need to. A published copy kept its LaraUtilX namespace while landing in app/, where Composer's PSR-4 mapping expects App\, so the copy was never autoloaded and every call still resolved to the package.
Extend or wrap it instead:
namespace App\Support;
use LaraUtilX\Utilities\SchedulerUtil;
class AppScheduler extends SchedulerUtil
{
// override what you need
}
Then bind your subclass in a service provider if you want it resolved in place of the package's:
$this->app->bind(\LaraUtilX\Utilities\SchedulerUtil::class, \App\Support\AppScheduler::class);
This utility enhances the management and monitoring of scheduled tasks in Laravel applications, providing insights into task execution and status.
SchedulerUtil threw on any application that actually had a scheduled task. It called getNextRunDate() and isRunning(), neither of which exists on Laravel's Event class, and it logged every event through print_r on each call, which exhausted memory once real events were registered.
It now uses Laravel's own isDue() and inspects the overlapping mutex, and it no longer writes to the log:
$scheduler = new SchedulerUtil();
$summary = $scheduler->getScheduleSummary();
// [['command' => ..., 'expression' => '* * * * *', 'next_run' => ..., 'is_due' => true, 'is_running' => false, 'output' => ...]]
$scheduler->hasOverdueTasks(); // true when something is due and not already running
isDue() and isRunning() are public, so you can ask about a single event:
$events = app(Illuminate\Console\Scheduling\Schedule::class)->events();
$scheduler->isDue($events[0]);
$scheduler->isRunning($events[0]);
Only events declared with withoutOverlapping() hold a mutex; anything else reports false rather than throwing.
hasOverdueTasks() could also never return true in earlier releases, because it compared nextRunDate(), which is always in the future, against the current time.
SchedulerUtil reads the Schedule bound in the container, and that is only populated once the console kernel has booted. Laravel registers schedules through Artisan::starting() for withSchedule(), and through afterResolving(ConsoleKernel::class) for routes/console.php. Neither fires during a web request.
Calling these methods from an HTTP route therefore returns an empty schedule rather than an error:
HTTP request -> getScheduleSummary() === []
Artisan command -> getScheduleSummary() === [ ...your events... ]
This applies to every supported Laravel version, not just 11 and later. Use SchedulerUtil from Artisan commands, or from jobs the scheduler itself dispatches. If you need schedule information in a web dashboard, collect it in a scheduled command and persist it, rather than reading the schedule during the request.
hasDueTasks() reports whether any task matches the current minute and is not already running. It is not overdue detection.
$scheduler->hasDueTasks(); // true whenever an everyMinute() task exists
Laravel's Event::isDue() evaluates the cron expression against now, so a task scheduled everyMinute() makes this true almost constantly. That is working as intended; it simply answers a different question than the name hasOverdueTasks() implied.
Genuine overdue detection would require recording when each task last ran, which the scheduler does not keep. If you need it, persist last-run timestamps from your own task callbacks and compare against them.
Renamed in v1.5.5.
hasOverdueTasks()still works and delegates tohasDueTasks(), but it is deprecated and will be removed in a future major release.