Example #1
0
<?php

//
// Add this line to your crontab file:
//
// * * * * * cd /path/to/project && php jobby.php 1>> /dev/null 2>&1
//
require __DIR__ . '/vendor/autoload.php';
$jobby = new \Jobby\Jobby();
$jobby->add('CommandExample', array('command' => 'ls', 'schedule' => '* * * * *', 'output' => 'logs/command.log', 'enabled' => true));
$jobby->add('ClosureExample', array('command' => function () {
    echo "I'm a function!\n";
    return true;
}, 'schedule' => '* * * * *', 'output' => 'logs/closure.log', 'enabled' => true));
$jobby->run();
Example #2
0
// First demo-job - print "date" to logs/command-pdo.log.
$insertCronJobConfiguration->execute(['CommandExample', 'date', '* * * * *', 'logs/command-pdo.log']);
// Second demo-job - a Closure which does some php::echo(). The Closure is saved to PDO-backend, too.
$secondJobFn = function () {
    echo "I'm a function (" . date('Y-m-d H:i:s') . ')!' . PHP_EOL;
    return true;
};
$secondJobFnSerializable = new \SuperClosure\SerializableClosure($secondJobFn);
$secondJobFnSerialized = serialize($secondJobFnSerializable);
$insertCronJobConfiguration->execute(['ClosureExample', $secondJobFnSerialized, '* * * * *', 'logs/closure-pdo.log']);
/*
 * Examples are now set up, and saved to PDO-backend.
 *
 * Now, fetch all jobbies from PDO-backend and run them.
 */
$jobbiesStmt = $dbh->query("SELECT * FROM `{$dbhJobbiesTableName}`");
$jobbies = $jobbiesStmt->fetchAll(PDO::FETCH_ASSOC);
$jobby = new \Jobby\Jobby();
foreach ($jobbies as $job) {
    // Filter out each value, which is not set (for example, "maxRuntime" is not defined in the job).
    $job = array_filter($job);
    $commandUnserialized = @unserialize($job['command']);
    if (false !== $commandUnserialized) {
        assert($commandUnserialized instanceof \SuperClosure\SerializableClosure);
        $job['command'] = $commandUnserialized;
    }
    $jobName = $job['name'];
    unset($job['name']);
    $jobby->add($jobName, $job);
}
$jobby->run();