PHPackages                             heartsentwined/zf2-cron - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. heartsentwined/zf2-cron

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

heartsentwined/zf2-cron
=======================

ZF2 cron module

v3.2.3(9y ago)606.8k17[2 issues](https://github.com/heartsentwined/zf2-cron/issues)[2 PRs](https://github.com/heartsentwined/zf2-cron/pulls)ISCPHPPHP &gt;=5.3.3

Since Sep 16Pushed 7y ago8 watchersCompare

[ Source](https://github.com/heartsentwined/zf2-cron)[ Packagist](https://packagist.org/packages/heartsentwined/zf2-cron)[ Docs](https://github.com/yalesov/zf2-cron)[ RSS](/packages/heartsentwined-zf2-cron/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (9)Versions (36)Used By (0)

zf2-cron
========

[](#zf2-cron)

[![Build Status](https://camo.githubusercontent.com/b30e75ac7db14a93ae59b156fd78b896f9222cb466dfec1354f350d0419bd827/68747470733a2f2f7472617669732d63692e6f72672f79616c65736f762f7a66322d63726f6e2e737667)](https://travis-ci.org/yalesov/zf2-cron)

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
============

[](#installation)

[Composer](http://getcomposer.org/):

```
{
  "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`](https://github.com/doctrine/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
========

[](#features)

- All standard cron features (within PHP)
- Extended cron expression to support more complex use cases
- Centralized cron entry point / runner - you only need to maintain one entry in your crontab
- Cron job registration at runtime - you can, for example, register conditional cron jobs
- Cron job identifiers - allow for later modification of cron jobs
- Cron job lock - prevent a single job from being executed multiple times
- Unresponsive script recovery - recover long-running cron jobs and re-queue them automatically
- Logging - All jobs, whether `success`, `running`, `missed`, or `error`, will be logged; error messages and stack traces are also logged for `error` and `missed` cron jobs.

Config
======

[](#config)

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

- `scheduleAhead`: how long ahead to schedule cron jobs. Cron jobs must be scheduled into individual jobs before they can be run, in order to keep track of individual cron job locks, logs, and recovery from individual long-running scripts.
- `scheduleLifetime`: how long before a scheduled job is considered missed. If a job is scheduled, but not run - e.g. the crontab entry is deleted - for longer than `scheduleLifetime`, it will be invalidated and marked as `missed`.
- `maxRunningTime`: maximum running time of each cron job. If jobs are running for longer than `maxRunningTime`, it will be recovered and re-queued for re-run.
- `successLogLifetime`: how long to keep successfully completed cron job logs
- `failureLogLifetime`: how long to keep failed (`missed` / `error`) cron job logs

Usage
=====

[](#usage)

Add a cron job
--------------

[](#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:

- range: `0-5`
- range + interval: `10-59/5`
- comma-separated combinations of these: `1,4,7,10-20`
- English months: `january`
- English months (abbreviated to three letters): `jan`
- English weekdays: `monday`
- English weekdays (abbreviated to three letters): `mon`
- These text counterparts can be used in all places where their numerical counterparts are allowed, e.g. `jan-jun/2`
- A full example: `0-5,10-59/5 * 2-10,15-25 january-june/2 mon-fri` (every minute between minute 0-5 + every 5th minute between 10-59; every hour; every day between day 2-10 and day 15-25; every 2nd month between January-June; Monday-Friday)

`$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](http://php.net/manual/en/function.call-user-func-array.php), so `$args[0]` will be the first argument, `$args[1]` the second, etc.

Run the cron jobs
-----------------

[](#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
-----------------------------------

[](#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`:

- `id`: unique identifier for individual cron jobs
- `code`: cron job "batch/family" identifier, as set in `Cron::register()`
- `status`: one of `pending`, `success`, `running`, `missed`, or `error`
- `errorMsg`: stores the error message for `error` cron jobs
- `stackTrace`: stores the stack trace for `error` cron jobs
- `createTime`: (datetime) time when this individual job is created
- `scheduleTime`: (datetime) time when this individual job is scheduled to be run
- `executeTime`: (datetime) time when this job is run (start of execution)
- `finishTime`: (datetime) time when this job has terminated. will be `null` for jobs other than `success`.

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

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 83.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~41 days

Recently: every ~0 days

Total

35

Last Release

3598d ago

Major Versions

0.1.0 → 1.0.02012-09-16

1.1.2 → 2.0.02012-09-19

2.2.11 → v3.0.02016-07-05

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2252978?v=4)[heartsentwined](/maintainers/heartsentwined)[@heartsentwined](https://github.com/heartsentwined)

---

Top Contributors

[![yalesov](https://avatars.githubusercontent.com/u/20264293?v=4)](https://github.com/yalesov "yalesov (10 commits)")[![hikaru-shindo](https://avatars.githubusercontent.com/u/524290?v=4)](https://github.com/hikaru-shindo "hikaru-shindo (1 commits)")[![TomHAnderson](https://avatars.githubusercontent.com/u/493920?v=4)](https://github.com/TomHAnderson "TomHAnderson (1 commits)")

---

Tags

cronZendFrameworkzf2

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/heartsentwined-zf2-cron/health.svg)

```
[![Health](https://phpackages.com/badges/heartsentwined-zf2-cron/health.svg)](https://phpackages.com/packages/heartsentwined-zf2-cron)
```

###  Alternatives

[slm/google-analytics

Google Analytics tracking integration for Zend Framework 2

4180.3k](/packages/slm-google-analytics)[akrabat/akrabat-session

A Zend Framework 2 module for configuring sessions

134.5k](/packages/akrabat-akrabat-session)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
