|
| 1 | +# Configuration |
| 2 | + |
| 3 | +## Global configuration |
| 4 | +The plugin allows some simple runtime configuration. |
| 5 | +You may create a file called `app_queue.php` inside your `config` folder (NOT the plugins config folder) to set the following values: |
| 6 | + |
| 7 | +- Seconds to sleep() when no executable job is found: |
| 8 | + |
| 9 | + ```php |
| 10 | + $config['Queue']['sleeptime'] = 10; |
| 11 | + ``` |
| 12 | + |
| 13 | +- Probability in percent of an old job cleanup happening: |
| 14 | + |
| 15 | + ```php |
| 16 | + $config['Queue']['gcprob'] = 10; |
| 17 | + ``` |
| 18 | + |
| 19 | +- Default timeout after which a job is requeued if the worker doesn't report back: |
| 20 | + |
| 21 | + ```php |
| 22 | + $config['Queue']['defaultworkertimeout'] = 1800; |
| 23 | + ``` |
| 24 | + |
| 25 | +- Default number of retries if a job fails or times out: |
| 26 | + |
| 27 | + ```php |
| 28 | + $config['Queue']['defaultworkerretries'] = 3; |
| 29 | + ``` |
| 30 | + |
| 31 | +- Seconds of running time after which the worker will terminate (0 = unlimited): |
| 32 | + |
| 33 | + ```php |
| 34 | + $config['Queue']['workermaxruntime'] = 120; |
| 35 | + ``` |
| 36 | + |
| 37 | + *Warning:* Do not use 0 if you are using a cronjob to permanantly start a new worker once in a while and if you do not exit on idle. |
| 38 | + |
| 39 | +- Seconds of running time after which the PHP process of the worker will terminate (0 = unlimited): |
| 40 | + |
| 41 | + ```php |
| 42 | + $config['Queue']['workertimeout'] = 120 * 100; |
| 43 | + ``` |
| 44 | + |
| 45 | + *Warning:* Do not use 0 if you are using a cronjob to permanently start a new worker once in a while and if you do not exit on idle. This is the last defense of the tool to prevent flooding too many processes. So make sure this is long enough to never cut off jobs, but also not too long, so the process count stays in manageable range. |
| 46 | + |
| 47 | +- Should a worker process quit when there are no more tasks for it to execute (true = exit, false = keep running): |
| 48 | + |
| 49 | + ```php |
| 50 | + $config['Queue']['exitwhennothingtodo'] = false; |
| 51 | + ``` |
| 52 | + |
| 53 | +- Minimum number of seconds before a cleanup run will remove a completed task (set to 0 to disable): |
| 54 | + |
| 55 | + ```php |
| 56 | + $config['Queue']['cleanuptimeout'] = 2592000; // 30 days |
| 57 | + ``` |
| 58 | + |
| 59 | +- Max workers (per server): |
| 60 | + |
| 61 | + ```php |
| 62 | + $config['Queue']['maxworkers'] = 3 // Defaults to 1 (single worker can be run per server) |
| 63 | + ``` |
| 64 | + |
| 65 | +- Multi-server setup: |
| 66 | + |
| 67 | + ```php |
| 68 | + $config['Queue']['multiserver'] = true // Defaults to false (single server) |
| 69 | + ``` |
| 70 | + |
| 71 | + For multiple servers running either CLI/web separately, or even multiple CLI workers on top, make sure to enable this. |
| 72 | + |
| 73 | +- Use a different connection: |
| 74 | + |
| 75 | + ```php |
| 76 | + $config['Queue']['connection'] = 'custom'; // Defaults to 'default' |
| 77 | + ``` |
| 78 | + |
| 79 | +- Ignore certain task classes to they don't end up in the generated SQL query. Can be used to filter out the example tasks classes shipped with the plugin, if you're not using them: |
| 80 | + |
| 81 | + ```php |
| 82 | + $config['Queue']['ignoredTasks'] = [ |
| 83 | + 'Queue\Queue\Task\CostsExampleTask', |
| 84 | + 'Queue\Queue\Task\EmailTask', |
| 85 | + 'Queue\Queue\Task\ExampleTask', |
| 86 | + 'Queue\Queue\Task\ExceptionExampleTask', |
| 87 | + 'Queue\Queue\Task\ExecuteTask', |
| 88 | + 'Queue\Queue\Task\MonitorExampleTask', |
| 89 | + 'Queue\Queue\Task\ProgressExampleTask', |
| 90 | + 'Queue\Queue\Task\RetryExampleTask', |
| 91 | + 'Queue\Queue\Task\SuperExampleTask', |
| 92 | + 'Queue\Queue\Task\UniqueExampleTask', |
| 93 | + ]; // Defaults to [] |
| 94 | + ``` |
| 95 | + |
| 96 | +Don't forget to load that config file with `Configure::load('app_queue');` in your bootstrap. |
| 97 | +You can also use `$this->addPlugin('Queue', ['bootstrap' => true]);` which will load your `app_queue.php` config file automatically. |
| 98 | + |
| 99 | +Example `app_queue.php`: |
| 100 | + |
| 101 | +```php |
| 102 | +return [ |
| 103 | + 'Queue' => [ |
| 104 | + 'workermaxruntime' => 60, |
| 105 | + 'sleeptime' => 15, |
| 106 | + ], |
| 107 | +]; |
| 108 | +``` |
| 109 | + |
| 110 | +You can also drop the configuration into an existing config file (recommended) that is already been loaded. |
| 111 | +The values above are the default settings which apply, when no configuration is found. |
| 112 | + |
| 113 | +### Serializer strategy |
| 114 | +By default, the payload data array will be serialized using PHPs native object serializer. |
| 115 | +It is recommended to switch to JSON serializing instead, if you don't need to send any actual objects, but only primitive values and arrays. |
| 116 | +```php |
| 117 | +'Queue' => [ |
| 118 | + ... |
| 119 | + 'serializerClass' => \Queue\Utility\JsonSerializer::class, |
| 120 | + 'serializerConfig' => [...], |
| 121 | +], |
| 122 | +``` |
| 123 | +This is usually also safer from data perspective. |
| 124 | +Any update of your code would not break the process here for queued tasks as no objects could be outdated in serialized payload. |
| 125 | + |
| 126 | +You can also use a custom one as long as it implements the `Queue\Utility\SerializerInterface`. |
| 127 | + |
| 128 | +Note: When using JSON one, make sure to use Email and Mailer tasks only with JSON safe way (not passing actual objects). |
| 129 | + |
| 130 | +### Backend configuration |
| 131 | + |
| 132 | +- isSearchEnabled: Set to false if you do not want search/filtering capability. |
| 133 | + This is auto-detected based on [Search](https://github.com/FriendsOfCake/search) plugin being available/loaded if not disabled. |
| 134 | + |
| 135 | +- isStatsEnabled: Set to true to enable. This requires [chart.js](https://github.com/chartjs/Chart.js) asset to be available. |
| 136 | + You can also overwrite the template and as such change the asset library as well as the output/chart. |
| 137 | + |
| 138 | + |
| 139 | +### Configuration tips |
| 140 | + |
| 141 | +For the beginning maybe use not too many runners in parallel, and keep the runtimes rather short while starting new jobs every few minutes. |
| 142 | +You can then always increase spawning of runners if there is a shortage. |
| 143 | + |
| 144 | +## Task configuration |
| 145 | + |
| 146 | +You can set two main things on each task as property: timeout and retries. |
| 147 | +```php |
| 148 | + /** |
| 149 | + * Timeout for this task in seconds, after which the task is reassigned to a new worker. |
| 150 | + * |
| 151 | + * @var int |
| 152 | + */ |
| 153 | + public $timeout = 120; |
| 154 | + |
| 155 | + /** |
| 156 | + * Number of times a failed instance of this task should be restarted before giving up. |
| 157 | + * |
| 158 | + * @var int |
| 159 | + */ |
| 160 | + public $retries = 1; |
| 161 | +``` |
| 162 | +Make sure you set the timeout high enough so that it could never run longer than this, otherwise you risk it being re-run while still being run. |
| 163 | +It is recommended setting it to at least 2x the maximum possible execution length. See [Concurrent workers](limitations.md) |
| 164 | + |
| 165 | +Set the retries to at least 1, otherwise it will never execute again after failure in the first run. |
0 commit comments