yalesov / zf2-cron

ZF2 cron module
60 stars 16 forks source link

zf2-cron

Build Status

ZF2 cron module

This module serves as a central cron runner. It allows you to register a cron job in your ZF2 app; these jobs will be aggregated and run together. This allows you to maintain only a single entry in your crontab, while maintaining as many cron jobs as needed in the ZF2 app.

Installation

Composer:

{
  "require": {
    "yalesov/zf2-cron": "3.*"
  }
}

Then add Yalesov\Cron to the modules key in (app root)/config/application.config.*

Cron module will also hook onto your application's database, through DoctrineORMModule. It will create a single table, he_cron_job, and will use the default EntityManager doctrine.entitymanager.orm_default. If your settings are different, please modify the doctrine section of config/module.config.yml and instances of doctrine.entitymanager.orm_default in Yalesov\Cron\Controller\CronController as needed.

Finally, you need to update your database schema. The recommended way is through Doctrine's CLI:

$ vendor/bin/doctrine-module orm:schema-tool:update --force

Features

Config

Copy config/cron.local.php.dist to (app root)/config/autoload/cron.local.php, and modify the settings.

Usage

Add a cron job

Run Foo::runCron('bar', 'baz') every 15 minutes, with the identifier foo.

use Yalesov\Cron\Service\Cron;
Cron::register(
  'foo',
  '*/15 * * * *',
  'Foo::runCron',
  array('bar', 'baz')
);

Function signature:

public static function register($code, $frequency, $callback, array $args = array())

$code is a unique identifier for the job. It allows for later job overwriting, retrieval of Jobs by identifier, filtering, etc.

$frequency is any valid cron expression, in addition supporting:

$callback is any valid PHP callback.

$args (optional) are the arguments to the callback. The cron job will be called with call_user_func_array, so $args[0] will be the first argument, $args[1] the second, etc.

Run the cron jobs

In order to actually run the cron jobs, you will need to setup a (real) cron job to trigger the Cron module. It will then run your registered cron jobs at their specified frequencies, retrying and logging as specified.

Recommended: set up a cron job and run the ZF2 app through CLI:

$ php public/index.php cron

Fallback: if the above doesn't work, you can always run it through a browser (or through lynx, wget, etc)

http://(zf2 app)/cron

Security: this will not expose any of your cron data or error outputs to any end-user. The controller will immediately close the connection, send an empty response, and continue running the cron jobs as background.

Retrieving logs; advanced use cases

You can interact with individual cron jobs through the Doctrine 2 ORM API. The Cron module only defines a single Entity Yalesov\Cron\Entity\Job:

Example: retrieve all error messages of the cron job foo:

// $em instance of EntityManager
$errorJobs = $em->getRepository('Yalesov\Cron\Entity\Job')->findBy(array(
  'code'   => 'foo',
  'status' => \Yalesov\Cron\Repository\Job::STATUS_ERROR,
));
foreach ($errorJobs as $job) {
  echo sprintf(
    "cron job, code %s, executed at %s with error \n %s \n\n",
    $job->getCode(), // will always be 'foo' in this example
    $job->getExecuteTime()->format('r'),
    $job->getErrorMsg()
  );
}