• Stars
    star
    383
  • Rank 111,995 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 5 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Schedule Cron jobs (commands/callbacks/bash scripts) within your Symfony application.

The ScheduleBundle

CI Code Coverage Latest Version Total Downloads

Schedule Cron jobs (commands/callbacks/bash scripts) within your Symfony application. Most applications have jobs that need to run at specific intervals. This bundle enables you to define these jobs in your code. Job definitions (tasks) are version controlled like any other feature of your application. A single Cron entry (php bin/console schedule:run) on your server running every minute executes due tasks.

The inspiration and some of the API/code for this Bundle comes from Laravel's Task Scheduling feature.

  1. Installation
  2. Quick Start
  3. Defining the Schedule
    1. ScheduleBuilder Service
    2. Your Kernel
    3. Bundle Configuration
    4. AsScheduledTask Attribute
    5. Self-Scheduling Commands
    6. Timezone
    7. Schedule Extensions
      1. Filters
      2. Callbacks
      3. Ping Webhook
      4. Email On Failure
      5. Notify On Failure
      6. Run on Single Server
      7. Limit to specific environment(s)
  4. Defining Tasks
    1. Task Types
      1. CommandTask
      2. CallbackTask
      3. ProcessTask
      4. MessageTask
      5. PingTask
      6. CompoundTask
    2. Task Description
    3. Frequency
      1. Cron Expression
      2. Fluent Expression Builder
      3. Hashed Cron Expression
    4. Task ID
    5. Timezone
    6. Task Extensions
      1. Filters
      2. Callbacks
      3. Ping Webhook
      4. Email Output
      5. Notify Output
      6. Prevent Overlap
      7. Run on Single Server
      8. Between
  5. Running the Schedule
    1. Cron Job on Server
    2. Symfony Cloud
    3. Alternatives
    4. Force Run
    5. Dealing with Failures
    6. Ensuring Schedule is Running
    7. Disable Schedule during Deploy
  6. CLI Commands
    1. schedule:list
    2. schedule:run
  7. Extending
    1. Custom Tasks
    2. Custom Extensions
    3. Events
  8. Full Configuration Reference

Installation

$ composer require zenstruck/schedule-bundle

If not using Symfony Flex, be sure to enable the bundle.

Quick Start

  1. Add your schedule service (assumes autowire and autoconfiguration enabled):

    // src/Schedule/AppScheduleBuilder.php
    
    namespace App\Schedule;
    
    use Zenstruck\ScheduleBundle\Schedule;
    use Zenstruck\ScheduleBundle\Schedule\ScheduleBuilder;
    
    class AppScheduleBuilder implements ScheduleBuilder
    {
        public function buildSchedule(Schedule $schedule): void
        {
            $schedule
                ->timezone('UTC')
                ->environments('prod')
            ;
    
            $schedule->addCommand('app:send-weekly-report --detailed')
                ->description('Send the weekly report to users.')
                ->sundays()
                ->at(1)
            ;
    
            // ...
        }
    }
  2. List your tasks to diagnose any problems:

    $ php bin/console schedule:list
  3. Add the following Cron job on your server running every minute:

    * * * * * cd /path-to-your-project && php bin/console schedule:run >> /dev/null 2>&1
    

See Defining the Schedule and Defining Tasks for more options.

Full Configuration Reference

zenstruck_schedule:

    # The LockFactory service to use for the without overlapping extension
    without_overlapping_lock_factory: null # Example: lock.default.factory

    # The LockFactory service to use for the single server extension - be sure to use a "remote store" (https://symfony.com/doc/current/components/lock.html#remote-stores)
    single_server_lock_factory: null # Example: lock.redis.factory

    # The HttpClient service to use
    http_client:          null # Example: http_client

    # The default timezone for tasks (override at task level), null for system default
    timezone:             null # Example: America/New_York

    messenger:
        enabled:              false

        # The message bus to use
        message_bus:          message_bus

    mailer:
        enabled:              false

        # The mailer service to use
        service:              mailer

        # The default "from" email address (use if no mailer default from is configured)
        default_from:         null

        # The default "to" email address (can be overridden by extension)
        default_to:           null

        # The prefix to use for email subjects (use to distinguish between different application schedules)
        subject_prefix:       null # Example: "[Acme Inc Website]"

    notifier:
        enabled:              false

        # The notifier service to use
        service:              notifier

        # The default channel (can use a string, or array of channels)
        default_channel:      null

        # The default email address for email notifications
        default_email:        null

        # The default phone number for SMS notifications (can be overridden by extension)
        default_phone:        null

        # The prefix to use for notification subjects (use to distinguish between different application schedules)
        subject_prefix:       null # Example: "[Acme Inc Website]"

    schedule_extensions:

        # Set the environment(s) you only want the schedule to run in.
        environments:         [] # Example: [prod, staging]

        # Run schedule on only one server
        on_single_server:
            enabled:              false

            # Maximum expected lock duration in seconds
            ttl:                  3600

        # Send email if schedule fails (alternatively enable by passing a "to" email)
        email_on_failure:
            enabled:              false

            # Email address to send email to (leave blank to use "zenstruck_schedule.mailer.default_to")
            to:                   null

            # Email subject (leave blank to use extension default)
            subject:              null
              3600

        # Send notification if schedule fails (alternatively enable by passing a channel)
        notify_on_failure:
            enabled:              false

            # Channel to send notification to (leave blank to use "zenstruck_schedule.notifier.default_channel")
            channel:              null

            # Notification subject (leave blank to use extension default)
            subject:              null

            # Email address for email notifications  (leave blank to use extension default)
            email:                null

            # Phone number for SMS notifications (leave blank to use extension default)
            phone:                null

        # Ping a url before schedule runs (alternatively enable by passing a url)
        ping_before:
            enabled:              false

            # The url to ping
            url:                  ~ # Required

            # The HTTP method to use
            method:               GET

            # See HttpClientInterface::OPTIONS_DEFAULTS
            options:              []

        # Ping a url after schedule runs (alternatively enable by passing a url)
        ping_after:
            enabled:              false

            # The url to ping
            url:                  ~ # Required

            # The HTTP method to use
            method:               GET

            # See HttpClientInterface::OPTIONS_DEFAULTS
            options:              []

        # Ping a url if the schedule successfully ran (alternatively enable by passing a url)
        ping_on_success:
            enabled:              false

            # The url to ping
            url:                  ~ # Required

            # The HTTP method to use
            method:               GET

            # See HttpClientInterface::OPTIONS_DEFAULTS
            options:              []

        # Ping a url if the schedule failed (alternatively enable by passing a url)
        ping_on_failure:
            enabled:              false

            # The url to ping
            url:                  ~ # Required

            # The HTTP method to use
            method:               GET

            # See HttpClientInterface::OPTIONS_DEFAULTS
            options:              []
    tasks:

        # Example:
        -
            task:                send:sales-report --detailed
            frequency:           '0 * * * *'
            description:         Send sales report hourly
            without_overlapping: ~
            only_between:        9-17
            ping_on_success:     https://example.com/hourly-report-health-check
            email_on_failure:    [email protected]
            notify_on_failure:   chat/slack

        # Prototype
        -

            # Defaults to CommandTask, prefix with "bash:" to create ProcessTask, prefix url with "ping:" to create PingTask, pass array of commands to create CompoundTask (optionally keyed by description)
            task:                 ~ # Required, Example: "my:command arg1 --option1=value", "bash:/bin/my-script" or "ping:https://example.com"

            # Cron expression
            frequency:            ~ # Required, Example: '0 * * * *'

            # Task description
            description:          null

            # The timezone for this task, null for system default
            timezone:             null # Example: America/New_York

            # Prevent task from running if still running from previous run
            without_overlapping:
                enabled:              false

                # Maximum expected lock duration in seconds
                ttl:                  86400

            # Only run between given times (alternatively enable by passing a range, ie "9:00-17:00"
            only_between:
                enabled:              false
                start:                ~ # Required, Example: 9:00
                end:                  ~ # Required, Example: 17:00

            # Skip when between given times (alternatively enable by passing a range, ie "17:00-06:00"
            unless_between:
                enabled:              false
                start:                ~ # Required, Example: 17:00
                end:                  ~ # Required, Example: 06:00

            # Ping a url before task runs (alternatively enable by passing a url)
            ping_before:
                enabled:              false

                # The url to ping
                url:                  ~ # Required

                # The HTTP method to use
                method:               GET

                # See HttpClientInterface::OPTIONS_DEFAULTS
                options:              []

            # Ping a url after task runs (alternatively enable by passing a url)
            ping_after:
                enabled:              false

                # The url to ping
                url:                  ~ # Required

                # The HTTP method to use
                method:               GET

                # See HttpClientInterface::OPTIONS_DEFAULTS
                options:              []

            # Ping a url if the task successfully ran (alternatively enable by passing a url)
            ping_on_success:
                enabled:              false

                # The url to ping
                url:                  ~ # Required

                # The HTTP method to use
                method:               GET

                # See HttpClientInterface::OPTIONS_DEFAULTS
                options:              []

            # Ping a url if the task failed (alternatively enable by passing a url)
            ping_on_failure:
                enabled:              false

                # The url to ping
                url:                  ~ # Required

                # The HTTP method to use
                method:               GET

                # See HttpClientInterface::OPTIONS_DEFAULTS
                options:              []

            # Send email after task runs (alternatively enable by passing a "to" email)
            email_after:
                enabled:              false

                # Email address to send email to (leave blank to use "zenstruck_schedule.mailer.default_to")
                to:                   null

                # Email subject (leave blank to use extension default)
                subject:              null

            # Send email if task fails (alternatively enable by passing a "to" email)
            email_on_failure:
                enabled:              false

                # Email address to send email to (leave blank to use "zenstruck_schedule.mailer.default_to")
                to:                   null

                # Email subject (leave blank to use extension default)
                subject:              null

            # Send notification after task runs (alternatively enable by passing a channel)
            notify_after:
                enabled:              false

                # Channel to send notification to (leave blank to use "zenstruck_schedule.notifier.default_channel")
                channel:              null

                # Notification subject (leave blank to use extension default)
                subject:              null

                # Email to send email notifications to (leave blank to use extension default)
                email:                null

                # Phone number for SMS notifications (leave blank to use extension default)
                phone:                null

            # Send email if task fails (alternatively enable by passing a "to" email)
            notify_on_failure:
                enabled:              false

                # Channel to send notification to (leave blank to use "zenstruck_schedule.notifier.default_channel")
                channel:              null

                # Notification subject (leave blank to use extension default)
                subject:              null

                # Email to send email notifications to (leave blank to use extension default)
                email:                null

                # Phone number for SMS notifications (leave blank to use extension default)
                phone:                null

More Repositories

1

foundry

A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.
PHP
634
star
2

messenger-test

Assertions and helpers for testing your symfony/messenger queues.
PHP
222
star
3

browser

A fluent interface for your Symfony functional tests.
PHP
185
star
4

messenger-monitor-bundle

Batteries included UI to monitor your Messenger workers, transports, schedules, and messages.
PHP
149
star
5

assert

Standalone, lightweight, framework agnostic, test assertion library.
PHP
56
star
6

console-extra

A modular set of features to reduce configuration boilerplate for your Symfony commands.
PHP
47
star
7

console-test

Alternative, opinionated helper for testing Symfony console commands.
PHP
34
star
8

callback

Callable wrapper to validate and inject arguments.
PHP
34
star
9

image

Image file wrapper with generic transformation support.
PHP
33
star
10

mailer-test

Alternative, opinionated helpers for testing emails sent with symfony/mailer.
PHP
33
star
11

redirect-bundle

Store redirects for your site and keeps statistics on redirects and 404 errors.
PHP
25
star
12

filesystem

Wrapper for league/flysystem with alternate API and added functionality.
PHP
17
star
13

uri

Object-oriented wrapper/manipulator for parse_url with additional features.
PHP
14
star
14

twig-service-bundle

Make functions, static methods, Symfony service methods available in your twig templates.
PHP
9
star
15

changelog

Generate pretty release changelogs using the commit log and Github API.
PHP
8
star
16

collection

Helpers for iterating/paginating/filtering collections (with Doctrine ORM/DBAL implementations and batch processing utilities).
PHP
8
star
17

signed-url-bundle

Helpers for signing and verifying urls with support for temporary and single-use urls.
PHP
6
star
18

class-metadata

Add human readable class aliases & metadata with efficient lookups.
PHP
5
star
19

dimension

Wrap quantity and unit of measure with conversions/humanizers.
PHP
4
star
20

redis

Lazy proxy for php-redis with DX helpers and utilities.
PHP
4
star
21

commonmark-extensions

A collection of CommonMark extensions.
PHP
4
star
22

phpmyadmin-server

Run phpMyAdmin in the background with a PHP webserver
PHP
3
star
23

dsn

DSN parsing library with support for complex expressions.
PHP
3
star
24

memoize

Helper trait to efficiently cache expensive methods in memory.
PHP
2
star
25

bytes

Parse, manipulate, humanize, and format bytes.
PHP
1
star
26

assert-html

Fluent html assertions plugin for zenstruck/assert.
PHP
1
star
27

stream

Object wrapper for PHP resources.
PHP
1
star
28

temp-file

Temporary file wrapper.
PHP
1
star
29

dom

DOM crawler with advanced selector API and assertions.
PHP
1
star