An OS independent task scheduler.
Provides a fluent interface for scheduling commands to run at various intervals. Works on *nix and Windows.
Initialize Tictock with your command:
use Tictock\Tictock;
$cmd = 'the command you want to run';
$tictock = new Tictock($cmd);
Schedule your command:
$schedule = $tictock->schedule()
->every()
->minute()
->every()
->hour()
->every()
->dayOfTheMonth()
->every()
->month()
->every()
->dayOfTheWeek();
$tictock->save($schedule);
The above would schedule your command to execute every minute of every hour of every day. This is the default schedule. We could actually simplify this as:
$schedule = $tictock->schedule();
$tictock->save($schedule);
What if we only want to execute on Fridays at 1 AM?
$schedule = $tictock->schedule()
->only()
->daysOfTheWeek(array(5)) // 0 = Sun, 1 = Mon, ... 5 = Fri, 6 = Sat
->only()
->hours(array(1)); // 0 = 12 AM, 1 = 1 AM, ... 12 = 12 PM, ... 23 = 11 PM
$tictock->save($schedule);
Notice how we don't have to declare that we want it to run every month, or every day of the month. The scheduler will automatically run at every interval unless we tell it otherwise.
Suppose we wanted to run something every 5 minutes?
$schedule = $tictock->schedule()
->every()
->minutes(5);
$tictock->save($schedule);
This could also be written as:
$schedule = $tictock->schedule()
->only()
->minutes(array(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55));
$tictock->save($schedule);
Note: Defining select hours or minutes to run is not supported by Windows. The smallest non-interval value supported by Windows is daysofTheMonth()
.
The every()
method supports the following:
minute()
Every minuteminutes(int $n)
Every$n
th minute (2 - 59)hour()
Every hourhours(int $n)
Every$n
th hour (2 - 23)dayOfTheMonth()
Every day of the monthdaysOfTheMonth(int $n)
Every$n
th day of the month (2 - 30)month()
Every monthmonths(int $n)
Every$n
th month (2 - 11)dayOfTheWeek()
Every day of the weekdaysOfTheWeek(int $n)
Every$n
th day of the week
The only()
method supports the following:
minutes(array $minutes)
Only these minutes (0 - 59)hours(array $hours)
Only these hours (0 - 23)0
= 12 AM, ...,23
= 11 PM
daysOfTheMonth(array $days)
Only these days of the month (1 - 31)months(array $months)
Only these months (1 - 12)1
= Janurary2
= February3
= March4
= April5
= May6
= June7
= July8
= August9
= September10
= October11
= November12
= December
daysOfTheWeek(array $days)
Only these days of the week (0 - 6)0
= Sunday1
= Monday2
= Tuesday3
= Wednesday4
= Thursday5
= Friday6
= Saturday
The result of the request is returned by Tictock::save()
. If you need the actual output returned, you need explicitly declare the scheduler. This can be done using the built-in ScheduleFactory
or by explicitly initializing the Scheduler you want.
use Tictock\Tictock;
$cmd = 'the command you want to run';
$tictock = new Tictock($cmd);
$scheduler = $tictock->scheduler();
$schedule = $tictock->schedule()
$result = $tictock->save($schedule, $scheduler);
print_r($scheduler->output());
Tictock is totally modular. Use your own Schedule or Scheduler to do crazy stuff, like create a recurring todo on some remote web service or program your sprinkler system.
class MySchedule implements \Tictock\Schedule\ScheduleInterface
{
// ...
}
class MyScheduler implements \Tictock\Scheduler\SchedulerInterface
{
// ...
}
use Tictock\Tictock;
$data = 'your data';
$tictock = new Tictock($data);
$schedule = new MySchedule();
$scheduler = new MyScheduler();
$tictock->save($schedule, $scheduler);