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 publish this utility through the below command:
php artisan vendor:publish --tag=lara-util-x-scheduler
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.