PHPackages                             tourze/symfony-cron-job-bundle - 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. [CLI &amp; Console](/categories/cli)
4. /
5. tourze/symfony-cron-job-bundle

ActiveLibrary[CLI &amp; Console](/categories/cli)

tourze/symfony-cron-job-bundle
==============================

Symfony 定时任务

1.2.0(4mo ago)032.2k20MITPHPCI passing

Since Mar 25Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/tourze/symfony-cron-job-bundle)[ Packagist](https://packagist.org/packages/tourze/symfony-cron-job-bundle)[ RSS](/packages/tourze-symfony-cron-job-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (48)Versions (18)Used By (20)

Symfony Cron Job Bundle
=======================

[](#symfony-cron-job-bundle)

[English](README.md) | [中文](README.zh-CN.md)

\[[![Latest Version](https://camo.githubusercontent.com/c43a88d3f5d7faaefbd4a20c91e38545074d5fa1bea4bbaf723e0b14fbcfcca8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746f75727a652f73796d666f6e792d63726f6e2d6a6f622d62756e646c652e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/c43a88d3f5d7faaefbd4a20c91e38545074d5fa1bea4bbaf723e0b14fbcfcca8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746f75727a652f73796d666f6e792d63726f6e2d6a6f622d62756e646c652e7376673f7374796c653d666c61742d737175617265)\] () \[[![PHP Version](https://camo.githubusercontent.com/d225e65faa64caf363da390767fe8f1eea311a81dfb74970917dfac8c0326487/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f746f75727a652f73796d666f6e792d63726f6e2d6a6f622d62756e646c652e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/d225e65faa64caf363da390767fe8f1eea311a81dfb74970917dfac8c0326487/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f746f75727a652f73796d666f6e792d63726f6e2d6a6f622d62756e646c652e7376673f7374796c653d666c61742d737175617265)\] () \[[![License](https://camo.githubusercontent.com/0e3cdd8e1ee418771b0ae8266f6ac5b2da356dd24c76e69e4aec4b6fb6139b47/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f746f75727a652f73796d666f6e792d63726f6e2d6a6f622d62756e646c652e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/0e3cdd8e1ee418771b0ae8266f6ac5b2da356dd24c76e69e4aec4b6fb6139b47/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f746f75727a652f73796d666f6e792d63726f6e2d6a6f622d62756e646c652e7376673f7374796c653d666c61742d737175617265)\] () \[[![Build Status](https://camo.githubusercontent.com/0346e18af1343d739d0405776c07b83798d5bb0200e3efcfdf657e5e662f6403/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f746f75727a652f7068702d6d6f6e6f7265706f2f63692e796d6c3f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/0346e18af1343d739d0405776c07b83798d5bb0200e3efcfdf657e5e662f6403/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f746f75727a652f7068702d6d6f6e6f7265706f2f63692e796d6c3f7374796c653d666c61742d737175617265)\] () \[[![Coverage Status](https://camo.githubusercontent.com/73ee3bab50824f778cedc6b4b3f78bcdab621eef812a979ec388b8ad692a4bff/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f746f75727a652f7068702d6d6f6e6f7265706f2e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/73ee3bab50824f778cedc6b4b3f78bcdab621eef812a979ec388b8ad692a4bff/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f746f75727a652f7068702d6d6f6e6f7265706f2e7376673f7374796c653d666c61742d737175617265)\] () \[[![Quality Score](https://camo.githubusercontent.com/760393fefb7504d9aab8da57c887d0b4d29c4af94dd456d7b3aeb971ed7b7e5d/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f746f75727a652f73796d666f6e792d63726f6e2d6a6f622d62756e646c652e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/760393fefb7504d9aab8da57c887d0b4d29c4af94dd456d7b3aeb971ed7b7e5d/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f746f75727a652f73796d666f6e792d63726f6e2d6a6f622d62756e646c652e7376673f7374796c653d666c61742d737175617265)\] () \[[![Total Downloads](https://camo.githubusercontent.com/a41d8ef0b1aa7b9b79e3eb458de9625c2f9da0eaf71bdfaaccb3c0fd1af81573/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f746f75727a652f73796d666f6e792d63726f6e2d6a6f622d62756e646c652e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/a41d8ef0b1aa7b9b79e3eb458de9625c2f9da0eaf71bdfaaccb3c0fd1af81573/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f746f75727a652f73796d666f6e792d63726f6e2d6a6f622d62756e646c652e7376673f7374796c653d666c61742d737175617265)\] ()

A Symfony bundle for managing and running cron jobs with flexible scheduling, registration, and async execution support. Features anti-duplicate execution, HTTP triggers, and Twig integration for serverless environments.

Table of Contents
-----------------

[](#table-of-contents)

- [Quick Start](#quick-start)
    - [Requirements](#requirements)
    - [Installation](#installation)
    - [1. Register a Cron Job](#1-register-a-cron-job)
    - [2. Enable the Bundle](#2-enable-the-bundle)
    - [3. Add Cron Entry](#3-add-cron-entry)
    - [4. Start the Scheduler](#4-start-the-scheduler)
    - [5. Manual Execution](#5-manual-execution)
- [Features](#features)
- [Advanced Usage](#advanced-usage)
    - [Custom Lock TTL](#custom-lock-ttl)
    - [Dynamic Job Registration](#dynamic-job-registration)
- [Serverless Integration](#serverless-integration)
    - [HTTP Triggers for Serverless](#http-triggers-for-serverless)
    - [Twig Auto-trigger](#twig-auto-trigger)
- [Environment Configuration](#environment-configuration)
- [Available Commands](#available-commands)
- [API Reference](#api-reference)
    - [AsCronTask Attribute](#ascrontask-attribute)
- [Interfaces](#interfaces)
    - [CronCommandProvider Interface](#croncommandprovider-interface)
    - [CommandRequest Class](#commandrequest-class)
    - [CronTriggerService](#crontriggerservice)
- [Configuration](#configuration)
    - [Bundle Dependencies](#bundle-dependencies)
    - [Cache Configuration](#cache-configuration)
    - [Messenger Configuration](#messenger-configuration)
- [Contributing](#contributing)
- [License](#license)
- [Changelog](#changelog)

Quick Start
-----------

[](#quick-start)

### Requirements

[](#requirements)

- PHP &gt;= 8.1
- Symfony &gt;= 6.4
- Extensions: `posix`, `pcntl`

### Installation

[](#installation)

Install via Composer:

```
composer require tourze/symfony-cron-job-bundle
```

### 1. Register a Cron Job

[](#1-register-a-cron-job)

Use the `AsCronTask` attribute on your Symfony command:

```
use Tourze\Symfony\CronJob\Attribute\AsCronTask;

#[AsCronTask('0 * * * *')] // runs every hour
class MyHourlyCommand extends Command { ... }
```

Or implement the `CronCommandProvider` interface to provide jobs dynamically.

### 2. Enable the Bundle

[](#2-enable-the-bundle)

Add the bundle to your `bundles.php` file:

```
return [
    // ... other bundles
    Tourze\Symfony\CronJob\CronJobBundle::class => ['all' => true],
];
```

### 3. Add Cron Entry

[](#3-add-cron-entry)

```
php bin/console cron-job:add-cron-tab
```

This registers the main cron entry in your system crontab.

### 4. Start the Scheduler

[](#4-start-the-scheduler)

```
php bin/console cron:start
```

This starts a process to check and run due cron jobs every minute.

### 5. Manual Execution

[](#5-manual-execution)

```
php bin/console cron:run
```

Manually trigger and run all cron jobs that are due at the current time.

Features
--------

[](#features)

- **Multiple Registration Methods**: Register cron jobs via PHP attribute or provider interface
- **Auto-generate Crontab**: Auto-generate crontab entries and manage them programmatically
- **Asynchronous Execution**: Execute scheduled commands asynchronously via Symfony Messenger
- **Anti-duplicate Execution**: Built-in locking mechanism prevents duplicate task execution
- **HTTP Triggers**: Support HTTP triggers for serverless environments (AWS Lambda, Function Compute, etc.)
- **Twig Integration**: Auto-trigger via JavaScript in templates
- **Flexible Scheduling**: Support for custom cron expressions with per-task lock TTL
- **Symfony Integration**: Integrates with Symfony Messenger, Lock, and Cache components

Advanced Usage
--------------

[](#advanced-usage)

### Custom Lock TTL

[](#custom-lock-ttl)

Configure custom lock timeout for specific tasks:

```
use Tourze\Symfony\CronJob\Attribute\AsCronTask;

#[AsCronTask(
    expression: '*/5 * * * *',  // Every 5 minutes
    lockTtl: 300               // Lock for 5 minutes (300 seconds)
)]
class MyCustomCommand extends Command
{
    // ... command implementation
}
```

### Dynamic Job Registration

[](#dynamic-job-registration)

Implement the `CronCommandProvider` interface for dynamic job registration:

```
use Tourze\Symfony\CronJob\Provider\CronCommandProvider;
use Tourze\Symfony\CronJob\Request\CommandRequest;

class MyJobProvider implements CronCommandProvider
{
    public function getCommands(): iterable
    {
        $request = new CommandRequest();
        $request->setCommand('app:process-queue');
        $request->setCronExpression('*/10 * * * *');
        $request->setLockTtl(600);  // 10 minutes
        $request->setOptions(['--env' => 'prod']);

        yield $request;
    }
}
```

Serverless Integration
----------------------

[](#serverless-integration)

### HTTP Triggers for Serverless

[](#http-triggers-for-serverless)

For serverless environments, use HTTP triggers:

```
# Direct HTTP request
curl -X POST https://your-app.com/cron/trigger
```

### Twig Auto-trigger

[](#twig-auto-trigger)

Add auto-trigger functionality to your templates:

```
{# Basic usage: trigger every 60 seconds #}
{{ cron_auto_trigger() }}

{# Custom interval: trigger every 30 seconds #}
{{ cron_auto_trigger(30000) }}

{# With debug and retry options #}
{{ cron_auto_trigger(60000, {
    debug: true,
    maxRetries: 5,
    retryDelay: 10000
}) }}
```

Environment Configuration
-------------------------

[](#environment-configuration)

Configure auto-trigger interval via environment variables:

```
# .env
CRON_AUTO_TRIGGER_INTERVAL=30000  # 30 seconds
```

Available Commands
------------------

[](#available-commands)

- `cron-job:add-cron-tab` - Add the main cron entry to system crontab
- `cron:start` - Start the cron scheduler daemon process
- `cron:run` - Run all cron jobs due at the current time

API Reference
-------------

[](#api-reference)

### AsCronTask Attribute

[](#ascrontask-attribute)

Configure cron jobs directly on command classes:

```
#[AsCronTask(
    expression: '0 */6 * * *',  // Every 6 hours
    lockTtl: 21600             // Lock for 6 hours (21600 seconds)
)]
```

**Parameters:**

- `expression`: Cron expression (default: `'* * * * *'`)
- `lockTtl`: Lock timeout in seconds (default: `null` = 3600 seconds)

Interfaces
----------

[](#interfaces)

### CronCommandProvider Interface

[](#croncommandprovider-interface)

Implement this interface to provide dynamic cron jobs:

```
interface CronCommandProvider
{
    public function getCommands(): iterable;
}
```

### CommandRequest Class

[](#commandrequest-class)

Configure dynamic cron job requests:

```
$request = new CommandRequest();
$request->setCommand('app:example');
$request->setCronExpression('0 2 * * *');
$request->setLockTtl(7200);
$request->setOptions(['--batch-size' => 100]);
```

**Methods:**

- `setCommand(string $command)`: Set the command name
- `setCronExpression(string $expression)`: Set the cron expression
- `setLockTtl(?int $ttl)`: Set the lock timeout
- `setOptions(array $options)`: Set command options

### CronTriggerService

[](#crontriggerservice)

Main service for triggering cron jobs:

```
public function triggerScheduledTasks(): bool
```

Returns `true` if tasks were triggered, `false` if already triggered this minute.

Configuration
-------------

[](#configuration)

### Bundle Dependencies

[](#bundle-dependencies)

This bundle depends on the following packages:

- `tourze/async-command-bundle` - For asynchronous command execution
- `tourze/lock-service-bundle` - For task locking mechanism
- `tourze/symfony-routing-auto-loader-bundle` - For auto-routing

### Cache Configuration

[](#cache-configuration)

The bundle uses Symfony's cache system for anti-duplicate execution. Configure your cache adapter in `config/packages/cache.yaml`:

```
framework:
    cache:
        app: cache.adapter.redis  # Or your preferred adapter
```

### Messenger Configuration

[](#messenger-configuration)

For asynchronous execution, configure Symfony Messenger:

```
# config/packages/messenger.yaml
framework:
    messenger:
        transports:
            async: "%env(MESSENGER_TRANSPORT_DSN)%"
        routing:
            'Tourze\AsyncCommandBundle\Message\RunCommandMessage': async
```

Contributing
------------

[](#contributing)

- Open issues or pull requests on GitHub
- Follow PSR coding standards
- Write and run tests before submitting PRs

License
-------

[](#license)

MIT License. See [LICENSE](LICENSE) for details.

Changelog
---------

[](#changelog)

See Git history for latest changes and releases.

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance74

Regular maintenance activity

Popularity21

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~16 days

Recently: every ~11 days

Total

17

Last Release

145d ago

Major Versions

0.1.7 → 1.0.02025-10-31

### Community

Maintainers

![](https://www.gravatar.com/avatar/e354fdb316da535dfa8ba2e9193a473c403b6bc6fb9170778d1dc50e304c6e9d?d=identicon)[tourze](/maintainers/tourze)

---

Top Contributors

[![tourze](https://avatars.githubusercontent.com/u/13899502?v=4)](https://github.com/tourze "tourze (3 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tourze-symfony-cron-job-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/tourze-symfony-cron-job-bundle/health.svg)](https://phpackages.com/packages/tourze-symfony-cron-job-bundle)
```

###  Alternatives

[contao/core-bundle

Contao Open Source CMS

1231.6M2.4k](/packages/contao-core-bundle)[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[ec-cube/ec-cube

EC-CUBE EC open platform.

78527.0k1](/packages/ec-cube-ec-cube)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.2M386](/packages/shopware-core)

PHPackages © 2026

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