dev-master
9999999-dev
BSD 3-Clause
The Requires
The Development Requires
by Ruben Knol
Wallogit.com
2017 © Pedro Peláez
PHP task scheduler/deferred task execution bundle for Symfony 3.3+ and PHP 7.0+, (*2)
Install with Composer:, (*3)
$ composer require habuio/task-scheduler-bundle
Open your app/AppKernel.php and add it:, (*4)
// [...]
public function registerBundles()
{
$bundles = [
/// [...]
new Habu\TaskSchedulerBundle\TaskSchedulerBundle(),
];
/// [...]
return $bundles;
}
// [...]
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;
}
}
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)
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)
TBD, (*15)
BSD 3-Clause