Skip to content

Commit 90c1fa4

Browse files
authored
Merge pull request jobbyphp#62 from garethellis36/check-if-job-should-run-in-main-process
Check if job is due to run in main process
2 parents 7473835 + 9ce08ac commit 90c1fa4

File tree

5 files changed

+111
-7
lines changed

5 files changed

+111
-7
lines changed

src/BackgroundJob.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,8 @@ protected function shouldRun()
198198
}
199199
}
200200

201-
$schedule = \DateTime::createFromFormat('Y-m-d H:i:s', $this->config['schedule']);
202-
if ($schedule !== false) {
203-
return $schedule->format('Y-m-d H:i') == (date('Y-m-d H:i'));
204-
}
205-
206-
$cron = CronExpression::factory($this->config['schedule']);
207-
if (!$cron->isDue()) {
201+
$scheduleChecker = new ScheduleChecker();
202+
if (!$scheduleChecker->isDue($this->config['schedule'])) {
208203
return false;
209204
}
210205

src/Jobby.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ public function run()
144144
throw new Exception('posix extension is required');
145145
}
146146

147+
$scheduleChecker = new ScheduleChecker();
147148
foreach ($this->jobs as $job => $config) {
149+
if (!$scheduleChecker->isDue($config['schedule'])) {
150+
continue;
151+
}
148152
if ($isUnix) {
149153
$this->runUnix($job, $config);
150154
} else {

src/ScheduleChecker.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Jobby;
4+
5+
use Cron\CronExpression;
6+
7+
class ScheduleChecker
8+
{
9+
/**
10+
* @param string $schedule
11+
* @return bool
12+
*/
13+
public function isDue($schedule)
14+
{
15+
$dateTime = \DateTime::createFromFormat('Y-m-d H:i:s', $schedule);
16+
if ($dateTime !== false) {
17+
return $dateTime->format('Y-m-d H:i') == (date('Y-m-d H:i'));
18+
}
19+
20+
return CronExpression::factory($schedule)->isDue();
21+
}
22+
}

tests/JobbyTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,32 @@ public function testShell()
6767
$this->assertEquals('Hello World!', $this->getLogContent());
6868
}
6969

70+
/**
71+
* @return void
72+
*/
73+
public function testBackgroundProcessIsNotSpawnedIfJobIsNotDueToBeRun()
74+
{
75+
$hour = date("H", strtotime("+1 hour"));
76+
$jobby = new Jobby();
77+
$jobby->add(
78+
'HelloWorldShell',
79+
[
80+
'command' => 'php ' . __DIR__ . '/_files/helloworld.php',
81+
'schedule' => "* {$hour} * * *",
82+
'output' => $this->logFile,
83+
]
84+
);
85+
$jobby->run();
86+
87+
// Job runs asynchronously, so wait a bit
88+
sleep($this->getSleepTime());
89+
90+
$this->assertFalse(
91+
file_exists($this->logFile),
92+
"Failed to assert that log file doesn't exist and that background process did not spawn"
93+
);
94+
}
95+
7096
/**
7197
* @covers ::add
7298
* @covers ::run

tests/ScheduleCheckerTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Jobby\Tests;
4+
5+
use Jobby\ScheduleChecker;
6+
use PHPUnit_Framework_TestCase;
7+
8+
class ScheduleCheckerTest extends PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var ScheduleChecker
12+
*/
13+
private $scheduleChecker;
14+
15+
/**
16+
* @return void
17+
*/
18+
protected function setUp()
19+
{
20+
parent::setUp();
21+
22+
$this->scheduleChecker = new ScheduleChecker();
23+
}
24+
25+
/**
26+
* @return void
27+
*/
28+
public function test_it_can_detect_a_due_job_from_a_datetime_string()
29+
{
30+
$this->assertTrue($this->scheduleChecker->isDue(date('Y-m-d H:i:s')));
31+
}
32+
33+
/**
34+
* @return void
35+
*/
36+
public function test_it_can_detect_a_non_due_job_from_a_datetime_string()
37+
{
38+
$this->assertFalse($this->scheduleChecker->isDue(date('Y-m-d H:i:s', strtotime('tomorrow'))));
39+
}
40+
41+
/**
42+
* @return void
43+
*/
44+
public function test_it_can_detect_a_due_job_from_a_cron_expression()
45+
{
46+
$this->assertTrue($this->scheduleChecker->isDue("* * * * *"));
47+
}
48+
49+
/**
50+
* @return void
51+
*/
52+
public function test_it_can_detect_a_non_due_job_from_a_cron_expression()
53+
{
54+
$hour = date("H", strtotime('+1 hour'));
55+
$this->assertFalse($this->scheduleChecker->isDue("* {$hour} * * *"));
56+
}
57+
}

0 commit comments

Comments
 (0)