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

Support for scheduling fortnight commands #47 #61

Merged
merged 2 commits into from
Jul 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/Indatus/Dispatcher/Drivers/Cron/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/

use App;
use Indatus\Dispatcher\Scheduling\Schedulable;

class Scheduler extends Schedulable
Expand Down Expand Up @@ -122,7 +123,30 @@ public function monthly()
}

/**
* Run once a week at midnight in the morning of the first day of the month
* Run once every other week at midnight on Sunday morning
*
* @return $this
*/
public function everyOtherWeek()
{
$carbon = App::make('Carbon');
$weeks = 2;

if ($carbon->now()->weekOfYear % $weeks == 0) {
$this->setScheduleMinute(0);
$this->setScheduleHour(0);
$this->setScheduleDayOfMonth(self::ANY);
$this->setScheduleMonth(self::ANY);
$this->setScheduleDayOfWeek("0");
} else {
return $this->never();
}

return $this;
}

/**
* Run once a week at midnight on Sunday morning
*
* @return $this
*/
Expand Down Expand Up @@ -169,6 +193,22 @@ public function hourly()
return $this;
}

/**
* Valid cron syntax that will never run. Feb 31st?!
* @see http://stackoverflow.com/a/13938099
* @return $this
*/
public function never()
{
$this->setScheduleMinute(0);
$this->setScheduleHour(0);
$this->setScheduleDayOfMonth(31);
$this->setScheduleMonth(2);
$this->setScheduleDayOfWeek(self::ANY);

return $this;
}

/**
* Set the minutes under which this command will run
* @param mixed $minutesIntoTheHour
Expand Down
29 changes: 29 additions & 0 deletions tests/Drivers/Cron/TestCronScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @author Ben Kuhl <[email protected]>
*/

use Mockery as m;
use Indatus\Dispatcher\Drivers\Cron\Scheduler;

class TestCronScheduler extends TestCase
Expand Down Expand Up @@ -50,6 +51,28 @@ public function testMonthly()
$this->assertEquals($this->scheduler->getSchedule(), '0 0 1 '.Scheduler::ANY.' '.Scheduler::ANY);
}

public function testEveryOtherWeekEven()
{
$carbon = m::mock();
$carbon->shouldReceive('now')->andReturn($carbon);
$carbon->weekOfYear = 32;
App::instance('Carbon', $carbon);

$this->assertInstanceOf($this->schedulerClass, $this->scheduler->everyOtherWeek());
$this->assertEquals($this->scheduler->getSchedule(), '0 0 '.Scheduler::ANY.' '.Scheduler::ANY.' 0');
}

public function testEveryOtherWeekOdd()
{
$carbon = m::mock();
$carbon->shouldReceive('now')->andReturn($carbon);
$carbon->weekOfYear = 33;
App::instance('Carbon', $carbon);

$this->assertInstanceOf($this->schedulerClass, $this->scheduler->everyOtherWeek());
$this->assertEquals($this->scheduler->getSchedule(), '0 0 31 2 '.Scheduler::ANY);
}

public function testWeekly()
{
$this->assertInstanceOf($this->schedulerClass, $this->scheduler->weekly());
Expand All @@ -68,6 +91,12 @@ public function testHourly()
$this->assertEquals($this->scheduler->getSchedule(), '0 '.Scheduler::ANY.' '.Scheduler::ANY.' '.Scheduler::ANY.' '.Scheduler::ANY);
}

public function testNever()
{
$this->assertInstanceOf($this->schedulerClass, $this->scheduler->never());
$this->assertEquals($this->scheduler->getSchedule(), '0 0 31 2 '.Scheduler::ANY);
}

public function testMinutes()
{
$minutes = 15;
Expand Down