badpixxel / tasking-bundle
Advanced Tasking Bundle for Symfony Applications (100% PHP)
Package info
gitlab.com/badpixxel-public/Tasking-Bundle
Type:symfony-bundle
pkg:composer/badpixxel/tasking-bundle
Requires
- php: ^8.1
- ext-curl: *
- ext-pcntl: *
- ext-posix: *
- doctrine/doctrine-bundle: ^2.0|^3.0
- monolog/monolog: ^3.0
- sonata-project/admin-bundle: ^4.0
- sonata-project/doctrine-orm-admin-bundle: ^4.0
- symfony/asset-mapper: ^6.4|^7.4|^8.0
- symfony/config: ^6.4|^7.4|^8.0
- symfony/console: ^6.4|^7.4|^8.0
- symfony/dependency-injection: ^6.4|^7.4|^8.0
- symfony/event-dispatcher: ^6.4|^7.4|^8.0
- symfony/http-kernel: ^6.4|^7.4|^8.0
- symfony/monolog-bundle: ^3.0|^4.0
- symfony/options-resolver: ^6.4|^7.4|^8.0
- webmozart/assert: ^1.0
Requires (Dev)
- badpixxel/paddock-core: ^5.0
- badpixxel/php-sdk: ^3.0
- sentry/sentry-symfony: ^4.0|^5.0
- symfony/debug-bundle: ^6.4|^7.4|^8.0
- symfony/flex: ^1.0|^2.0
- symfony/framework-bundle: ^6.4|^7.4|^8.0
- symfony/phpunit-bridge: ^6.4|^7.4|^8.0
- symfony/runtime: ^6.4|^7.4|^8.0
- symfony/stopwatch: ^6.4|^7.4|^8.0
- symfony/twig-bundle: ^6.4|^7.4|^8.0
- symfony/ux-live-component: ^2.0
- symfony/web-profiler-bundle: ^6.4|^7.4|^8.0
- twig/extra-bundle: >=3.0
- twig/intl-extra: >=3.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Advanced Tasking Manager for Symfony. 100% Php with high concurrency management.
Key features
- Background tasking: create jobs to be run by workers (Symfony commands)
- 100% PHP, works on any Linux Server with PHP7.2+
- High concurrency management: use jobs token to ensure task is executed by a single process.
- Multi-server compatible: tokens are stored in a central SQL Table.
- Optimized memory footprint: create a dedicated Symfony environnement to reduce memory impacts.
Installation
Step 1 - Requirements and Installing the bundle
The first step is to tell composer that you want to download Tasking-Bundle which can be achieved by typing the following at the command prompt:
composer require badpixxel/tasking-bundle
Step 2 - Enable the bundle in your kernel
The bundle must be added to your AppKernel.
**Step usually not necessary in Symfony Flex.
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new \BadPixxel\Tasking\BadpixxelTaskingBundle(),
// ...
);
}
Create Your First Job
Background jobs must extend BadPixxel\Tasking\Model\AbstractJob.
use BadPixxel\Tasking\Model\AbstractJob;
class MyJob extends AbstractJob
{
/** @return bool */
public function execute() : bool
{
// Execute your background operations
// ...
return true;
}
}
Job Token may be defined multiple way:
use BadPixxel\Tasking\Model\AbstractJob;
class MyJob extends AbstractJob
{
/** You can set it directly by overriding this constant */
protected $token = "";
/**
* Or by writing an array of parameters to setToken()
* @param array $parameters
* @return self
*/
public function setup(array $parameters): self
{
//====================================================================//
// Setup Job Token
$this->setToken($parameters);
return $this;
}
}
Available Job Types
There are few predefined abstract job types, for different kinds of tasks:
- BadPixxel\Tasking\Model\AbstractJob: a single simple task, executed once by job class.
- BadPixxel\Tasking\Model\AbstractServiceJob: execute a Symfony service action with given parameters
- BadPixxel\Tasking\Model\AbstractStaticJob: a simple task, executed & repeated every XX minutes.
- BadPixxel\Tasking\Model\AbstractBatchJob: step-by-step, execute multiple tasks inside a single job.
Long Running Tasks
A running task is considered as faulty, and replayed, when it was not seen alive for longer than the
tasks error delay. Its worker is considered as dead when it stays silent longer than
watchdog_delay + refresh_delay.
Any code running inside a task (job, service, listener) can signal the task is still working by
dispatching a TaskKeepAliveEvent, without depending on the tasking services:
use BadPixxel\Tasking\Events\TaskKeepAliveEvent;
// Request at least 60 more seconds (defaults to watchdog_delay)
$event = $dispatcher->dispatch(new TaskKeepAliveEvent(60));
if (!$event->isAccepted()) {
// Task token is about to expire: stop working & let the task be replayed
}
Each signal resets the PHP time limit and marks the task & its worker as alive in database
(throttled to one write per refresh_delay). Lifetime is capped by the task token lifetime.
Outside a running task, dispatching this event is a no-op.
Note: this feature adds a LastAliveAtTimeStamp column to the tasks table, update your database schema.
Symfony Commands
The bundle comes with management commands to pilot workers from command line.
tasking:check Tasking Service : Check Supervisor Process is Running on Current Machines
tasking:start Tasking Service : Start All Supervisors & Workers Process on All Machines
tasking:status Tasking Service : Check Status of Tasking Services
tasking:stop Tasking Service : Stop All Supervisors & Workers Process on All Machines
tasking:supervisor Run a Supervisor Worker Process
tasking:worker Run a Tasking Worker Process
Note: Tasking processes & supervisor are activated & checked each time a new task is added to queue
Configuration reference
Bundle configuration are stored under badpixxel_tasking:
badpixxel_tasking:
entity_manager: default // Name of Doctrine Entity Manager to use for Tasks & Token Storage
environement: prod // Symfony Environnement to use for workers
refresh_delay: 3 // Delay for workers status refresh
watchdog_delay: 30 // Watchdog delay for tasks execution
multiserver: false // Enable multiserver mode
multiserver_path: '' // Url for remote servers checks
server:
force_crontab: false // Use crontab to ensure supervisor is running (Useless if you uses 3+ workers)
php_version: php // Bash comamnd for php
supervisor:
max_age: 3600 // Time to live of supervisor process, if reached, process will die
refresh_delay: 500 // Delay between two worker refresh
max_workers: 3 // Number of worker to use
max_memory: 100 // Max. Memory, if reached, process will die
workers:
max_tasks: 100 // Max. number of jobs to execute, if reached, process will die
max_age: 120 // Time to live of a worker process, if reached, process will die
max_memory: 200 // Max. Memory, if reached, process will die
tasks:
max_age: 180 // Time to live of a finished task in database
try_count: 5 // Number of failed attemps for a task
try_delay: 120 // Delay before retry of a failed task
static: // Key => Class values for Static Jobs
myStaticJob: AppBundle\Jobs\MyStaticJob
Docker Dev Environnement
A Docker Compose file is available to run a development server. You can start it typing the following at the command prompt:
docker-compose up -d
Testing & Code Quality
This bundle uses Phpunit for functional testing.
docker-compose exec app php vendor/bin/phpunit
This bundle uses Grumphp for all code quality checks (PHPMD, PhpCsFixer, PhpStan, and more...).
docker-compose exec app php vendor/bin/grumphp run
License
This package is available under the MIT license.

