Skip to content

[6.x] Add events to signal when scheduled task runs #29888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Illuminate/Console/Events/ScheduledTaskFinished.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Illuminate\Console\Events;

use Illuminate\Console\Scheduling\Event;

class ScheduledTaskFinished
{
/**
* The scheduled event that ran.
*
* @var \Illuminate\Console\Scheduling\Event
*/
public $task;

/**
* The runtime of the scheduled event.
*
* @var float
*/
public $runtime;

/**
* Create a new event instance.
*
* @param \Illuminate\Console\Scheduling\Event $task
* @param float $runtime
* @return void
*/
public function __construct(Event $task, $runtime)
{
$this->task = $task;
$this->runtime = $runtime;
}
}
26 changes: 26 additions & 0 deletions src/Illuminate/Console/Events/ScheduledTaskStarting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Illuminate\Console\Events;

use Illuminate\Console\Scheduling\Event;

class ScheduledTaskStarting
{
/**
* The scheduled event being run.
*
* @var \Illuminate\Console\Scheduling\Event
*/
public $task;

/**
* Create a new event instance.
*
* @param \Illuminate\Console\Scheduling\Event $task
* @return void
*/
public function __construct(Event $task)
{
$this->task = $task;
}
}
23 changes: 22 additions & 1 deletion src/Illuminate/Console/Scheduling/ScheduleRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Date;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Console\Events\ScheduledTaskFinished;
use Illuminate\Console\Events\ScheduledTaskStarting;

class ScheduleRunCommand extends Command
{
Expand Down Expand Up @@ -42,6 +45,13 @@ class ScheduleRunCommand extends Command
*/
protected $eventsRan = false;

/**
* The event dispatcher.
*
* @var \Illuminate\Contracts\Notifications\Dispatcher
*/
protected $dispatcher;

/**
* Create a new command instance.
*
Expand All @@ -58,11 +68,13 @@ public function __construct()
* Execute the console command.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
* @return void
*/
public function handle(Schedule $schedule)
public function handle(Schedule $schedule, Dispatcher $dispatcher)
{
$this->schedule = $schedule;
$this->dispatcher = $dispatcher;

foreach ($this->schedule->dueEvents($this->laravel) as $event) {
if (! $event->filtersPass($this->laravel)) {
Expand Down Expand Up @@ -108,8 +120,17 @@ protected function runEvent($event)
{
$this->line('<info>Running scheduled command:</info> '.$event->getSummaryForDisplay());

$this->dispatcher->dispatch(new ScheduledTaskStarting($event));

$start = microtime(true);

$event->run($this->laravel);

$this->dispatcher->dispatch(new ScheduledTaskFinished(
$event,
round(microtime(true) - $start, 2)
));

$this->eventsRan = true;
}
}