-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 'outdated' option to scheduler command (#3771)
- Loading branch information
1 parent
88eb9f9
commit a71403f
Showing
4 changed files
with
194 additions
and
2 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
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,82 @@ | ||
<?php | ||
|
||
namespace unit\Grav\Common\Scheduler; | ||
|
||
use Grav\Common\Scheduler\Job; | ||
|
||
class JobTest extends \Codeception\Test\Unit | ||
{ | ||
/** | ||
* @dataProvider dataProviderForTestIsOverdue | ||
*/ | ||
public function testIsOverdue($job, $date, $lastRun, $expected) | ||
{ | ||
$this->assertEquals($expected, $job->isOverdue($date, $lastRun)); | ||
} | ||
|
||
public function dataProviderForTestIsOverdue() | ||
{ | ||
return [ | ||
'New Job' => [ | ||
'job' => (new Job('ls'))->at('* * * * *'), | ||
'date' => null, | ||
'lastRun' => null, | ||
'expected' => false | ||
], | ||
'New Job created 1 hour ago' => [ | ||
'job' => (new Job('ls'))->at('* * * * *'), | ||
'date' => new \DateTime('+1 hour'), | ||
'lastRun' => null, | ||
'expected' => true | ||
], | ||
'New Job created 1 minute ago' => [ | ||
'job' => (new Job('ls'))->at('* * * * *'), | ||
'date' => new \DateTime('+1 minute'), | ||
'lastRun' => null, | ||
'expected' => false | ||
], | ||
'New Job created 2 minutes ago' => [ | ||
'job' => (new Job('ls'))->at('* * * * *'), | ||
'date' => new \DateTime('+2 minutes'), | ||
'lastRun' => null, | ||
'expected' => true | ||
], | ||
'Job created 1 hour ago and last run 1 mn ago' => [ | ||
'job' => (new Job('ls'))->at('* * * * *'), | ||
'date' => new \DateTime('+1 hour'), | ||
'lastRun' => new \DateTime('+1 minutes'), | ||
'expected' => true | ||
], | ||
'Job created 1 hour ago and last run 30 mn ago' => [ | ||
'job' => (new Job('ls'))->at('* * * * *'), | ||
'date' => new \DateTime('+1 hour'), | ||
'lastRun' => new \DateTime('+30 minutes'), | ||
'expected' => true | ||
], | ||
'Job created 30 minutes ago and last run 1 hour ago' => [ | ||
'job' => (new Job('ls'))->at('* * * * *'), | ||
'date' => new \DateTime('+30 minutes'), | ||
'lastRun' => new \DateTime('+1 hour'), | ||
'expected' => false | ||
], | ||
'New hourly Job' => [ | ||
'job' => (new Job('ls'))->at('0 * * * *'), | ||
'date' => null, | ||
'lastRun' => null, | ||
'expected' => false | ||
], | ||
'New hourly Job created at 2 hours ago' => [ | ||
'job' => (new Job('ls'))->at('0 * * * *'), | ||
'date' => new \DateTime('+2 hours'), | ||
'lastRun' => null, | ||
'expected' => true | ||
], | ||
'Hourly Job created 1 hour ago and last run 30 mn ago' => [ | ||
'job' => (new Job('ls'))->at('0 * * * *'), | ||
'date' => new \DateTime('+1 hour'), | ||
'lastRun' => new \DateTime('+30 minutes'), | ||
'expected' => true | ||
], | ||
]; | ||
} | ||
} |
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 unit\Grav\Common\Scheduler; | ||
|
||
use Codeception\Util\Fixtures; | ||
use Grav\Common\Grav; | ||
use Grav\Common\Scheduler\Scheduler; | ||
use Grav\Common\Yaml; | ||
use RocketTheme\Toolbox\File\File; | ||
|
||
class SchedulerTest extends \Codeception\Test\Unit | ||
{ | ||
/** | ||
* @var \UnitTester | ||
*/ | ||
protected $tester; | ||
|
||
protected $grav; | ||
|
||
/** | ||
* @var \Grav\Common\Scheduler\Scheduler | ||
*/ | ||
protected $scheduler; | ||
private $statusFilePath; | ||
|
||
public function dataProviderForTestIsOverdue() | ||
{ | ||
return [ | ||
[ | ||
new \DateTime('+2 hours'), | ||
[ | ||
'aze45aze' => ['args'=>[], 'command'=>'ls', 'at'=>'0 * * * *'], | ||
], | ||
[ | ||
'aze45aze' => ['last-run' => strtotime('2021-01-01 00:00:00')], | ||
] | ||
], | ||
[ | ||
new \DateTime('+2 hours'), | ||
[ | ||
'aze45aze' => ['args'=>[], 'command'=>'ls', 'at'=>'0 * * * *'], | ||
'zedz5a4eza' => ['args'=>[], 'command'=>'ls', 'at'=>'*/15 * * * *'], | ||
], | ||
[ | ||
'aze45aze' => ['last-run' => strtotime('-5 minutes')], | ||
] | ||
], | ||
]; | ||
} | ||
|
||
protected function _before() | ||
{ | ||
$this->grav = Fixtures::get('grav')(); | ||
$this->scheduler = new Scheduler(); | ||
$this->statusFilePath = Grav::instance()['locator']->findResource('user-data://scheduler', true, true).'/status.yaml'; | ||
} | ||
|
||
protected function _after() | ||
{ | ||
if (file_exists($this->statusFilePath)) { | ||
unlink($this->statusFilePath); | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderForTestIsOverdue | ||
*/ | ||
public function testIsOverdue($date, $jobs, $status){ | ||
$file = $this->scheduler->getJobStates(); | ||
$file->save($status); | ||
$this->grav['config']->set('scheduler.custom_jobs', $jobs); | ||
$this->scheduler->run($date, false, true); | ||
$this->assertFileExists($this->statusFilePath); | ||
$this->assertFileIsReadable($this->statusFilePath); | ||
dump(file_get_contents($this->statusFilePath)); | ||
foreach ($jobs as $id => $job) { | ||
$this->assertStringContainsString($id, file_get_contents($this->statusFilePath)); | ||
} | ||
} | ||
} |