From 80e0b016868a79364608fdb8543341277a930e32 Mon Sep 17 00:00:00 2001 From: Max Furtuna Date: Fri, 7 Dec 2018 17:13:15 +0200 Subject: [PATCH] #64 some progress --- lib/Worker/DefaultPool.php | 7 +++++++ lib/Worker/Internal/PooledWorker.php | 7 +++++++ lib/Worker/TaskWorker.php | 12 ++++++++++++ lib/Worker/Worker.php | 7 +++++++ test/Worker/AbstractWorkerTest.php | 28 ++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+) diff --git a/lib/Worker/DefaultPool.php b/lib/Worker/DefaultPool.php index 1e5efd8e..965eed3d 100644 --- a/lib/Worker/DefaultPool.php +++ b/lib/Worker/DefaultPool.php @@ -252,4 +252,11 @@ private function pull(): Worker return $worker; } + + public function restart($force = false): Promise + { + + } + + } diff --git a/lib/Worker/Internal/PooledWorker.php b/lib/Worker/Internal/PooledWorker.php index 7c39a933..c5206c7f 100644 --- a/lib/Worker/Internal/PooledWorker.php +++ b/lib/Worker/Internal/PooledWorker.php @@ -72,4 +72,11 @@ public function kill() { $this->worker->kill(); } + + public function restart($force = false): Promise + { + return $this->worker->restart($force); + } + + } diff --git a/lib/Worker/TaskWorker.php b/lib/Worker/TaskWorker.php index 58614e16..7777b251 100644 --- a/lib/Worker/TaskWorker.php +++ b/lib/Worker/TaskWorker.php @@ -150,4 +150,16 @@ public function kill() $this->exitStatus = new Success(0); } + + public function restart($force = false): Promise + { + return call(function () use ($force){ + if($force) { + $this->context->kill(); + } else { + yield $this->shutdown(); + } + yield $this->context->start(); + }); + } } diff --git a/lib/Worker/Worker.php b/lib/Worker/Worker.php index 7a6c35c7..8e474cb9 100644 --- a/lib/Worker/Worker.php +++ b/lib/Worker/Worker.php @@ -41,4 +41,11 @@ public function shutdown(): Promise; * Immediately kills the context. */ public function kill(); + + /** + * Restart worker + * @param bool $force Whether for cancel current task or wait it finished + * @return Promise + */ + public function restart($force = false): Promise; } diff --git a/test/Worker/AbstractWorkerTest.php b/test/Worker/AbstractWorkerTest.php index ba5f7d38..0c721d62 100644 --- a/test/Worker/AbstractWorkerTest.php +++ b/test/Worker/AbstractWorkerTest.php @@ -262,4 +262,32 @@ public function run(Environment $environment) yield $worker->shutdown(); }); } + + + public function testRestart() + { + Loop::run(function () { + $worker = $this->createWorker(); + for($i=0; $i<=1; $i++) { + $returnValue = yield $worker->enqueue(new TestTask(42)); + $this->assertEquals(42, $returnValue); + + yield $worker->restart(); + } + }); + } + + public function testForceRestart() + { + Loop::run(function () { + $worker = $this->createWorker(); + for($i=0; $i<=1; $i++) { + $returnValue = yield $worker->enqueue(new TestTask(42)); + $this->assertEquals(42, $returnValue); + + yield $worker->restart(true); + } + }); + } + }