|
7 | 7 |
|
8 | 8 | # php-parallel-processes |
9 | 9 |
|
10 | | -Work in progress ;) |
| 10 | +Execute processes in parallel. |
| 11 | + |
| 12 | +Examples of use: start your environment, CI tools... |
11 | 13 |
|
12 | 14 | # Installation |
13 | 15 |
|
| 16 | +## Use official Docker image |
| 17 | + |
| 18 | +You can use the official Docker image to not install anything: |
| 19 | +[steevanb/php-parallel-processes:x.y.z](https://hub.docker.com/r/steevanb/php-parallel-processes/tags). |
| 20 | + |
| 21 | +Example: |
| 22 | +```bash |
| 23 | +docker \ |
| 24 | + run \ |
| 25 | + --rm \ |
| 26 | + --tty \ |
| 27 | + --interactive \ |
| 28 | + --volume "$(pwd)":"$(pwd)" \ |
| 29 | + --workdir "$(pwd)" \ |
| 30 | + steevanb/php-parallel-processes:x.y.z \ |
| 31 | + php parallel-processes.php |
| 32 | +``` |
| 33 | + |
| 34 | +### If your processes use docker |
| 35 | + |
| 36 | +If processes in `parallel-processes.php` use Docker, you have to add a volume on your Docker socket: |
| 37 | +```bash |
| 38 | +--volume /var/run/docker.sock:/var/run/docker.sock |
14 | 39 | ``` |
| 40 | + |
| 41 | +All official `php-parallel-processes` images have Docker and Docker compose installed, so you only need to add a volume on the socket. |
| 42 | + |
| 43 | +### alpine, bookworm and buster |
| 44 | + |
| 45 | +3 Docker images are provided for each `php-parallel-processes` version, use the one you want depending on your needs: |
| 46 | + * `alpine`: smaller version, but could be "too much simple" sometimes |
| 47 | + * `buster`: middle version, contains almost everything needed |
| 48 | + * `bookworm`: larger version, should contain what you need |
| 49 | + |
| 50 | +## Install as Composer dependency |
| 51 | + |
| 52 | +If you want to add `php-parallel-processes` directly in your project: |
| 53 | + |
| 54 | +```bash |
15 | 55 | composer require steevanb/php-parallel-processes ^1.0 |
16 | 56 | ``` |
17 | 57 |
|
18 | | -# Usage with Docker |
| 58 | +# Create processes configuration |
19 | 59 |
|
20 | | -If you don't want to install it as dependency, you can use the official Docker image: |
21 | | -[steevanb/php-parallel-processes:x.y.z](https://hub.docker.com/r/steevanb/php-parallel-processes/tags). |
| 60 | +You need to create a configuration for your processes, written in PHP. |
| 61 | + |
| 62 | +Example: [bin/start.php](bin/start.php) |
| 63 | + |
| 64 | +## Basic example |
| 65 | + |
| 66 | +```php |
| 67 | +<?php |
| 68 | + |
| 69 | +declare(strict_types=1); |
| 70 | + |
| 71 | +use Steevanb\ParallelProcess\{ |
| 72 | + Console\Application\ParallelProcessesApplication, |
| 73 | + Process\Process |
| 74 | +}; |
| 75 | +use Symfony\Component\Console\Input\ArgvInput; |
| 76 | + |
| 77 | +# If you use the official Docker image, you should use the Composer global autoload |
| 78 | +require $_ENV['COMPOSER_GLOBAL_AUTOLOAD_FILE_NAME']; |
| 79 | +# If you use the Composer dependency version, you should use your Composer autoload |
| 80 | +require __DIR__ . '/vendor/autoload.php'; |
| 81 | + |
| 82 | +(new ParallelProcessesApplication()) |
| 83 | + ->addProcess((new Process(['first', 'process']))->setName('First process')) |
| 84 | + ->addProcess((new Process(['second', 'process']))->setName('Second process')) |
| 85 | + ->run(new ArgvInput($argv)); |
| 86 | +``` |
| 87 | + |
| 88 | +## Configurations |
| 89 | + |
| 90 | +### Global timeout |
| 91 | + |
| 92 | + |
| 93 | +You can configure the global timeout with `ParallelProcessesApplication::setTimeout()`. |
| 94 | + |
| 95 | +Default value: `null` (no timeout). |
| 96 | + |
| 97 | +```php |
| 98 | +(new ParallelProcessesApplication()) |
| 99 | + // Timeout is in seconds, here we will have a 10s timeout |
| 100 | + ->setTimeout(10) |
| 101 | +``` |
| 102 | + |
| 103 | +### Refresh interval |
| 104 | + |
| 105 | +You can configure the refresh interval, to scan all processes status and start the next ones, |
| 106 | +with `ParallelProcessesApplication::setRefreshInterval()`. |
| 107 | + |
| 108 | +Default value: `10000` (10ms) |
| 109 | + |
| 110 | +```php |
| 111 | +(new ParallelProcessesApplication()) |
| 112 | + // Timeout is in microseconds, here we will have a 500ms timeout |
| 113 | + ->setRefreshInterval(50000) |
| 114 | +``` |
| 115 | + |
| 116 | +### Maximum processes in parallel |
| 117 | + |
| 118 | +You can configure the maximum number of processes in parallel |
| 119 | +with `ParallelProcessesApplication::setMaximumParallelProcesses()`. |
| 120 | + |
| 121 | +Default value: `null` (no maximum) |
| 122 | + |
| 123 | +```php |
| 124 | +(new ParallelProcessesApplication()) |
| 125 | + // Here we will have maximum 3 processes in parallel |
| 126 | + ->setMaximumParallelProcesses(3) |
| 127 | +``` |
| 128 | + |
| 129 | +### Theme |
| 130 | + |
| 131 | +`php-parallel-processes` comes with 2 themes: |
| 132 | + * [default](src/Console/Application/Theme/DefaultTheme.php) |
| 133 | + * Output everything |
| 134 | + * Use verbosity (`-v`, `-vv` or `-vvv`) to add execution time and process outputs |
| 135 | + * Should be used when you need to see live processes status, most of the time ;) |
| 136 | + * [summary](src/Console/Application/Theme/SummaryTheme.php) |
| 137 | + * Output only the start and the end of parallel processes |
| 138 | + * Use verbosity (`-v`, `-vv` or `-vvv`) to add execution time and process outputs |
| 139 | + * Should be used when you don't need to see live processes status, in CI for example |
| 140 | + |
| 141 | +Configure it with `ParallelProcessesApplication::setTheme()`: |
| 142 | +```php |
| 143 | +use Steevanb\ParallelProcess\Console\Application\Theme\DefaultTheme(); |
| 144 | + |
| 145 | +(new ParallelProcessesApplication()) |
| 146 | + ->setTheme(new DefaultTheme()) |
| 147 | +``` |
| 148 | + |
| 149 | +You can also configure it in CLI with `--theme` (CLI override PHP configuration): |
| 150 | +```bash |
| 151 | +php parallel-processes.php --theme summary |
| 152 | +``` |
| 153 | + |
| 154 | +You can create your own theme by implementing [ThemeInterface](src/Console/Application/Theme/ThemeInterface.php). |
0 commit comments