diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2023-04-13 02:17:19 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2023-04-13 02:17:19 +0300 |
commit | 7330a35806ffc1c91f0765d1fe19df2c459d5579 (patch) | |
tree | 489bad797be3bfadcb966bbfb290dd5044f74189 /src | |
parent | 544f2444211aa1ad9e3611818fb87e8a8882ff55 (diff) |
add signals example
Diffstat (limited to 'src')
-rw-r--r-- | src/classes/Job.php | 9 | ||||
-rw-r--r-- | src/classes/jobs.php | 21 | ||||
-rw-r--r-- | src/jobs/LongRunningTask.php | 24 | ||||
-rw-r--r-- | src/launcher.php | 5 | ||||
-rw-r--r-- | src/main.php | 19 |
5 files changed, 73 insertions, 5 deletions
diff --git a/src/classes/Job.php b/src/classes/Job.php index 4ecbf6c..56052cb 100644 --- a/src/classes/Job.php +++ b/src/classes/Job.php @@ -48,4 +48,13 @@ abstract class Job extends model { 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) {} }
\ No newline at end of file diff --git a/src/classes/jobs.php b/src/classes/jobs.php index 9acbc50..5010775 100644 --- a/src/classes/jobs.php +++ b/src/classes/jobs.php @@ -82,6 +82,21 @@ class jobs 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. * @@ -198,10 +213,11 @@ class jobs /** * @param string|string[] $targets + * @throws \jobd\exceptions\JobdException + * @return bool */ - public static function poke($targets) + public static function poke($targets): bool { - $client = getJobdMaster(); if (!is_array($targets)) @@ -248,7 +264,6 @@ class jobs class job_target { - const any = "any"; public static function high(int $server): string diff --git a/src/jobs/LongRunningTask.php b/src/jobs/LongRunningTask.php new file mode 100644 index 0000000..3684055 --- /dev/null +++ b/src/jobs/LongRunningTask.php @@ -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'); + } + } + +} diff --git a/src/launcher.php b/src/launcher.php index d1b9892..8cd9fda 100644 --- a/src/launcher.php +++ b/src/launcher.php @@ -7,6 +7,8 @@ $job = null; register_shutdown_function(function() { global $job; + if ($job instanceof \jobd\exceptions\JobInterruptedException) + exit($job->getCode()); if ($job !== true) exit(1); }); @@ -25,6 +27,9 @@ if ($job->status != Job::STATUS_RUNNING) try { if ($job->run() !== false) $job = true; +} catch (\jobd\exceptions\JobInterruptedException $e) { + fprintf(STDERR, $e->getMessage()."\n"); + $job = $e; } catch (Exception $e) { fprintf(STDERR, $e.''); exit(1); diff --git a/src/main.php b/src/main.php index 068263f..92531c8 100644 --- a/src/main.php +++ b/src/main.php @@ -10,19 +10,23 @@ Commands: test hello createfile + run_lrt + kill_lrt ID EOF; exit; } -$cmd = $argv[1]; +array_shift($argv); +$cmd = array_shift($argv); + $func = "cmd_{$cmd}"; if (!function_exists($func)) { echo red("command '".$cmd."' is not implemented")."\n"; exit(1); } -call_user_func($func); +call_user_func($func, $argv); /** Commands */ @@ -90,4 +94,15 @@ function cmd_hello() { function cmd_createfile() { $file = input('Enter file name: '); 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); }
\ No newline at end of file |