Skip to content

Commit

Permalink
Allow run() to be executed multiple times (#24)
Browse files Browse the repository at this point in the history
* Allow run() to be executed multiple times

The current code expects the run() to be called only once. This is
fine if you start the scheduler from cron each minute and re-initialize
it each time. If you want to manually run the scheduler, or maybe
have a lot of jobs you do not want to init each minute, it would
be useful to call run() multiple times in the lifetime of the
scheduler.

In this case the collected data of the last run should be reset.
the executedJobs, failedJobs and outputSchedule.

* Allow queued jobs to be removed

If the scheduler is used to do multiple runs then it would be useful
to reset all queued Jobs. Currently the only way to do this is by
re-creating the scheduler object. But if the object is injected in the
code then this is not practical..

* Fix StyleCI analysis failure

Comment format fixed.

* Allow run() to be executed multiple times

After discussion with @peppeocchi make the re-running manually
triggered. So added a resetRun() method which can be called before
each run(). Also adjusted the test for this.

* Allow run() to be executed multiple times

The isDue check on the jobs is not provided a datetime in the run()
method. This means they use the creation time of the job as the
'current' time to compare against.

Most likely this creation time is approx the time run() is called so
normally this is no problem. But if setting up the job schedule takes
more time then I would say, using the creation time as run time is
unexpected. And if you run run() multiple times then it will always
run the same jobs because the time never changes.

So now provide an explicit run time for all jobs, which is the same
for all jobs each run.

* Fix StyleCI analysis failure

Remove empty line
  • Loading branch information
merijnvdk authored and peppeocchi committed Sep 1, 2017
1 parent 2eafcf5 commit f8374af
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/GO/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ public function run()
{
$jobs = $this->getQueuedJobs();

$runTime = new DateTime('now');

foreach ($jobs as $job) {
if ($job->isDue()) {
if ($job->isDue($runTime)) {
try {
$job->run();
$this->pushExecutedJob($job);
Expand All @@ -169,6 +171,19 @@ public function run()
return $this->getExecutedJobs();
}

/**
* Reset all collected data of last run.
*
* Call before run() if you call run() multiple times.
*/
public function resetRun()
{
// Reset collected data of last run
$this->executedJobs = [];
$this->failedJobs = [];
$this->outputSchedule = [];
}

/**
* Add an entry to the scheduler verbose output array.
*
Expand Down Expand Up @@ -268,4 +283,12 @@ public function getVerboseOutput($type = 'text')
throw new InvalidArgumentException('Invalid output type');
}
}

/**
* Remove all queued Jobs.
*/
public function clearJobs()
{
$this->jobs = [];
}
}
33 changes: 33 additions & 0 deletions tests/GO/SchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,39 @@ public function testShouldPrioritizeJobsInBackround()
$this->assertEquals('async_foreground', $jobs[1]->getId());
}

public function testCouldRunTwice()
{
$scheduler = new Scheduler();

$scheduler->call(function () {
return true;
});

$scheduler->run();

$this->assertCount(1, $scheduler->getExecutedJobs(), 'Number of executed jobs');

$scheduler->resetRun();
$scheduler->run();

$this->assertCount(1, $scheduler->getExecutedJobs(), 'Number of executed jobs');
}

public function testClearJobs()
{
$scheduler = new Scheduler();

$scheduler->call(function () {
return true;
});

$this->assertCount(1, $scheduler->getQueuedJobs(), 'Number of queued jobs');

$scheduler->clearJobs();

$this->assertCount(0, $scheduler->getQueuedJobs(), 'Number of queued jobs');
}

public function testShouldRunDelayedJobsIfDueWhenCreated()
{
$scheduler = new Scheduler();
Expand Down

0 comments on commit f8374af

Please sign in to comment.