add signals example
This commit is contained in:
parent
544f244421
commit
7330a35806
@ -34,7 +34,11 @@ is stored in [`init.php`](src/init.php) as global constants. Adjust to your need
|
|||||||
|
|
||||||
```
|
```
|
||||||
jobd-master --config jobd-master.conf
|
jobd-master --config jobd-master.conf
|
||||||
|
```
|
||||||
|
```
|
||||||
jobd --config jobd-1.conf
|
jobd --config jobd-1.conf
|
||||||
|
```
|
||||||
|
```
|
||||||
jobd --config jobd-2.conf
|
jobd --config jobd-2.conf
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"ch1p/jobd-client": "^1.5",
|
"ch1p/jobd-client": "^1.5",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"ext-mysqli": "*"
|
"ext-mysqli": "*",
|
||||||
|
"ext-pcntl": "*"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,4 +48,13 @@ abstract class Job extends model {
|
|||||||
|
|
||||||
abstract public function run();
|
abstract public function run();
|
||||||
|
|
||||||
|
public function __construct(array $raw) {
|
||||||
|
parent::__construct($raw);
|
||||||
|
|
||||||
|
pcntl_async_signals(true);
|
||||||
|
pcntl_signal(SIGTERM, [$this, 'signalHandler']);
|
||||||
|
pcntl_signal(SIGINT, [$this, 'signalHandler']);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function signalHandler(int $signal) {}
|
||||||
}
|
}
|
@ -82,6 +82,21 @@ class jobs
|
|||||||
return self::add($target, $name, $data, Job::STATUS_MANUAL);
|
return self::add($target, $name, $data, Job::STATUS_MANUAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function sendSignal(int $job_id, int $signal, string $target)
|
||||||
|
{
|
||||||
|
$client = getJobdMaster();
|
||||||
|
$response = $client->sendSignal($job_id, $signal, $target);
|
||||||
|
|
||||||
|
// master request failed
|
||||||
|
if (($error = $response->getError()) !== null)
|
||||||
|
throw new Exception("jobd returned error: ".$error);
|
||||||
|
|
||||||
|
$data = $response->getData();
|
||||||
|
$client->close();
|
||||||
|
|
||||||
|
return $data[$job_id];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run jobs with given ids and status=Job::STATUS_MANUAL and wait for results.
|
* Run jobs with given ids and status=Job::STATUS_MANUAL and wait for results.
|
||||||
*
|
*
|
||||||
@ -198,10 +213,11 @@ class jobs
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string|string[] $targets
|
* @param string|string[] $targets
|
||||||
|
* @throws \jobd\exceptions\JobdException
|
||||||
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function poke($targets)
|
public static function poke($targets): bool
|
||||||
{
|
{
|
||||||
|
|
||||||
$client = getJobdMaster();
|
$client = getJobdMaster();
|
||||||
|
|
||||||
if (!is_array($targets))
|
if (!is_array($targets))
|
||||||
@ -248,7 +264,6 @@ class jobs
|
|||||||
|
|
||||||
class job_target
|
class job_target
|
||||||
{
|
{
|
||||||
|
|
||||||
const any = "any";
|
const any = "any";
|
||||||
|
|
||||||
public static function high(int $server): string
|
public static function high(int $server): string
|
||||||
|
24
src/jobs/LongRunningTask.php
Normal file
24
src/jobs/LongRunningTask.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace jobs;
|
||||||
|
|
||||||
|
use jobd\exceptions\JobInterruptedException;
|
||||||
|
|
||||||
|
class LongRunningTask extends \Job
|
||||||
|
{
|
||||||
|
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
set_time_limit(0);
|
||||||
|
sleep(120);
|
||||||
|
echo 'ok';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function signalHandler(int $signal)
|
||||||
|
{
|
||||||
|
if ($signal == 15) {
|
||||||
|
throw new JobInterruptedException(0, 'i\'m exiting gracefully');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -7,6 +7,8 @@ $job = null;
|
|||||||
|
|
||||||
register_shutdown_function(function() {
|
register_shutdown_function(function() {
|
||||||
global $job;
|
global $job;
|
||||||
|
if ($job instanceof \jobd\exceptions\JobInterruptedException)
|
||||||
|
exit($job->getCode());
|
||||||
if ($job !== true)
|
if ($job !== true)
|
||||||
exit(1);
|
exit(1);
|
||||||
});
|
});
|
||||||
@ -25,6 +27,9 @@ if ($job->status != Job::STATUS_RUNNING)
|
|||||||
try {
|
try {
|
||||||
if ($job->run() !== false)
|
if ($job->run() !== false)
|
||||||
$job = true;
|
$job = true;
|
||||||
|
} catch (\jobd\exceptions\JobInterruptedException $e) {
|
||||||
|
fprintf(STDERR, $e->getMessage()."\n");
|
||||||
|
$job = $e;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
fprintf(STDERR, $e.'');
|
fprintf(STDERR, $e.'');
|
||||||
exit(1);
|
exit(1);
|
||||||
|
19
src/main.php
19
src/main.php
@ -10,19 +10,23 @@ Commands:
|
|||||||
test
|
test
|
||||||
hello
|
hello
|
||||||
createfile
|
createfile
|
||||||
|
run_lrt
|
||||||
|
kill_lrt ID
|
||||||
|
|
||||||
EOF;
|
EOF;
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$cmd = $argv[1];
|
array_shift($argv);
|
||||||
|
$cmd = array_shift($argv);
|
||||||
|
|
||||||
$func = "cmd_{$cmd}";
|
$func = "cmd_{$cmd}";
|
||||||
if (!function_exists($func)) {
|
if (!function_exists($func)) {
|
||||||
echo red("command '".$cmd."' is not implemented")."\n";
|
echo red("command '".$cmd."' is not implemented")."\n";
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
call_user_func($func);
|
call_user_func($func, $argv);
|
||||||
|
|
||||||
|
|
||||||
/** Commands */
|
/** Commands */
|
||||||
@ -91,3 +95,14 @@ function cmd_createfile() {
|
|||||||
$file = input('Enter file name: ');
|
$file = input('Enter file name: ');
|
||||||
jobs::add(job_target::any, jobs\CreateFile::class, ['file' => $file]);
|
jobs::add(job_target::any, jobs\CreateFile::class, ['file' => $file]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cmd_run_lrt() {
|
||||||
|
$ltr_id = jobs::add(job_target::low(1), jobs\LongRunningTask::class);
|
||||||
|
echo "id: $ltr_id\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
function cmd_kill_lrt($argv) {
|
||||||
|
$id = $argv[0];
|
||||||
|
$result = jobs::sendSignal($id, 15, job_target::low(1));
|
||||||
|
var_dump($result);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user