Download the PHP package vectorial1024/laravel-process-async without Composer
On this page you can find all versions of the php package vectorial1024/laravel-process-async. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vectorial1024/laravel-process-async
More information about vectorial1024/laravel-process-async
Files in vectorial1024/laravel-process-async
Package laravel-process-async
Short Description Utilize Laravel Process to run PHP code asynchronously, as if using Laravel Concurrency.
License MIT
Informations about the package laravel-process-async
laravel-process-async
Utilize Laravel Processes to run PHP code asynchronously, as if using Laravel Concurrency.
What really is this?
Laravel Processes was first introduced in Laravel 10. This library wraps around Process::start() to let you execute code in the background to achieve async, albeit with some caveats:
- You may only execute PHP code
- Restrictions from
opis/closureapply (see their README) - Hands-off execution: no built-in result-checking, check the results yourself (e.g. via database, file cache, etc)
This library internally uses an Artisan command to run the async code, which is similar to Laravel 11 Concurrency.
Why should I want this?
This library is very helpful for these cases:
- You want a cross-platform minimal-setup async for easy vertical scaling
pthreadsis dead (source)parallelis general-purpose and does not have Laravel integration
- You want to start quick-and-dirty async tasks right now (e.g. prefetching resources, pinging remote, etc.)
- Best is if your task only has very few lines of code
- Laravel 11 Concurrency is too limiting; e.g.:
- You want to do something else while waiting for results
- You want to conveniently limit the max (real) execution time of the concurrent tasks
- And perhaps more!
Of course, if you are considering extreme scaling (e.g. Redis queues in Laravel, multi-worker clusters, etc.) or guaranteed task execution, then this library is obviously not for you.
Installation
via Composer:
This library supports Unix and Windows; see the Testing section for more details.
Extra requirements for Unix
If you are on Unix, check that you also have the following:
- GNU Core Utilities (
coreutils)- MacOS do
brew install coreutils! - Other Unix distros should check if
coreutilsis preinstalled
- MacOS do
Change log
Please see CHANGELOG.md.
Example code and features
Tasks can be defined as PHP closures, or (recommended) as an instance of a class that implements AsyncTaskInterface.
A very simple example task to write Hello World to a file:
Task time limits
You can set task time limits before you start them, but you cannot change them after the tasks are started. When the time limit is reached, the async task is killed.
The default time limit is 30 real seconds. You can also choose to not set any time limit, in this case the (CLI) PHP max_execution_time directive will control the time limit.
Note: AsyncTaskInterface contains an implementable method handleTimeout for you to define timeout-related cleanups (e.g. write to some log that the task has timed out). This method is still called when the PHP max_execution_time directive is triggered.
Some tips:
- Don't sleep too long! On Windows, timeout handlers cannot trigger while your task is sleeping.
- Use short but frequent sleeps instead.
- Avoid using
SIGINT! On Unix, this signal is reserved for timeout detection.
Task IDs
You can assign task IDs to tasks before they are run, but you cannot change them after the tasks are started. This allows you to track the statuses of long-running tasks across web requests.
By default, if a task does not has its user-specified task ID when starting, a ULID will be generated as its task ID.
Some tips:
- Task IDs can be optional (i.e.
null) but CANNOT be blank (i.e."")! - If multiple tasks are started with the same task ID, then the task status object will only track the first task that was started
- Known issue: on Windows, checking task statuses can be slow (about 0.5 - 1 seconds) due to underlying bottlenecks
Securing the task runners
The way this library works means that attackers (or other unwanted parties) may simply craft malicious commands that mimic legitimate usage of this library.
To secure the task runners from being started illegitimately, you may configure the .env file to contain the following key:
You may need to clear your Laravel optimisation cache after changing this value.
The contents of the async tasks will be signed by this secret key, so that this library can know whether the tasks are started by this library itself or someone else.
Fake Objects
Fake objects are available for users to simulate async task behaviors without actually starting async tasks. This should be helpful when writing tests that attempts to react to task statuses.
The following fake objects are available:
FakeAsyncTask: a fake ofAsyncTaskFakeAsyncTaskStatus: a fake ofAsyncTaskStatus
Notably, they can be used like this:
Testing
PHPUnit via Composer script:
| Latest cross-platform testing results: | Runtime | MacOS | Ubuntu | Windows |
|---|---|---|---|---|
| Laravel 10 (PHP 8.1) | skipped* | skipped* | skipped* | |
| Laravel 11 (PHP 8.2) | ||||
| Laravel 12 (PHP 8.3) | ||||
| Laravel 13 (PHP ???) | 🛠️ | 🛠️ | 🛠️ |
*Note: tests for these Laravel versions are skipped because they have old artisan file contents:
- It is difficult to mock multi-version
artisanfiles for different Laravel versions (see https://github.com/Vectorial1024/laravel-process-async/issues/6). - It is rare for the
artisanfile at Laravel to be updated - The actual behavior is expected to be the same.
All versions of laravel-process-async with dependencies
illuminate/support Version ^10.0|^11.0|^12.0
loophp/phposinfo Version ^1.8
opis/closure Version ^4.0