2017 © Pedro Peláez
 

symfony-bundle task-scheduler-bundle

image

habu/task-scheduler-bundle

  • Tuesday, May 16, 2017
  • by rknol_mi24
  • Repository
  • 1 Watchers
  • 0 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

TaskSchedulerBundle

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Latest Unstable Version License, (*1)

PHP task scheduler/deferred task execution bundle for Symfony 3.3+ and PHP 7.0+, (*2)

Installation

Install dependency into your project

Install with Composer:, (*3)

$ composer require habuio/task-scheduler-bundle

Enable bundle in your kernel

Open your app/AppKernel.php and add it:, (*4)

// [...]

    public function registerBundles()
    {
        $bundles = [
            /// [...]
            new Habu\TaskSchedulerBundle\TaskSchedulerBundle(),
        ];

        /// [...]

        return $bundles;
    }

// [...]

Usage

Create task service class

Implementation wise, the bundle is designed to be completely transparent in the way you write your code., (*5)

Each task service looks like any other Symfony service you'd write, and you can call the methods on it like you can on any other., (*6)

Create a new file, for example AppBundle/Task/MathTask.php:, (*7)

<?php

namespace AppBundle\Task;

use Habu\TaskSchedulerBundle\Task;

class MathTask extends Task
{
    public function add($a, $b)
    {
        return $a + $b;
    }
}

Define Symfony service definition

Now, let's open your bundle's services.yml to define a service definition for our task:, (*8)

services:
    # [...]
    app.tasks.math:
        class: AppBundle\Task\MathTask
        tags: ['task_scheduler.task']

Note the task_scheduler.task service tag - this enables the service to be executed inside our task worker, (*9)

Produce a task

Now that we have everything set up, let's delay execution of a task to a background worker., (*10)

Why don't we do it in a Controller (Controller/DefaultController.php):, (*11)

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        $task = $this->get('app.tasks.math');

        // Defer execution of the task to a background worker.
        $ref = $task->add->delay(2, 2);

        // Schedule a task to be executed at a specific moment in time
        $task->add->schedule(new \DateTime('2018-01-01 00:00:00'), 5, 5);

        // Halt execution of the application until the worker
        // finishes processing the task and yields the result.
        var_dump($ref->get()); exit;
    }
}

As you can see, our task service has this magic method delay on top of our pre-existing service methods, that we called to defer execution to a background worker, as well as schedule, which allows task execution to be deferred until a specific moment in time., (*12)

Calling methods such as delay and schedule will return you with a ReferenceInterface object:, (*13)

interface ReferenceInterface
{
    /**
     * Wait for, and return the result of deferred executed
     * task method.
     *
     * @return mixed
     */
    public function get();

    /**
     * Calling this method blocks execution of application
     * flow until the execution of associated deferred task
     * has been completed, and a result is available.
     *
     * @return void
     */
    public function wait();
}

Depending on the producer implementation and bundle configuration, you may use these objects to access the result of background-executed tasks., (*14)

Run the background worker job

TBD, (*15)

The Versions

16/05 2017

dev-master

9999999-dev

  Sources   Download

BSD 3-Clause

The Requires

 

The Development Requires

by Ruben Knol