-
Notifications
You must be signed in to change notification settings - Fork 0
Add a persistent Job. #9
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
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9009ef4
Add a persitent Job.
fmizzell 778fea3
Passing a shell instance during job hydration.
fmizzell 5f0d3da
Fix code style.
fmizzell 4bd9ee4
Fix code style.
fmizzell 19af8cb
Fix code style.
fmizzell 178deaf
Creating mock job.
fmizzell 17d1901
Fixing complexity issue.
fmizzell f44309b
Make persistent job constructor protected, and allow for passing conf…
fmizzell c3a85e0
Save state after run for a persistent job.
fmizzell 22f936e
Fix some indentations for Code Climate (#11)
thierrydallacroce 1f3333f
Contracts release.
fmizzell 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
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
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,72 @@ | ||
<?php | ||
|
||
namespace Procrastinator\Job; | ||
|
||
use Contracts\StorerInterface; | ||
use Contracts\RetrieverInterface; | ||
use Contracts\HydratableInterface; | ||
|
||
abstract class AbstractPersistentJob extends Job implements HydratableInterface | ||
{ | ||
private $identifier; | ||
private $storage; | ||
|
||
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 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
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
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
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,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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once PR GetDKAN/contracts#10 (comment) gets merge, the above should be switched back to master