-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract duplicate code which handles job failing.
- Loading branch information
1 parent
534889e
commit 55afe12
Showing
4 changed files
with
65 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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,48 @@ | ||
<?php | ||
|
||
namespace Illuminate\Queue; | ||
|
||
use Illuminate\Container\Container; | ||
use Illuminate\Queue\Events\JobFailed; | ||
use Illuminate\Contracts\Events\Dispatcher; | ||
|
||
class FailingJob | ||
{ | ||
/** | ||
* Delete the given job, call the "failed" method, and raise the failed job event. | ||
* | ||
* @param string $connectionName | ||
* @param \Illuminate\Queue\Jobs\Job $job | ||
* @param \Exception $e | ||
* @return void | ||
*/ | ||
public static function handle($connectionName, $job, $e = null) | ||
{ | ||
if ($job->isDeleted()) { | ||
return; | ||
} | ||
|
||
try { | ||
// If the job has failed, we will delete it, call the "failed" method and then call | ||
// an event indicating the job has failed so it can be logged if needed. This is | ||
// to allow every developer to better keep monitor of their failed queue jobs. | ||
$job->delete(); | ||
|
||
$job->failed($e); | ||
} finally { | ||
static::events()->fire(new JobFailed( | ||
$connectionName, $job, $e ?: new ManuallyFailedException | ||
)); | ||
} | ||
} | ||
|
||
/** | ||
* Get the event dispatcher instance. | ||
* | ||
* @return \Illuminate\Contracts\Events\Dispatcher | ||
*/ | ||
protected static function events() | ||
{ | ||
return Container::getInstance()->make(Dispatcher::class); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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