Skip to content

Commit 8cb7395

Browse files
dhaarbrinkpeppeocchi
authored andcommitted
[#75] Run a job every X minutes (#82)
* [#75] Run a job every X minutes * [#75] Fixed cs issue * [#75] explain the new feature
1 parent 1b18892 commit 8cb7395

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,10 @@ If you don't call any of this method, the job will run every minute (* * * * *).
129129
```php
130130
$scheduler->php('script.php')->at('* * * * *');
131131
```
132-
- `everyMinute` - Run every minute
132+
- `everyMinute` - Run every minute. You can optionally pass a `$minute` to specify the job runs every `$minute` minutes.
133133
```php
134134
$scheduler->php('script.php')->everyMinute();
135+
$scheduler->php('script.php')->everyMinute(5);
135136
```
136137
- `hourly` - Run once per hour. You can optionally pass the `$minute` you want to run, by default it will run every hour at minute '00'.
137138
```php

src/GO/Traits/Interval.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,19 @@ public function date($date)
3939
/**
4040
* Set the execution time to every minute.
4141
*
42+
* @param int|string|null When set, specifies that the job will be run every $minute minutes
43+
*
4244
* @return self
4345
*/
44-
public function everyMinute()
46+
public function everyMinute($minute = null)
4547
{
46-
return $this->at('* * * * *');
48+
$minuteExpression = '*';
49+
if ($minute !== null) {
50+
$c = $this->validateCronSequence($minute);
51+
$minuteExpression = '*/' . $c['minute'];
52+
}
53+
54+
return $this->at($minuteExpression . ' * * * *');
4755
}
4856

4957
/**

tests/GO/IntervalTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,14 @@ public function testShouldFailIfDifferentYear()
264264
// As instance of datetime
265265
$this->assertFalse($job->date('2018-01-01')->isDue(new \DateTime('2019-01-01')));
266266
}
267+
268+
public function testEveryMinuteWithParameter()
269+
{
270+
$job = new Job('ls');
271+
272+
// Job should run at 10:00, 10:05, 10:10 etc., but not at 10:02
273+
$this->assertTrue($job->everyMinute(5)->isDue(\DateTime::createFromFormat('H:i', '10:00')));
274+
$this->assertFalse($job->everyMinute(5)->isDue(\DateTime::createFromFormat('H:i', '10:02')));
275+
$this->assertTrue($job->everyMinute(5)->isDue(\DateTime::createFromFormat('H:i', '10:05')));
276+
}
267277
}

0 commit comments

Comments
 (0)