File tree Expand file tree Collapse file tree 5 files changed +111
-7
lines changed Expand file tree Collapse file tree 5 files changed +111
-7
lines changed Original file line number Diff line number Diff line change @@ -198,13 +198,8 @@ protected function shouldRun()
198
198
}
199
199
}
200
200
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 ' ])) {
208
203
return false ;
209
204
}
210
205
Original file line number Diff line number Diff line change @@ -144,7 +144,11 @@ public function run()
144
144
throw new Exception ('posix extension is required ' );
145
145
}
146
146
147
+ $ scheduleChecker = new ScheduleChecker ();
147
148
foreach ($ this ->jobs as $ job => $ config ) {
149
+ if (!$ scheduleChecker ->isDue ($ config ['schedule ' ])) {
150
+ continue ;
151
+ }
148
152
if ($ isUnix ) {
149
153
$ this ->runUnix ($ job , $ config );
150
154
} else {
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -67,6 +67,32 @@ public function testShell()
67
67
$ this ->assertEquals ('Hello World! ' , $ this ->getLogContent ());
68
68
}
69
69
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
+
70
96
/**
71
97
* @covers ::add
72
98
* @covers ::run
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments