77use Neos \Flow \Mvc \Controller \ActionController ;
88use Netlogix \Neos \AsyncWorkspaceActions \Domain \Model \Job ;
99use Netlogix \Neos \AsyncWorkspaceActions \Domain \Repository \JobRepository ;
10+ use function usleep ;
1011
1112class 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}
0 commit comments