PHP Classes

Symfony Task bundle: Schedule task using cron with Symfony

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 68 All time: 10,325 This week: 673Up
Version License PHP version Categories
task-bundle 1.0.2MIT/X Consortium ...5PHP 5, Time and Date, Libraries
Description 

Author

This package can schedule task using cron with Synfony.

It provides an interface that can be implemented by classes that will run tasks.

The task classes should have annotation comments to define when the tasks should be run.

The package can execute tasks when it is called and the current time matches the schedule to run a task.

Picture of Emil Kilhage
Name: Emil Kilhage <contact>
Classes: 3 packages by
Country: Sweden Sweden
Age: ???
All time rank: 428127 in Sweden Sweden
Week rank: 195 Up2 in Sweden Sweden Up

Documentation

task-bundle

Build Status Scrutinizer Code Quality Coverage Status Latest Stable Version Total Downloads License

Provides a simple framework to manage scheduling and execution of tasks Symfony application.

Prerequisite

This bundle requires cron to be installed on the server to be able to execute scheduled tasks

Installation

Add the glooby/task-bundle package to your require section in the composer.json file.

$ composer require glooby/task-bundle ~3.0

Add the GloobyTaskBundle to your application's kernel:

<?php
public function registerBundles()
{
    $bundles = [
        // ...
        new Glooby\TaskBundle\GloobyTaskBundle(),
        // ...
    ];
    ...
}

Create this file /etc/cron.d/glooby_scheduler_run

  *  nginx  cd /path/to/project && php bin/console scheduler:run --env=prod &> /dev/null 2>&1

Documentation

Create a executable Task

To setup a new runnable task you should follow these steps

Implement the TaskInterface

example: src/Glooby/Api/TaskBundle/Task/PingTask.php


    class PingTask implements TaskInterface
    {
        /
         * @inheritdoc
         */
        public function run(array $params = [])
        {
            return 'pong';
        }
    }

Add a service for your task

services:
    glooby_task.ping:
        class: Glooby\TaskBundle\Task\PingTask

Try and run the task trough cli


    $ bin/console task:run glooby_task.ping

    "pong"

Setup Scheduled task

To setup a new schedule you should follow the steps below

Make your service runnable

Follow the steps in Create a executable Task

Tag your service

By tagging your service with the glooby.scheduled_task tag it will be treated as a scheduled task

example:

src/Glooby/Api/TaskBundle/Resources/config/services.yml


services:
    glooby_task.ping:
        class: Glooby\TaskBundle\Task\PingTask
        tags:
            - { name: glooby.scheduled_task }

Annotate your class

Annotate your class with this annotation: Glooby\TaskBundle\Annotation\Schedule

Parameters

interval

The first parameter to the annotation is defaulted to the interval parameter. In this parameter you configure the interval that the service should be executed.

The interval is a string of five or optional six subexpressions that describe details of the schedule. The syntax is based on the Linux cron daemon definition.

            
    -    -    -    -    -    -
    |    |    |    |    |    |
    |    |    |    |    |    + year [optional]
    |    |    |    |    +----- day of week (0 - 7) (Sunday=0 or 7)
    |    |    |    +---------- month (1 - 12)
    |    |    +--------------- day of month (1 - 31)
    |    +-------------------- hour (0 - 23)
    +------------------------- min (0 - 59)

This is the only required parameter


use Glooby\TaskBundle\Annotation\Schedule;

/
 @Schedule("  ")
 */
class PingTask implements TaskInterface
{

Here you have several shortcuts that you can use instead for most common use cases

| value | interval | |:---------------:|:------------:| | @yearly | 0 0 1 1 * | | @annually | 0 0 1 1 * | | @monthly | 0 0 1 | | @weekly | 0 0 0 | | @daily | 0 0 * | | @hourly | 0 | | @semi_hourly | /30 * | | @quarter_hourly | /15 * | | | |


use Glooby\TaskBundle\Annotation\Schedule;

/
 * @Schedule("@hourly")
 */
class PingTask implements TaskInterface
{

params

The params that should be used when calling


use Glooby\TaskBundle\Annotation\Schedule;

/
* @Schedule("@weekly", params={"wash": true, "flush": 500})
*/
class CityImporter implements TaskInterface
{

active

Phe active parameter tells if the schedule should be active or not, default=true


use Glooby\TaskBundle\Annotation\Schedule;

/
@Schedule("/6", active=false)
*/
class PingTask implements TaskInterface
{

Sync schedules to the database, this has to be run after each update


bin/console scheduler:run

Running the Tests

Install the dependencies:

$ script/bootstrap

Then, run the test suite:

$ script/test

Contributing

See CONTRIBUTING file.

License

This bundle is released under the MIT license. See the complete license in the bundle: LICENSE.md

www.glooby.com www.glooby.se


  Files folder image Files (52)  
File Role Description
Files folder imagebin (2 files)
Files folder imagescript (4 files)
Files folder imagesrc (1 file, 12 directories)
Accessible without login Plain text file .coveralls.yml Data Auxiliary data
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file contributors.txt Doc. Documentation
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (52)  /  bin  
File Role Description
  Accessible without login Plain text file composer Data Auxiliary data
  Accessible without login Plain text file phpunit Data Auxiliary data

  Files folder image Files (52)  /  script  
File Role Description
  Accessible without login Plain text file bootstrap Data Auxiliary data
  Accessible without login Plain text file setperm Data Auxiliary data
  Accessible without login Plain text file test Data Auxiliary data
  Accessible without login Plain text file updatedeps Data Auxiliary data

  Files folder image Files (52)  /  src  
File Role Description
Files folder imageAnnotation (1 file)
Files folder imageCommand (2 directories)
Files folder imageDependencyInjection (2 files, 1 directory)
Files folder imageEntity (4 files)
Files folder imageManager (1 file)
Files folder imageModel (4 files)
Files folder imageQueue (4 files)
Files folder imageResources (1 directory)
Files folder imageSchedule (1 file)
Files folder imageSynchronizer (1 file)
Files folder imageTask (5 files)
Files folder imageTests (3 directories)
  Plain text file GloobyTaskBundle.php Class Class source

  Files folder image Files (52)  /  src  /  Annotation  
File Role Description
  Plain text file Schedule.php Class Class source

  Files folder image Files (52)  /  src  /  Command  
File Role Description
Files folder imageScheduler (3 files)
Files folder imageTask (1 file)

  Files folder image Files (52)  /  src  /  Command  /  Scheduler  
File Role Description
  Plain text file PruneCommand.php Class Class source
  Plain text file RunCommand.php Class Class source
  Plain text file SyncCommand.php Class Class source

  Files folder image Files (52)  /  src  /  Command  /  Task  
File Role Description
  Plain text file RunCommand.php Class Class source

  Files folder image Files (52)  /  src  /  DependencyInjection  
File Role Description
Files folder imageCompiler (1 file)
  Plain text file Configuration.php Class Class source
  Plain text file GloobyTaskExtension.php Class Class source

  Files folder image Files (52)  /  src  /  DependencyInjection  /  Compiler  
File Role Description
  Plain text file RegisterSchedulesPass.php Class Class source

  Files folder image Files (52)  /  src  /  Entity  
File Role Description
  Plain text file QueuedTask.php Class Class source
  Plain text file QueuedTaskRepository.php Class Class source
  Plain text file Schedule.php Class Class source
  Plain text file ScheduleRepository.php Class Class source

  Files folder image Files (52)  /  src  /  Manager  
File Role Description
  Plain text file TaskManager.php Class Class source

  Files folder image Files (52)  /  src  /  Model  
File Role Description
  Plain text file QueuedTask.php Class Class source
  Plain text file QueuedTaskInterface.php Class Class source
  Plain text file Schedule.php Class Class source
  Plain text file ScheduleInterface.php Class Class source

  Files folder image Files (52)  /  src  /  Queue  
File Role Description
  Plain text file QueueMonitor.php Class Class source
  Plain text file QueueProcessor.php Class Class source
  Plain text file QueuePruner.php Class Class source
  Plain text file QueueScheduler.php Class Class source

  Files folder image Files (52)  /  src  /  Resources  
File Role Description
Files folder imageconfig (1 file)

  Files folder image Files (52)  /  src  /  Resources  /  config  
File Role Description
  Accessible without login Plain text file services.yml Data Auxiliary data

  Files folder image Files (52)  /  src  /  Schedule  
File Role Description
  Plain text file ScheduleRegistry.php Class Class source

  Files folder image Files (52)  /  src  /  Synchronizer  
File Role Description
  Plain text file ScheduleSynchronizer.php Class Class source

  Files folder image Files (52)  /  src  /  Task  
File Role Description
  Plain text file OutputAwareInterface.php Class Class source
  Plain text file PingTask.php Class Class source
  Plain text file QueuedTaskAwareInterface.php Class Class source
  Plain text file TaskInterface.php Class Class source
  Plain text file TaskRunner.php Class Class source

  Files folder image Files (52)  /  src  /  Tests  
File Role Description
Files folder imageAnnotation (1 file)
Files folder imageDependencyInjection (2 files, 2 directories)
Files folder imageSchedule (1 file)

  Files folder image Files (52)  /  src  /  Tests  /  Annotation  
File Role Description
  Plain text file ScheduleTest.php Class Class source

  Files folder image Files (52)  /  src  /  Tests  /  DependencyInjection  
File Role Description
Files folder imageComplier (1 file)
Files folder imagefixtures (1 file)
  Plain text file ConfigurationTest.php Class Class source
  Plain text file GloobyTaskExtensionTest.php Class Class source

  Files folder image Files (52)  /  src  /  Tests  /  DependencyInjection  /  Complier  
File Role Description
  Plain text file RegisterSchedulesPassTest.php Class Class source

  Files folder image Files (52)  /  src  /  Tests  /  DependencyInjection  /  fixtures  
File Role Description
  Accessible without login Plain text file config.yml Data Auxiliary data

  Files folder image Files (52)  /  src  /  Tests  /  Schedule  
File Role Description
  Plain text file ScheduleRegistryTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:68
This week:0
All time:10,325
This week:673Up