Skip to content

Commit

Permalink
Merge pull request #9 from Vectorial1024/detachment
Browse files Browse the repository at this point in the history
Feature: background runner detachment
  • Loading branch information
Vectorial1024 authored Dec 22, 2024
2 parents a391073 + c4b4c09 commit b47efb9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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`.

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 15 additions & 3 deletions src/AsyncTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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");
}

/**
Expand Down

0 comments on commit b47efb9

Please sign in to comment.