lukaszaleckas / laravel-concurrency
Installs: 9
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/lukaszaleckas/laravel-concurrency
Requires
- php: ^8.1
- ext-json: *
- ext-pcntl: *
- laravel/framework: ^10.0
Requires (Dev)
- mockery/mockery: ^1.5
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^9.5
- slevomat/coding-standard: ^6.4
- squizlabs/php_codesniffer: ^3.6
README
Installation
- Run:
composer require lukaszaleckas/laravel-concurrency
Service provider should be automatically registered, if not add
LaravelConcurrency\ConcurrencyServiceProvider::class
to your application's app.php.
- Publish configuration file:
php artisan vendor:publish --provider="LaravelConcurrency\ConcurrencyServiceProvider"
- Run migrations to create
taskstable:
php artisan migrate
Usage Example
Create a task
Create a new task which implements LaravelConcurrency\Contracts\TaskInterface:
use LaravelConcurrency\Contracts\TaskInterface; class SleepForTwoSeconds implements TaskInterface { public function run(): mixed { sleep(2); return null; } }
Tasks return result which you want to get after running them. This can be anything you want, but it needs to be serializable.
In this example we just return null.
Spawn task workers
Tasks are run by tasks workers. To spawn a task worker run:
php artisan concurrency:run-task-worker
One worker can run only one task at a time. So we'll spawn two of those for this example.
Run tasks
Now run a couple of these tasks concurrently:
use LaravelConcurrency\Facades\Concurrency; Concurrency::wait( new SleepForTwoSeconds(), new SleepForTwoSeconds() )
If you notice, these tasks were both completed in ~2s, which means they've run concurrently.
You should also notice task workers logs that indicate which task ran in which process.