Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Adding Minutes Option in Some Frequencies #47789

Merged
merged 8 commits into from
Jul 28, 2023
62 changes: 39 additions & 23 deletions src/Illuminate/Console/Scheduling/ManagesFrequencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,68 +250,67 @@ public function hourly()
/**
* Schedule the event to run hourly at a given offset in the hour.
*
* @param array|int $offset
* @param array|string|int $offset
* @return $this
*/
public function hourlyAt($offset)
{
$offset = is_array($offset) ? implode(',', $offset) : $offset;

return $this->spliceIntoPosition(1, $offset);
return $this->hourBasedSchedule($offset, '*');
}

/**
* Schedule the event to run every odd hour.
*
* @param array|string|int $offset
* @return $this
*/
public function everyOddHour()
public function everyOddHour($offset = 0)
{
return $this->spliceIntoPosition(1, 0)->spliceIntoPosition(2, '1-23/2');
return $this->hourBasedSchedule($offset, '1-23/2');
}

/**
* Schedule the event to run every two hours.
*
* @param array|string|int $offset
* @return $this
*/
public function everyTwoHours()
public function everyTwoHours($offset = 0)
{
return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, '*/2');
return $this->hourBasedSchedule($offset, '*/2');
}

/**
* Schedule the event to run every three hours.
*
* @param array|string|int $offset
* @return $this
*/
public function everyThreeHours()
public function everyThreeHours($offset = 0)
{
return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, '*/3');
return $this->hourBasedSchedule($offset, '*/3');
}

/**
* Schedule the event to run every four hours.
*
* @param array|string|int $offset
* @return $this
*/
public function everyFourHours()
public function everyFourHours($offset = 0)
{
return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, '*/4');
return $this->hourBasedSchedule($offset, '*/4');
}

/**
* Schedule the event to run every six hours.
*
* @param array|string|int $offset
* @return $this
*/
public function everySixHours()
public function everySixHours($offset = 0)
{
return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, '*/6');
return $this->hourBasedSchedule($offset, '*/6');
}

/**
Expand All @@ -321,8 +320,7 @@ public function everySixHours()
*/
public function daily()
{
return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, 0);
return $this->hourBasedSchedule(0, 0);
}

/**
Expand All @@ -346,8 +344,10 @@ public function dailyAt($time)
{
$segments = explode(':', $time);

return $this->spliceIntoPosition(2, (int) $segments[0])
->spliceIntoPosition(1, count($segments) === 2 ? (int) $segments[1] : '0');
return $this->hourBasedSchedule(
count($segments) === 2 ? (int) $segments[1] : '0',
(int) $segments[0]
);
}

/**
Expand All @@ -374,7 +374,23 @@ public function twiceDailyAt($first = 1, $second = 13, $offset = 0)
{
$hours = $first.','.$second;

return $this->spliceIntoPosition(1, $offset)
return $this->hourBasedSchedule($offset, $hours);
}

/**
* Schedule the event to run at the given minutes and hours.
*
* @param array|string|int $minutes
* @param array|string|int $hours
* @return $this
*/
protected function hourBasedSchedule($minutes, $hours)
{
$minutes = is_array($minutes) ? implode(',', $minutes) : $minutes;

$hours = is_array($hours) ? implode(',', $hours) : $hours;

return $this->spliceIntoPosition(1, $minutes)
->spliceIntoPosition(2, $hours);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Console/Scheduling/FrequencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function testOverrideWithHourly()
{
$this->assertSame('0 * * * *', $this->event->everyFiveMinutes()->hourly()->getExpression());
$this->assertSame('37 * * * *', $this->event->hourlyAt(37)->getExpression());
$this->assertSame('*/10 * * * *', $this->event->hourlyAt('*/10')->getExpression());
$this->assertSame('15,30,45 * * * *', $this->event->hourlyAt([15, 30, 45])->getExpression());
}

Expand All @@ -84,6 +85,24 @@ public function testHourly()
$this->assertSame('0 */3 * * *', $this->event->everyThreeHours()->getExpression());
$this->assertSame('0 */4 * * *', $this->event->everyFourHours()->getExpression());
$this->assertSame('0 */6 * * *', $this->event->everySixHours()->getExpression());

$this->assertSame('37 1-23/2 * * *', $this->event->everyOddHour(37)->getExpression());
$this->assertSame('37 */2 * * *', $this->event->everyTwoHours(37)->getExpression());
$this->assertSame('37 */3 * * *', $this->event->everyThreeHours(37)->getExpression());
$this->assertSame('37 */4 * * *', $this->event->everyFourHours(37)->getExpression());
$this->assertSame('37 */6 * * *', $this->event->everySixHours(37)->getExpression());

$this->assertSame('*/10 1-23/2 * * *', $this->event->everyOddHour('*/10')->getExpression());
$this->assertSame('*/10 */2 * * *', $this->event->everyTwoHours('*/10')->getExpression());
$this->assertSame('*/10 */3 * * *', $this->event->everyThreeHours('*/10')->getExpression());
$this->assertSame('*/10 */4 * * *', $this->event->everyFourHours('*/10')->getExpression());
$this->assertSame('*/10 */6 * * *', $this->event->everySixHours('*/10')->getExpression());

$this->assertSame('15,30,45 1-23/2 * * *', $this->event->everyOddHour([15, 30, 45])->getExpression());
$this->assertSame('15,30,45 */2 * * *', $this->event->everyTwoHours([15, 30, 45])->getExpression());
$this->assertSame('15,30,45 */3 * * *', $this->event->everyThreeHours([15, 30, 45])->getExpression());
$this->assertSame('15,30,45 */4 * * *', $this->event->everyFourHours([15, 30, 45])->getExpression());
$this->assertSame('15,30,45 */6 * * *', $this->event->everySixHours([15, 30, 45])->getExpression());
}

public function testMonthly()
Expand Down