|
1 | 1 | # Tasking-Bundle |
2 | 2 |
|
3 | | -Advanced Tasking Manager for Symfony. 100% Php with Tasks concurrency management. |
| 3 | +Advanced Tasking Manager for Symfony. 100% Php with high concurrency management. |
4 | 4 |
|
5 | 5 | Key features |
6 | | -- 100% PHP, works on any Lunix Server with PHP7 only (TEsted on Jelastic) |
7 | | -- Background tasks concurrency management. Use tokens to prevent tasks being started twice. |
8 | | -- Multi-server Cluster compatible. |
9 | | -- Optimized memory footprint. |
| 6 | +- Background tasking: create jobs to be run by workers (Symfony commands) |
| 7 | +- 100% PHP, works on any Linux Server with PHP7.2+ |
| 8 | +- High concurrency management: use jobs token to ensure task is executed by a single process. |
| 9 | +- Multi-server compatible: tokens are stored in a central SQL Table. |
| 10 | +- Optimized memory footprint: create a dedicated Symfony environnement to reduce memory impacts. |
| 11 | + |
| 12 | +[](https://travis-ci.org/SplashSync/Tasking-Bundle) |
| 13 | +[](https://packagist.org/packages/splash/tasking-bundle) |
| 14 | +[](https://packagist.org/packages/splash/tasking-bundle) |
10 | 15 |
|
11 | 16 | ## Installation |
12 | 17 |
|
13 | | -Check out Symfony documentation [here](http://symfony.com/doc/current/bundles/DoctrineBundle/installation.html). |
| 18 | +#### Step 1 - Requirements and Installing the bundle |
| 19 | +The first step is to tell composer that you want to download Tasking-Bundle which can |
| 20 | +be achieved by typing the following at the command prompt: |
| 21 | + |
| 22 | +```bash |
| 23 | +composer require splash/tasking-bundle |
| 24 | +``` |
| 25 | + |
| 26 | +#### Step 2 - Enable the bundle in your kernel |
| 27 | + |
| 28 | +The bundle must be added to your `AppKernel`. |
| 29 | + |
| 30 | +**Step usually not necescary in Symfony 4**. |
| 31 | + |
| 32 | +```php |
| 33 | +// app/AppKernel.php |
| 34 | + |
| 35 | +public function registerBundles() |
| 36 | +{ |
| 37 | + return array( |
| 38 | + // ... |
| 39 | + new Splash\Tasking\SplashTaskingBundle(), |
| 40 | + // ... |
| 41 | + ); |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +## Create Your First Job |
| 46 | + |
| 47 | +Background jobs must extend [Splash\Tasking\Model\AbstractJob](https://github.com/SplashSync/Tasking-Bundle/blob/master/src/Model/AbstractJob.php). |
| 48 | + |
| 49 | +```php |
| 50 | +use Splash\Tasking\Model\AbstractJob; |
| 51 | + |
| 52 | +class MyJob extends AbstractJob |
| 53 | +{ |
| 54 | + /** @return bool */ |
| 55 | + public function execute() : bool |
| 56 | + { |
| 57 | + // Execute your background operations |
| 58 | + // ... |
| 59 | + return true; |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +Job Token may be defined multiple way: |
| 65 | + |
| 66 | +```php |
| 67 | +use Splash\Tasking\Model\AbstractJob; |
| 68 | + |
| 69 | +class MyJob extends AbstractJob |
| 70 | +{ |
| 71 | + /** You can set it directly by overriding this constant */ |
| 72 | + protected $token = ""; |
| 73 | + |
| 74 | + /** |
| 75 | + * Or by writing an array of parameters to setToken() |
| 76 | + * @param array $parameters |
| 77 | + * @return self |
| 78 | + */ |
| 79 | + public function setup(array $parameters): self |
| 80 | + { |
| 81 | + //====================================================================// |
| 82 | + // Setup Job Token |
| 83 | + $this->setToken($parameters); |
| 84 | + |
| 85 | + return $this; |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | +## Available Job Types |
| 90 | + |
| 91 | +There are few predefined abstract job types, for different kinds of tasks: |
| 92 | +- Splash\Tasking\Model\AbstractJob: a single simple task, executed once by job class. |
| 93 | +- Splash\Tasking\Model\AbstractServiceJob: execute a Symfony service action with given parameters |
| 94 | +- Splash\Tasking\Model\AbstractStaticJob: a simple task, executed & repeated every XX minutes. |
| 95 | +- Splash\Tasking\Model\AbstractBatch: step-by-step, execute multiple tasks inside a single job. |
| 96 | + |
| 97 | +## Symfony Commands |
| 98 | + |
| 99 | +The bundle comes with management commands to pilot workers from command line. |
| 100 | +```bash |
| 101 | +tasking:check Tasking Service : Check Supervisor Process is Running on Current Machines |
| 102 | +tasking:start Tasking Service : Start All Supervisors & Workers Process on All Machines |
| 103 | +tasking:status Tasking Service : Check Status of Tasking Services |
| 104 | +tasking:stop Tasking Service : Stop All Supervisors & Workers Process on All Machines |
| 105 | +tasking:supervisor Run a Supervisor Worker Process |
| 106 | +tasking:worker Run a Tasking Worker Process |
| 107 | +``` |
| 108 | + |
| 109 | +**Note: Tasking processes & supervisor are activated & checked each time a new task is added to queue** |
| 110 | + |
| 111 | +## Configuration reference |
| 112 | + |
| 113 | +Bundle configuration are stored under **splash_tasking**: |
| 114 | + |
| 115 | +```yaml |
| 116 | +splash_tasking: |
| 117 | + entity_manager: default // Name of Doctrine Entity Manager to use for Tasks & Token Storage |
| 118 | + environement: prod // Symfony Environnement to use for workers |
| 119 | + refresh_delay: 3 // Delay for workers status refresh |
| 120 | + watchdog_delay: 30 // Watchdog delay for tasks execution |
| 121 | + multiserver: false // Enable multiserver mode |
| 122 | + multiserver_path: '' // Url for remote servers checks |
| 123 | + server: |
| 124 | + force_crontab: false // Use crontab to ensure supervisor is running (Useless if you uses 3+ workers) |
| 125 | + php_version: php // Bash comamnd for php |
| 126 | + supervisor: |
| 127 | + max_age: 3600 // Time to live of supervisor process, if reached, process will die |
| 128 | + refresh_delay: 500 // Delay between two worker refresh |
| 129 | + max_workers: 3 // Number of worker to use |
| 130 | + max_memory: 100 // Max. Memory, if reached, process will die |
| 131 | + workers: |
| 132 | + max_tasks: 100 // Max. number of jobs to execute, if reached, process will die |
| 133 | + max_age: 120 // Time to live of a worker process, if reached, process will die |
| 134 | + max_memory: 200 // Max. Memory, if reached, process will die |
| 135 | + tasks: |
| 136 | + max_age: 180 // Time to live of a finished task in database |
| 137 | + try_count: 5 // Number of failed attemps for a task |
| 138 | + try_delay: 120 // Delay before retry of a failed task |
| 139 | + error_delay: 40 // Delay to consider a started task as failed |
| 140 | + static: // Key => Class values for Static Jobs |
| 141 | + myStaticJob: AppBundle\Jobs\MyStaticJob |
| 142 | +``` |
| 143 | +
|
| 144 | +## Docker Dev Environnement |
| 145 | +
|
| 146 | +A Docker Compose file is available to run a development server. |
| 147 | +You can start it typing the following at the command prompt: |
| 148 | +
|
| 149 | +```bash |
| 150 | +docker-compose up -d |
| 151 | +``` |
| 152 | + |
| 153 | +## Testing & Code Quality |
14 | 154 |
|
15 | | -## Documentation |
| 155 | +This bundle uses Phpunit for functional testing. |
| 156 | +```bash |
| 157 | +docker-compose exec app php vendor/bin/phpunit |
| 158 | +``` |
16 | 159 |
|
17 | | -Check out the documentation [here](https://github.com/SplashSync/Tasking-Bundle/tree/master/Resources/docs). |
| 160 | +This bundle uses Grumphp for all code quality checks (PHPMD, PhpCsFixer, PhpStan, and more...). |
| 161 | +```bash |
| 162 | +docker-compose exec app php vendor/bin/grumphp run |
| 163 | +``` |
18 | 164 |
|
19 | 165 | ## License |
20 | 166 |
|
21 | | -This package is available under the [MIT license](LICENSE). |
| 167 | +This package is available under the MIT license. |
22 | 168 |
|
0 commit comments