phillipsdata / tictock

An OS independent task scheduler
MIT License
1 stars 1 forks source link

Tictock

Build Status

An OS independent task scheduler.

Provides a fluent interface for scheduling commands to run at various intervals. Works on *nix and Windows.

Basic Usage

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().

Explanation of Periods

The every() method supports the following:

The only() method supports the following:

Advanced Usage

Output

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());

Extending Tictock

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);