Skip to content

Commit 3426c99

Browse files
authored
Merge pull request #14 from netlogix/feat/add-polling-configuration
2 parents 09b0108 + 4211232 commit 3426c99

4 files changed

Lines changed: 42 additions & 9 deletions

File tree

Classes/Controller/StatusController.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Neos\Flow\Mvc\Controller\ActionController;
88
use Netlogix\Neos\AsyncWorkspaceActions\Domain\Model\Job;
99
use Netlogix\Neos\AsyncWorkspaceActions\Domain\Repository\JobRepository;
10+
use function usleep;
1011

1112
class StatusController extends ActionController
1213
{
@@ -17,14 +18,25 @@ class StatusController extends ActionController
1718
*/
1819
protected $jobRepository;
1920

20-
public function pollAction(Job $job): void
21+
/**
22+
* @var array
23+
* @Flow\InjectConfiguration(path="polling")
24+
*/
25+
protected array $pollingConfiguration = [];
26+
27+
public function pollAction(Job $job, int $count = 1): void
2128
{
2229
if ($job->getStatus() !== Job::STATUS_DONE) {
23-
sleep(5);
24-
$this->redirect('poll', null, null, ['job' => $job]);
30+
$this->sleep($count);
31+
$this->redirect('poll', null, null, [
32+
'job' => $job,
33+
'count' => ++$count
34+
]);
2535
}
2636

27-
$this->redirect('done', null, null, ['job' => $job]);
37+
$this->redirect('done', null, null, [
38+
'job' => $job
39+
]);
2840
}
2941

3042
public function doneAction(Job $job): string
@@ -37,4 +49,16 @@ public function doneAction(Job $job): string
3749
return json_encode($feedback, JSON_PRETTY_PRINT);
3850
}
3951

52+
private function sleep(int $retry): void
53+
{
54+
$retryInterval = ($this->pollingConfiguration['retryInterval'] ?? 5);
55+
if ($this->pollingConfiguration['exponentialBackoff'] ?? false) {
56+
$backoff = ($this->pollingConfiguration['factor'] ?? 2) ** ($retry - 1) * $retryInterval;
57+
} else {
58+
$backoff = $retryInterval;
59+
}
60+
$sleeping = (int) ($backoff * 1000 * 1000);
61+
usleep($sleeping);
62+
}
63+
4064
}

Configuration/Routes.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
'@action': 'poll'
1818
'@format': 'json'
1919
httpMethods: ['GET']
20+
appendExceedingArguments: true

Configuration/Settings.Threshold.yaml

Lines changed: 0 additions & 5 deletions
This file was deleted.

Configuration/Settings.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Netlogix:
2+
Neos:
3+
AsyncWorkspaceActions:
4+
# Amount of nodes required to publish or discard asynchronously
5+
nodeThreshold: 100
6+
7+
polling:
8+
# Amount of seconds to wait between retries
9+
retryInterval: 5
10+
# Use exponential backoff
11+
exponentialBackoff: false
12+
# Exponential backoff factor
13+
factor: 2

0 commit comments

Comments
 (0)