-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[12.x] ScheduledTaskFailed
not dispatched on scheduled forground task fails
#55624
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
Open
achrafAa
wants to merge
7
commits into
laravel:12.x
Choose a base branch
from
achrafAa:12.x_ScheduledTaskFailed_not_dispatched_on_scheduled_forground_task_fails
base: 12.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+218
−0
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
12683ae
Add exception handling for failed scheduled commands
achrafAa 4ed5cdf
Add test for command without explicit return
achrafAa cdc08e3
add test for no explicit return
achrafAa 59eb6c4
Revert "add test for no explicit return"
achrafAa eb7be29
Add tests for background command execution with and without explicit …
achrafAa 6f78ed8
move the check after set-ting the eventsRan to true
achrafAa 1a556d2
redo pint removing doc blocks for handle
achrafAa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
216 changes: 216 additions & 0 deletions
216
tests/Integration/Console/Scheduling/ScheduleRunCommandTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Console\Scheduling; | ||
|
||
use Illuminate\Console\Events\ScheduledTaskFailed; | ||
use Illuminate\Console\Events\ScheduledTaskFinished; | ||
use Illuminate\Console\Events\ScheduledTaskStarting; | ||
use Illuminate\Console\Scheduling\Schedule; | ||
use Illuminate\Contracts\Container\BindingResolutionException; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Facades\Event; | ||
use Orchestra\Testbench\TestCase; | ||
|
||
class ScheduleRunCommandTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
Carbon::setTestNow(Carbon::now()); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
Carbon::setTestNow(); | ||
} | ||
|
||
/** | ||
* @throws BindingResolutionException | ||
*/ | ||
public function test_failing_command_in_foreground_triggers_event() | ||
{ | ||
Event::fake([ | ||
ScheduledTaskStarting::class, | ||
ScheduledTaskFinished::class, | ||
ScheduledTaskFailed::class, | ||
]); | ||
|
||
// Create a schedule and add the command | ||
$schedule = $this->app->make(Schedule::class); | ||
$task = $schedule->exec('exit 1') | ||
->everyMinute() | ||
->withoutOverlapping(); | ||
|
||
// Make sure it will run regardless of schedule | ||
$task->when(function () { | ||
return true; | ||
}); | ||
|
||
// Execute the scheduler | ||
$this->artisan('schedule:run'); | ||
|
||
// Verify the event sequence | ||
Event::assertDispatched(ScheduledTaskStarting::class); | ||
Event::assertDispatched(ScheduledTaskFinished::class); | ||
Event::assertDispatched(ScheduledTaskFailed::class, function ($event) use ($task) { | ||
return $event->task === $task && | ||
$event->exception->getMessage() === 'Scheduled command [exit 1] failed with exit code [1].'; | ||
}); | ||
} | ||
|
||
/** | ||
* @throws BindingResolutionException | ||
*/ | ||
public function test_failing_command_in_background_does_not_trigger_event() | ||
{ | ||
Event::fake([ | ||
ScheduledTaskStarting::class, | ||
ScheduledTaskFinished::class, | ||
ScheduledTaskFailed::class, | ||
]); | ||
|
||
// Create a schedule and add the command | ||
$schedule = $this->app->make(Schedule::class); | ||
$task = $schedule->exec('exit 1') | ||
->everyMinute() | ||
->runInBackground(); | ||
|
||
// Make sure it will run regardless of schedule | ||
$task->when(function () { | ||
return true; | ||
}); | ||
|
||
// Execute the scheduler | ||
$this->artisan('schedule:run'); | ||
|
||
// Verify the event sequence | ||
Event::assertDispatched(ScheduledTaskStarting::class); | ||
Event::assertDispatched(ScheduledTaskFinished::class); | ||
Event::assertNotDispatched(ScheduledTaskFailed::class); | ||
} | ||
|
||
/** | ||
* @throws BindingResolutionException | ||
*/ | ||
public function test_successful_command_does_not_trigger_event() | ||
{ | ||
Event::fake([ | ||
ScheduledTaskStarting::class, | ||
ScheduledTaskFinished::class, | ||
ScheduledTaskFailed::class, | ||
]); | ||
|
||
// Create a schedule and add the command | ||
$schedule = $this->app->make(Schedule::class); | ||
$task = $schedule->exec('exit 0') | ||
->everyMinute() | ||
->withoutOverlapping(); | ||
|
||
// Make sure it will run regardless of schedule | ||
$task->when(function () { | ||
return true; | ||
}); | ||
|
||
// Execute the scheduler | ||
$this->artisan('schedule:run'); | ||
|
||
// Verify the event sequence | ||
Event::assertDispatched(ScheduledTaskStarting::class); | ||
Event::assertDispatched(ScheduledTaskFinished::class); | ||
Event::assertNotDispatched(ScheduledTaskFailed::class); | ||
} | ||
|
||
/** | ||
* @throws BindingResolutionException | ||
*/ | ||
public function test_command_with_no_explicit_return_does_not_trigger_event() | ||
{ | ||
Event::fake([ | ||
ScheduledTaskStarting::class, | ||
ScheduledTaskFinished::class, | ||
ScheduledTaskFailed::class, | ||
]); | ||
|
||
// Create a schedule and add the command that just performs an action without explicit exit | ||
$schedule = $this->app->make(Schedule::class); | ||
$task = $schedule->exec('true') | ||
->everyMinute() | ||
->withoutOverlapping(); | ||
|
||
// Make sure it will run regardless of schedule | ||
$task->when(function () { | ||
return true; | ||
}); | ||
|
||
// Execute the scheduler | ||
$this->artisan('schedule:run'); | ||
|
||
// Verify the event sequence | ||
Event::assertDispatched(ScheduledTaskStarting::class); | ||
Event::assertDispatched(ScheduledTaskFinished::class); | ||
Event::assertNotDispatched(ScheduledTaskFailed::class); | ||
} | ||
|
||
/** | ||
* @throws BindingResolutionException | ||
*/ | ||
public function test_successful_command_in_background_does_not_trigger_event() | ||
{ | ||
Event::fake([ | ||
ScheduledTaskStarting::class, | ||
ScheduledTaskFinished::class, | ||
ScheduledTaskFailed::class, | ||
]); | ||
|
||
// Create a schedule and add the command | ||
$schedule = $this->app->make(Schedule::class); | ||
$task = $schedule->exec('exit 0') | ||
->everyMinute() | ||
->runInBackground(); | ||
|
||
// Make sure it will run regardless of schedule | ||
$task->when(function () { | ||
return true; | ||
}); | ||
|
||
// Execute the scheduler | ||
$this->artisan('schedule:run'); | ||
|
||
// Verify the event sequence | ||
Event::assertDispatched(ScheduledTaskStarting::class); | ||
Event::assertDispatched(ScheduledTaskFinished::class); | ||
Event::assertNotDispatched(ScheduledTaskFailed::class); | ||
} | ||
|
||
/** | ||
* @throws BindingResolutionException | ||
*/ | ||
public function test_command_with_no_explicit_return_in_background_does_not_trigger_event() | ||
{ | ||
Event::fake([ | ||
ScheduledTaskStarting::class, | ||
ScheduledTaskFinished::class, | ||
ScheduledTaskFailed::class, | ||
]); | ||
|
||
// Create a schedule and add the command that just performs an action without explicit exit | ||
$schedule = $this->app->make(Schedule::class); | ||
$task = $schedule->exec('true') | ||
->everyMinute() | ||
->runInBackground(); | ||
|
||
// Make sure it will run regardless of schedule | ||
$task->when(function () { | ||
return true; | ||
}); | ||
|
||
// Execute the scheduler | ||
$this->artisan('schedule:run'); | ||
|
||
// Verify the event sequence | ||
Event::assertDispatched(ScheduledTaskStarting::class); | ||
Event::assertDispatched(ScheduledTaskFinished::class); | ||
Event::assertNotDispatched(ScheduledTaskFailed::class); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.