From 9d92a0707c0d447c1d843eb0dab9a883c405c625 Mon Sep 17 00:00:00 2001 From: Vincent Wong Date: Sun, 22 Dec 2024 01:58:08 +0800 Subject: [PATCH 1/5] Adjust comments --- src/AsyncTask.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AsyncTask.php b/src/AsyncTask.php index 0c25c59..b004323 100644 --- a/src/AsyncTask.php +++ b/src/AsyncTask.php @@ -63,7 +63,7 @@ public function run(): void } /** - * Starts this AsyncTask immediately in the background. + * Starts this AsyncTask immediately in the background. A runner will then run this AsyncTask. * @return void */ public function start(): void From 8f08adf0b056038a17c17a9e02f7d1f277852b16 Mon Sep 17 00:00:00 2001 From: Vincent Wong Date: Sun, 22 Dec 2024 17:49:33 +0800 Subject: [PATCH 2/5] Relate with Laravel 11 Concurrency --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index a4b31c6..2e4ccb6 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,8 @@ This library is very helpful for these cases: - You want a minimal-setup async for easy vertical scaling - 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](https://laravel.com/docs/11.x/concurrency) is too limiting; e.g.: + - You want to do something else while waiting for results 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. From 7a06f38c29c700dc7bea15f81bd9fe7956926462 Mon Sep 17 00:00:00 2001 From: Vincent Wong Date: Sun, 22 Dec 2024 18:28:56 +0800 Subject: [PATCH 3/5] Install OS detection library --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c839fb7..6f97076 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "require": { "php": "^8.1", "illuminate/support": "^10.0|^11.0", - "laravel/serializable-closure": "^1.0" + "laravel/serializable-closure": "^1.0", + "loophp/phposinfo": "^1.8" }, "require-dev": { "phpunit/phpunit": "^10", From aa4b584bf340bd23d594164f080dfeed56bb358f Mon Sep 17 00:00:00 2001 From: Vincent Wong Date: Sun, 22 Dec 2024 18:40:57 +0800 Subject: [PATCH 4/5] Detach the runner from the parent --- src/AsyncTask.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/AsyncTask.php b/src/AsyncTask.php index b004323..9a8346b 100644 --- a/src/AsyncTask.php +++ b/src/AsyncTask.php @@ -6,6 +6,7 @@ use Illuminate\Process\InvokedProcess; use Illuminate\Support\Facades\Process; use Laravel\SerializableClosure\SerializableClosure; +use loophp\phposinfo\OsInfo; /** * The common handler of an AsyncTask; this can be a closure (will be wrapped inside AsyncTask) or an interface instance. @@ -68,9 +69,20 @@ public function run(): void */ public function start(): void { - // assume unix for now + // prepare the runner command $serializedTask = $this->toBase64Serial(); - $this->runnerProcess = Process::quietly()->start("php artisan async:run $serializedTask"); + $baseCommand = "php artisan async:run $serializedTask"; + + // then, specific actions depending on the runtime OS + if (OsInfo::isWindows()) { + // basically, in windows, it is too tedioous to check whether we are in cmd or ps, + // but we require cmd (ps won't work here), so might as well force cmd like this + $this->runnerProcess = Process::quietly()->start("cmd /c start /b $baseCommand"); + return; + } + // assume anything not windows to be unix + // unix use nohup + $this->runnerProcess = Process::quietly()->start("nohup $baseCommand"); } /** From c4b4c092fbb94822151338e2ef5ab79092c3ec81 Mon Sep 17 00:00:00 2001 From: Vincent Wong Date: Sun, 22 Dec 2024 20:49:01 +0800 Subject: [PATCH 5/5] Hint on multi-OS support --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 2e4ccb6..9085514 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ via Composer: composer require vectorial1024/laravel-process-async ``` +This library supports Unix and Windows; see the Testing section for more details. + ## Change log Please see `CHANGELOG.md`.