{"id":22900322,"url":"https://github.com/vectorial1024/laravel-process-async","last_synced_at":"2025-05-08T01:11:55.525Z","repository":{"id":265193549,"uuid":"889100869","full_name":"Vectorial1024/laravel-process-async","owner":"Vectorial1024","description":"Utilize Laravel Processes to run PHP code asynchronously, as if using Laravel Concurrency.","archived":false,"fork":false,"pushed_at":"2025-05-07T06:53:33.000Z","size":286,"stargazers_count":4,"open_issues_count":3,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-08T01:11:47.992Z","etag":null,"topics":["async","laravel","multi-processing","php"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/vectorial1024/laravel-process-async","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Vectorial1024.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-11-15T15:59:58.000Z","updated_at":"2025-05-07T06:53:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"b6540148-1a05-4be7-b09c-8601ed05e0b0","html_url":"https://github.com/Vectorial1024/laravel-process-async","commit_stats":null,"previous_names":["vectorial1024/laravel-process-async"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Vectorial1024%2Flaravel-process-async","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Vectorial1024%2Flaravel-process-async/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Vectorial1024%2Flaravel-process-async/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Vectorial1024%2Flaravel-process-async/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Vectorial1024","download_url":"https://codeload.github.com/Vectorial1024/laravel-process-async/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252978779,"owners_count":21834917,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","laravel","multi-processing","php"],"created_at":"2024-12-14T01:19:01.352Z","updated_at":"2025-05-08T01:11:55.517Z","avatar_url":"https://github.com/Vectorial1024.png","language":"PHP","readme":"# laravel-process-async\n[![Packagist License][packagist-license-image]][packagist-url]\n[![Packagist Version][packagist-version-image]][packagist-url]\n[![Packagist Downloads][packagist-downloads-image]][packagist-stats-url]\n[![PHP Dependency Version][php-version-image]][packagist-url]\n[![GitHub Repo Stars][github-stars-image]][github-repo-url]\n\nUtilize Laravel Processes to run PHP code asynchronously, as if using Laravel Concurrency.\n\n## What really is this?\n[Laravel Processes](https://laravel.com/docs/10.x/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:\n- You may only execute PHP code\n- Restrictions from `opis/closure` apply (see [their README](https://github.com/opis/closure))\n- Hands-off execution: no built-in result-checking, check the results yourself (e.g. via database, file cache, etc)\n\nThis library internally uses an Artisan command to run the async code, which is similar to Laravel 11 [Concurrency](https://laravel.com/docs/11.x/concurrency).\n\n## Why should I want this?\nThis library is very helpful for these cases:\n- You want a cross-platform minimal-setup async for easy vertical scaling\n- You want to start quick-and-dirty async tasks right now (e.g. prefetching resources, pinging remote, etc.)\n  - Best is if your task only has very few lines of code\n- Laravel 11 [Concurrency](https://laravel.com/docs/11.x/concurrency) is too limiting; e.g.:\n  - You want to do something else while waiting for results\n  - You want to conveniently limit the max (real) execution time of the concurrent tasks\n- And perhaps more!\n\nOf 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.\n\n## Installation\nvia Composer:\n\n```sh\ncomposer require vectorial1024/laravel-process-async\n```\n\nThis library supports Unix and Windows; see the Testing section for more details.\n\n### Extra requirements for Unix\nIf you are on Unix, check that you also have the following:\n- GNU Core Utilities (`coreutils`)\n  - MacOS do `brew install coreutils`!\n  - Other Unix distros should check if `coreutils` is preinstalled\n\n## Change log\nPlease see `CHANGELOG.md`.\n\n## Example code and features\nTasks can be defined as PHP closures, or (recommended) as an instance of a class that implements `AsyncTaskInterface`.\n\nA very simple example task to write Hello World to a file:\n\n```php\n// define the task...\n$target = \"document.txt\";\n$task = new AsyncTask(function () use ($target) {\n    file_put_contents($target, \"Hello World!\");\n});\n\n// if you are using interfaces, then it is just like this:\n// $task = new AsyncTask(new WriteToFileTask($target, $message));\n\n// then start it.\n$task-\u003estart();\n\n// the task is now run in another PHP process, and will not report back to this PHP process.\n```\n\n### Task time limits\nYou 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.\n\nThe 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.\n\nNote: `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.\n\n```php\n// start with the default time limit...\n$task-\u003estart();\n\n// start task with a different time limit...\n$task-\u003ewithTimeLimit(15)-\u003estart();\n\n// ...or not have any limits at all (beware of orphaned processes!)\n$task-\u003ewithoutTimeLimit()-\u003estart();\n```\n\nSome tips:\n- Don't sleep too long! On Windows, timeout handlers cannot trigger while your task is sleeping.\n  - Use short but frequent sleeps instead.\n- Avoid using `SIGINT`! On Unix, this signal is reserved for timeout detection. \n\n### Task IDs\nYou 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.\n\nBy default, if a task does not has its user-specified task ID when starting, a ULID will be generated as its task ID.\n\n```php\n// create a task with a specified task ID...\n$task = new AsyncTask(function () {}, \"customTaskID\");\n\n// will return a status object for immediate checking...\n$status = $task-\u003estart();\n\n// in case the task ID was not given, what is the generated task ID?\n$taskID = $status-\u003etaskID;\n\n// is that task still running?\n$status-\u003eisRunning();\n\n// when task IDs are known, task status objects can be recreated on-the-fly\n$anotherStatus = new AsyncTaskStatus(\"customTaskID\");\n```\n\nSome tips:\n- Task IDs can be optional (i.e. `null`) but CANNOT be blank (i.e. `\"\"`)!\n- If multiple tasks are started with the same task ID, then the task status object will only track the first task that was started\n- Known issue: on Windows, checking task statuses can be slow (about 0.5 - 1 seconds) due to underlying bottlenecks\n\n### Securing the task runners\nThe way this library works means that attackers (or other unwanted parties) may simply craft malicious commands that mimic legitimate usage of this library.\n\nTo secure the task runners from being started illegitimately, you may configure the `.env` file to contain the following key:\n\n```\nPROCESS_ASYNC_SECRET_KEY=[your secret key here]\n```\n\nYou may need to clear your Laravel optimisation cache after changing this value.\n\nThe 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.\n\n### Fake Objects\nFake 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.\n\nThe following fake objects are available:\n\n- `FakeAsyncTask`: a fake of `AsyncTask`\n- `FakeAsyncTaskStatus`: a fake of `AsyncTaskStatus`\n\nNotably, they can be used like this:\n\n```php\n// you may obtain the task status in the usual way...\n$fakeTask = new FakeAsyncTask(/* ... */, taskID: \"TestingTask\");\n$fakeStatus = $fakeTask-\u003estart();\n\n// ...or just construct it directly\n$fakeStatusDirect = new FakeAsyncTaskStatus(\"TestingTask\");\n// both are the same\nassert($fakeStatus == $fakeStatusDirect); // passes\n\n// in your test code, fake task status can be used just like the normal task status:\n$fakeStatus-\u003eisRunning(); // default returns true\n\n// note: FakeAsyncTaskStatus defaults to \"is running\" when constructed\n// to simulate \"task ended\", simply do:\n$fakeStatus-\u003efakeStopRunning();\n// then, the following will return false\n$fakeStatus-\u003eisRunning(); // returns false\n```\n\n## Testing\nPHPUnit via Composer script:\n```sh\ncomposer run-script test\n```\n\n\u003c!---\nWe also include MacOS on top of Ubuntu so that the devs (usually using Mac) can see for themselves it also works.\n---\u003e\n\nLatest cross-platform testing results:\n|Runtime|MacOS|Ubuntu|Windows|\n|---|---|---|---|\n|Laravel 10 (PHP 8.1)|skipped*|skipped*|skipped*|\n|Laravel 11 (PHP 8.2)|[![Build-M-L11][build-m-l11-image]][build-m-l11-url]|[![Build-U-L11][build-u-l11-image]][build-u-l11-url]|[![Build-W-L11][build-w-l11-image]][build-w-l11-url]|\n|Laravel 12 (PHP 8.3)|[![Build-M-L12][build-m-l12-image]][build-m-l12-url]|[![Build-U-L12][build-u-l12-image]][build-u-l12-url]|[![Build-W-L12][build-w-l12-image]][build-w-l12-url]|\n|Laravel 13 (PHP ???)|🛠️|🛠️|🛠️|\n\n\\*Note: tests for these Laravel versions are skipped because they have old `artisan` file contents:\n- It is difficult to mock multi-version `artisan` files for different Laravel versions (see https://github.com/Vectorial1024/laravel-process-async/issues/6).\n- It is rare for the `artisan` file at Laravel to be updated\n- The actual behavior is expected to be the same.\n\n[packagist-url]: https://packagist.org/packages/vectorial1024/laravel-process-async\n[packagist-stats-url]: https://packagist.org/packages/vectorial1024/laravel-process-async/stats\n[github-repo-url]: https://github.com/Vectorial1024/laravel-process-async\n\n[build-m-l11-url]: https://github.com/Vectorial1024/laravel-process-async/actions/workflows/macos_l11.yml\n[build-m-l11-image]: https://img.shields.io/github/actions/workflow/status/Vectorial1024/laravel-process-async/macos_l11.yml?style=plastic\n[build-m-l12-url]: https://github.com/Vectorial1024/laravel-process-async/actions/workflows/macos_l12.yml\n[build-m-l12-image]: https://img.shields.io/github/actions/workflow/status/Vectorial1024/laravel-process-async/macos_l12.yml?style=plastic\n\n[build-u-l11-url]: https://github.com/Vectorial1024/laravel-process-async/actions/workflows/ubuntu_l11.yml\n[build-u-l11-image]: https://img.shields.io/github/actions/workflow/status/Vectorial1024/laravel-process-async/ubuntu_l11.yml?style=plastic\n[build-u-l12-url]: https://github.com/Vectorial1024/laravel-process-async/actions/workflows/ubuntu_l12.yml\n[build-u-l12-image]: https://img.shields.io/github/actions/workflow/status/Vectorial1024/laravel-process-async/ubuntu_l12.yml?style=plastic\n\n[build-w-l11-url]: https://github.com/Vectorial1024/laravel-process-async/actions/workflows/windows_l11.yml\n[build-w-l11-image]: https://img.shields.io/github/actions/workflow/status/Vectorial1024/laravel-process-async/windows_l11.yml?style=plastic\n[build-w-l12-url]: https://github.com/Vectorial1024/laravel-process-async/actions/workflows/windows_l12.yml\n[build-w-l12-image]: https://img.shields.io/github/actions/workflow/status/Vectorial1024/laravel-process-async/windows_l12.yml?style=plastic\n\n[packagist-license-image]: https://img.shields.io/packagist/l/vectorial1024/laravel-process-async?style=plastic\n[packagist-version-image]: https://img.shields.io/packagist/v/vectorial1024/laravel-process-async?style=plastic\n[packagist-downloads-image]: https://img.shields.io/packagist/dm/vectorial1024/laravel-process-async?style=plastic\n[php-version-image]: https://img.shields.io/packagist/dependency-v/vectorial1024/laravel-process-async/php?style=plastic\u0026label=PHP\n[github-stars-image]: https://img.shields.io/github/stars/vectorial1024/laravel-process-async\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvectorial1024%2Flaravel-process-async","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvectorial1024%2Flaravel-process-async","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvectorial1024%2Flaravel-process-async/lists"}