diff --git a/README.md b/README.md index a4b31c6..9085514 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. @@ -28,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`. 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", diff --git a/src/AsyncTask.php b/src/AsyncTask.php index 0c25c59..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. @@ -63,14 +64,25 @@ 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 { - // 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"); } /**