-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
270 additions
and
98 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 |
---|---|---|
|
@@ -15,6 +15,9 @@ | |
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"getdkan/contracts": "^0.2.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^7.5" | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Procrastinator\Job; | ||
|
||
use Contracts\StorerInterface; | ||
use Contracts\RetrieverInterface; | ||
use Contracts\HydratableInterface; | ||
use Procrastinator\Result; | ||
|
||
abstract class AbstractPersistentJob extends Job implements HydratableInterface | ||
{ | ||
private $identifier; | ||
private $storage; | ||
|
||
public function run(): Result | ||
{ | ||
$result = parent::run(); | ||
$this->selfStore(); | ||
return $result; | ||
} | ||
|
||
public static function get(string $identifier, $storage, array $config = null) | ||
{ | ||
if ($storage instanceof StorerInterface && $storage instanceof RetrieverInterface) { | ||
$new = new static($identifier, $storage, $config); | ||
|
||
$json = $storage->retrieve($identifier); | ||
if ($json) { | ||
return static::hydrate($json, $new); | ||
} | ||
|
||
$storage->store(json_encode($new), $identifier); | ||
return $new; | ||
} | ||
return false; | ||
} | ||
|
||
protected function __construct(string $identifier, $storage, array $config = null) | ||
{ | ||
$this->identifier = $identifier; | ||
$this->storage = $storage; | ||
} | ||
|
||
public function setTimeLimit(int $seconds): bool | ||
{ | ||
$return = parent::setTimeLimit($seconds); | ||
$this->selfStore(); | ||
return $return; | ||
} | ||
|
||
public function jsonSerialize() | ||
{ | ||
$object = parent::jsonSerialize(); | ||
$object->identifier = $this->identifier; | ||
return $object; | ||
} | ||
|
||
protected function setStatus($status) | ||
{ | ||
parent::setStatus($status); | ||
$this->selfStore(); | ||
} | ||
|
||
protected function setError($message) | ||
{ | ||
parent::setError($message); | ||
$this->selfStore(); | ||
} | ||
|
||
protected function setState($state) | ||
{ | ||
parent::setState($state); | ||
$this->selfStore(); | ||
} | ||
|
||
private function selfStore() | ||
{ | ||
$this->storage->store(json_encode($this), $this->identifier); | ||
} | ||
} |
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
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,63 @@ | ||
<?php | ||
|
||
namespace ProcrastinatorTest\Job; | ||
|
||
use Contracts\Mock\Storage\Memory; | ||
use PHPUnit\Framework\TestCase; | ||
use ProcrastinatorTest\Job\Mock\Persistor; | ||
|
||
class AbstractPersistentJobTest extends TestCase | ||
{ | ||
public function testSerialization() | ||
{ | ||
$storage = new Memory(); | ||
|
||
$timeLimit = 10; | ||
$job = Persistor::get("1", $storage); | ||
$job->setStateProperty("ran", false); | ||
|
||
$job->setTimeLimit($timeLimit); | ||
$job->run(); | ||
|
||
$json = json_encode($job); | ||
|
||
/* @var $job2 \Procrastinator\Job\AbstractPersistentJob */ | ||
$job2 = Persistor::hydrate($json); | ||
|
||
$data = json_decode($job2->getResult()->getData()); | ||
$this->assertEquals(true, $data->ran); | ||
$this->assertEquals($timeLimit, $job2->getTimeLimit()); | ||
|
||
$job3 = Persistor::get("1", $storage); | ||
|
||
$data = json_decode($job3->getResult()->getData()); | ||
$this->assertEquals(true, $data->ran); | ||
$this->assertEquals(true, $job3->getStateProperty("ran")); | ||
$this->assertEquals(true, $job3->getStateProperty("ran2", true)); | ||
$this->assertEquals($timeLimit, $job3->getTimeLimit()); | ||
} | ||
|
||
public function testBadStorage() | ||
{ | ||
$this->assertFalse(Persistor::get("1", new class { | ||
})); | ||
} | ||
|
||
public function testJobError() | ||
{ | ||
$storage = new Memory(); | ||
|
||
$timeLimit = 10; | ||
$job = Persistor::get("1", $storage); | ||
$job->errorOut(); | ||
|
||
$job->setTimeLimit($timeLimit); | ||
$job->run(); | ||
|
||
$this->assertEquals("ERROR", $job->getResult()->getError()); | ||
|
||
$job2 = Persistor::get("1", $storage); | ||
$job2->run(); | ||
$this->assertEquals(true, $job2->getStateProperty("ran")); | ||
} | ||
} |
Oops, something went wrong.