Skip to content

Commit b47efb9

Browse files
Merge pull request #9 from Vectorial1024/detachment
Feature: background runner detachment
2 parents a391073 + c4b4c09 commit b47efb9

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ This library is very helpful for these cases:
1818
- You want a minimal-setup async for easy vertical scaling
1919
- You want to start quick-and-dirty async tasks right now (e.g. prefetching resources, pinging remote, etc.)
2020
- Best is if your task only has very few lines of code
21+
- Laravel 11 [Concurrency](https://laravel.com/docs/11.x/concurrency) is too limiting; e.g.:
22+
- You want to do something else while waiting for results
2123

2224
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.
2325

@@ -28,6 +30,8 @@ via Composer:
2830
composer require vectorial1024/laravel-process-async
2931
```
3032

33+
This library supports Unix and Windows; see the Testing section for more details.
34+
3135
## Change log
3236
Please see `CHANGELOG.md`.
3337

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"require": {
3333
"php": "^8.1",
3434
"illuminate/support": "^10.0|^11.0",
35-
"laravel/serializable-closure": "^1.0"
35+
"laravel/serializable-closure": "^1.0",
36+
"loophp/phposinfo": "^1.8"
3637
},
3738
"require-dev": {
3839
"phpunit/phpunit": "^10",

src/AsyncTask.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Process\InvokedProcess;
77
use Illuminate\Support\Facades\Process;
88
use Laravel\SerializableClosure\SerializableClosure;
9+
use loophp\phposinfo\OsInfo;
910

1011
/**
1112
* The common handler of an AsyncTask; this can be a closure (will be wrapped inside AsyncTask) or an interface instance.
@@ -63,14 +64,25 @@ public function run(): void
6364
}
6465

6566
/**
66-
* Starts this AsyncTask immediately in the background.
67+
* Starts this AsyncTask immediately in the background. A runner will then run this AsyncTask.
6768
* @return void
6869
*/
6970
public function start(): void
7071
{
71-
// assume unix for now
72+
// prepare the runner command
7273
$serializedTask = $this->toBase64Serial();
73-
$this->runnerProcess = Process::quietly()->start("php artisan async:run $serializedTask");
74+
$baseCommand = "php artisan async:run $serializedTask";
75+
76+
// then, specific actions depending on the runtime OS
77+
if (OsInfo::isWindows()) {
78+
// basically, in windows, it is too tedioous to check whether we are in cmd or ps,
79+
// but we require cmd (ps won't work here), so might as well force cmd like this
80+
$this->runnerProcess = Process::quietly()->start("cmd /c start /b $baseCommand");
81+
return;
82+
}
83+
// assume anything not windows to be unix
84+
// unix use nohup
85+
$this->runnerProcess = Process::quietly()->start("nohup $baseCommand");
7486
}
7587

7688
/**

0 commit comments

Comments
 (0)