Skip to content

Commit

Permalink
Add exception storing on failed jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
jfilla committed Oct 29, 2019
1 parent 4aa28de commit 2df65b9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
32 changes: 32 additions & 0 deletions src/GO/FailedJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php namespace GO;

use Exception;

class FailedJob
{
/**
* @var Job
*/
private $job;

/**
* @var Exception
*/
private $exception;

public function __construct(Job $job, Exception $exception)
{
$this->job = $job;
$this->exception = $exception;
}

public function getJob(): Job
{
return $this->job;
}

public function getException(): Exception
{
return $this->exception;
}
}
6 changes: 3 additions & 3 deletions src/GO/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Scheduler
/**
* Failed jobs.
*
* @var array
* @var FailedJob[]
*/
private $failedJobs = [];

Expand Down Expand Up @@ -256,7 +256,7 @@ public function getExecutedJobs()
*/
private function pushFailedJob(Job $job, Exception $e)
{
$this->failedJobs[] = $job;
$this->failedJobs[] = new FailedJob($job, $e);

$compiled = $job->compile();

Expand All @@ -273,7 +273,7 @@ private function pushFailedJob(Job $job, Exception $e)
/**
* Get the failed jobs.
*
* @return array
* @return FailedJob[]
*/
public function getFailedJobs()
{
Expand Down
12 changes: 9 additions & 3 deletions tests/GO/SchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use GO\Job;
use DateTime;
use GO\FailedJob;
use GO\Scheduler;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -82,7 +83,7 @@ public function testShouldMarkJobAsFailedIfScriptPathIsInvalid()
$scheduler->run();
$fail = $scheduler->getFailedJobs();
$this->assertCount(1, $fail);
$this->assertContainsOnlyInstancesOf(Job::class, $fail);
$this->assertContainsOnlyInstancesOf(FailedJob::class, $fail);
}

public function testShouldQueueAShellCommand()
Expand Down Expand Up @@ -152,8 +153,9 @@ public function testShouldKeepTrackOfFailedJobs()
{
$scheduler = new Scheduler();

$scheduler->call(function () {
throw new \Exception('Something failed');
$exception = new \Exception('Something failed');
$scheduler->call(function () use ($exception) {
throw $exception;
});

$this->assertEquals(count($scheduler->getFailedJobs()), 0);
Expand All @@ -162,6 +164,10 @@ public function testShouldKeepTrackOfFailedJobs()

$this->assertEquals(count($scheduler->getExecutedJobs()), 0);
$this->assertEquals(count($scheduler->getFailedJobs()), 1);
$failedJob = $scheduler->getFailedJobs()[0];
$this->assertInstanceOf(FailedJob::class, $failedJob);
$this->assertSame($exception, $failedJob->getException());
$this->assertInstanceOf(Job::class, $failedJob->getJob());
}

public function testShouldKeepExecutingJobsIfOneFails()
Expand Down

0 comments on commit 2df65b9

Please sign in to comment.