From 7330a35806ffc1c91f0765d1fe19df2c459d5579 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Thu, 13 Apr 2023 02:17:19 +0300 Subject: add signals example --- src/classes/Job.php | 9 +++++++++ src/classes/jobs.php | 21 ++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) (limited to 'src/classes') 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 -- cgit v1.2.3