Skip to content

Commit 05ef681

Browse files
committed
Add daemon Promise cleanup compatibility helper (#643)
ReactPHP Promise v2 exposes cleanup through always(), while v3 exposes it through finally(). Use a small daemon helper so check-plugin shutdown registration works with both dependency branches. (cherry picked from commit 741157d)
1 parent 8af9e23 commit 05ef681

3 files changed

Lines changed: 54 additions & 17 deletions

File tree

library/Vspheredb/CheckPluginHelper.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use gipfl\Cli\Screen;
77
use Icinga\Module\Vspheredb\Clicommands\Command;
8+
use Icinga\Module\Vspheredb\Daemon\PromiseUtil;
89
use Icinga\Module\Vspheredb\Data\Anonymizer;
910
use InvalidArgumentException;
1011
use React\Promise\PromiseInterface;
@@ -72,14 +73,17 @@ protected function run($callable)
7273
}
7374

7475
if ($result instanceof PromiseInterface) {
75-
$result->then(function () {
76-
// All done
77-
}, function (Exception $e) {
78-
$this->addProblem('UNKNOWN', $e->getMessage());
79-
$this->showOptionalTrace($e);
80-
})->finally(function () {
81-
$this->shutdown();
82-
});
76+
PromiseUtil::finally(
77+
$result->then(function () {
78+
// All done
79+
}, function (Exception $e) {
80+
$this->addProblem('UNKNOWN', $e->getMessage());
81+
$this->showOptionalTrace($e);
82+
}),
83+
function () {
84+
$this->shutdown();
85+
}
86+
);
8387
} else {
8488
$this->shutdown();
8589
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Icinga\Module\Vspheredb\Daemon;
4+
5+
use React\Promise\PromiseInterface;
6+
7+
/**
8+
* ReactPHP Promise compatibility helpers
9+
*/
10+
class PromiseUtil
11+
{
12+
/**
13+
* Register cleanup on ReactPHP Promise v2 and v3 promises
14+
*
15+
* @param PromiseInterface $promise Promise to register cleanup on
16+
* @param callable $callback Callback receiving no arguments
17+
*
18+
* @return PromiseInterface
19+
*/
20+
public static function finally(PromiseInterface $promise, callable $callback): PromiseInterface
21+
{
22+
if (method_exists($promise, 'finally')) {
23+
return $promise->finally($callback);
24+
}
25+
26+
return $promise->always($callback);
27+
}
28+
}

library/Vspheredb/Monitoring/CheckPlugin.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Icinga\Module\Vspheredb\Monitoring;
44

5+
use Icinga\Module\Vspheredb\Daemon\PromiseUtil;
6+
57
class CheckPlugin
68
{
79
/** @var array */
@@ -31,15 +33,18 @@ protected function run($callable)
3133
}
3234

3335
if ($result instanceof PromiseInterface) {
34-
$result->then(function () {
35-
echo "as\n";
36-
}, function (Exception $e) {
37-
var_dump('whut');
38-
$this->addProblem('UNKNOWN', $e->getMessage());
39-
})->finally(function () {
40-
var_dump('Shut after res');
41-
$this->shutdown();
42-
});
36+
PromiseUtil::finally(
37+
$result->then(function () {
38+
echo "as\n";
39+
}, function (Exception $e) {
40+
var_dump('whut');
41+
$this->addProblem('UNKNOWN', $e->getMessage());
42+
}),
43+
function () {
44+
var_dump('Shut after res');
45+
$this->shutdown();
46+
}
47+
);
4348
} else {
4449
$this->shutdown();
4550
}

0 commit comments

Comments
 (0)