Skip to content

Commit

Permalink
optional service runner timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
nekufa committed Dec 24, 2024
1 parent 8bbe54f commit 7b8f812
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Service/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,12 @@ private function controlSubject(string $verb, string $name, string $id): string
return "\$SRV.$verb.$name.$id";
}

public function run(): void
public function run(?float $timeout = null): void
{
$this->client->logger->info("$this->name is ready to accept connections\n");
$start = microtime(true);

while (true) {
while ($timeout ? microtime(true) < $start + $timeout : true) {
try {
$this->client->process();
} catch (\Exception $e) {
Expand Down
16 changes: 16 additions & 0 deletions tests/Functional/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Basis\Nats\Client;
use Basis\Nats\Message\Payload;
use Basis\Nats\Service\Service;
use Exception;
use Tests\FunctionalTestCase;
use Tests\Utils\TestEndpoint;

Expand Down Expand Up @@ -107,6 +108,21 @@ public function testServiceStats()
$this->assertSame($stats->endpoints[0]['average_processing_time'], 0.0);
}

public function testServiceRunner()
{
$service = $this->createTestService();
$called = false;

$service->addGroup('v1')->addEndpoint('test_runner', function () use (&$called) {
$called = true;
throw new Exception("Runtime error");
});

$service->client->publish('v1.test_runner', []);
$service->run(0.1);
$this->assertTrue($called);
}

public function testServiceRequestReplyClass()
{
$service = $this->createTestService();
Expand Down

0 comments on commit 7b8f812

Please sign in to comment.