Skip to content

Commit

Permalink
amphp#64 some progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekstazi committed Dec 7, 2018
1 parent e77c06a commit 80e0b01
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/Worker/DefaultPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,11 @@ private function pull(): Worker

return $worker;
}

public function restart($force = false): Promise
{

}


}
7 changes: 7 additions & 0 deletions lib/Worker/Internal/PooledWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,11 @@ public function kill()
{
$this->worker->kill();
}

public function restart($force = false): Promise
{
return $this->worker->restart($force);
}


}
12 changes: 12 additions & 0 deletions lib/Worker/TaskWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
}
7 changes: 7 additions & 0 deletions lib/Worker/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
28 changes: 28 additions & 0 deletions test/Worker/AbstractWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}

}

0 comments on commit 80e0b01

Please sign in to comment.