Skip to content

Commit 1b18892

Browse files
authored
Run jobs at specific date (#70)
* Run jobs on a specific date * Documented all of the interval helpers * StyleCI fixes
1 parent 110f8a0 commit 1b18892

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

README.md

+45
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,51 @@ If you don't call any of this method, the job will run every minute (* * * * *).
145145
$scheduler->php('script.php')->daily('22:03');
146146
```
147147

148+
There are additional helpers for weekdays (all accepting optionals hour and minute - defaulted at 00:00)
149+
- `sunday`
150+
- `monday`
151+
- `tuesday`
152+
- `wednesday`
153+
- `thursday`
154+
- `friday`
155+
- `saturday`
156+
157+
```php
158+
$scheduler->php('script.php')->saturday();
159+
$scheduler->php('script.php')->friday(18);
160+
$scheduler->php('script.php')->sunday(12, 30);
161+
```
162+
163+
And additional helpers for months (all accepting optionals day, hour and minute - defaulted to the 1st of the month at 00:00)
164+
- `january`
165+
- `february`
166+
- `march`
167+
- `april`
168+
- `may`
169+
- `june`
170+
- `july`
171+
- `august`
172+
- `september`
173+
- `october`
174+
- `november`
175+
- `december`
176+
177+
```php
178+
$scheduler->php('script.php')->january();
179+
$scheduler->php('script.php')->december(25);
180+
$scheduler->php('script.php')->august(15, 20, 30);
181+
```
182+
183+
You can also specify a `date` for when the job should run.
184+
The date can be specified as string or as instance of `DateTime`. In both cases you can specify the date only (e.g. 2018-01-01) or the time as well (e.g. 2018-01-01 10:30), if you don't specify the time it will run at 00:00 on that date.
185+
If you're providing a date in a "non standard" format, it is strongly adviced to pass an instance of `DateTime`. If you're using `createFromFormat` without specifying a time, and you want to default it to 00:00, just make sure to add a `!` to the date format, otherwise the time would be the current time. [Read more](http://php.net/manual/en/datetime.createfromformat.php)
186+
187+
```php
188+
$scheduler->php('script.php')->date('2018-01-01 12:20');
189+
$scheduler->php('script.php')->date(new DateTime('2018-01-01'));
190+
$scheduler->php('script.php')->date(DateTime::createFromFormat('!d/m Y', '01/01 2018'));
191+
```
192+
148193
### Send output to file/s
149194

150195
You can define one or multiple files where you want the output of your script/command/function execution to be sent to.

src/GO/Job.php

+11
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ class Job
5151
*/
5252
private $executionTime;
5353

54+
/**
55+
* Job schedule year.
56+
*
57+
* @var string
58+
*/
59+
private $executionYear = null;
60+
5461
/**
5562
* Temporary directory path for
5663
* lock files to prevent overlapping.
@@ -193,6 +200,10 @@ public function isDue(DateTime $date = null)
193200

194201
$date = $date !== null ? $date : $this->creationTime;
195202

203+
if ($this->executionYear && $this->executionYear !== $date->format('Y')) {
204+
return false;
205+
}
206+
196207
return $this->executionTime->isDue($date);
197208
}
198209

src/GO/Traits/Interval.php

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace GO\Traits;
22

3+
use DateTime;
34
use Cron\CronExpression;
45
use InvalidArgumentException;
56

@@ -18,6 +19,23 @@ public function at($expression)
1819
return $this;
1920
}
2021

22+
/**
23+
* Run the Job at a specific date.
24+
*
25+
* @param string/DateTime $date
26+
* @return self
27+
*/
28+
public function date($date)
29+
{
30+
if (! $date instanceof DateTime) {
31+
$date = new DateTime($date);
32+
}
33+
34+
$this->executionYear = $date->format('Y');
35+
36+
return $this->at("{$date->format('i')} {$date->format('H')} {$date->format('d')} {$date->format('m')} *");
37+
}
38+
2139
/**
2240
* Set the execution time to every minute.
2341
*

tests/GO/IntervalTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,40 @@ public function testShouldRunMonthlyOnCustomDayAndTime()
228228
$this->assertTrue($job->monthly(null, 15, '12:21')->isDue($date2));
229229
$this->assertFalse($job->monthly(null, 15, '12:21')->isDue($date3));
230230
}
231+
232+
public function testShouldRunAtSpecificDate()
233+
{
234+
$job = new Job('ls');
235+
236+
$date = '2018-01-01';
237+
238+
// As instance of datetime
239+
$this->assertTrue($job->date(new \DateTime($date))->isDue(new \DateTime($date)));
240+
// As date string
241+
$this->assertTrue($job->date($date)->isDue(new \DateTime($date)));
242+
// Fail for different day
243+
$this->assertFalse($job->date($date)->isDue(new \DateTime('2018-01-02')));
244+
}
245+
246+
public function testShouldRunAtSpecificDateTime()
247+
{
248+
$job = new Job('ls');
249+
250+
$date = '2018-01-01 12:20';
251+
252+
// As instance of datetime
253+
$this->assertTrue($job->date(new \DateTime($date))->isDue(new \DateTime($date)));
254+
// As date string
255+
$this->assertTrue($job->date($date)->isDue(new \DateTime($date)));
256+
// Fail for different time
257+
$this->assertFalse($job->date($date)->isDue(new \DateTime('2018-01-01 12:21')));
258+
}
259+
260+
public function testShouldFailIfDifferentYear()
261+
{
262+
$job = new Job('ls');
263+
264+
// As instance of datetime
265+
$this->assertFalse($job->date('2018-01-01')->isDue(new \DateTime('2019-01-01')));
266+
}
231267
}

0 commit comments

Comments
 (0)