Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions library/Vspheredb/CheckPluginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Exception;
use gipfl\Cli\Screen;
use Icinga\Module\Vspheredb\Clicommands\Command;
use Icinga\Module\Vspheredb\Daemon\PromiseUtil;
use Icinga\Module\Vspheredb\Data\Anonymizer;
use InvalidArgumentException;
use React\Promise\PromiseInterface;
Expand Down Expand Up @@ -75,14 +76,17 @@ protected function run($callable)
}

if ($result instanceof PromiseInterface) {
$result->then(function () {
// All done
}, function (Exception $e) {
$this->addProblem('UNKNOWN', $e->getMessage());
$this->showOptionalTrace($e);
})->finally(function () {
$this->shutdown();
});
PromiseUtil::finally(
$result->then(function () {
// All done
}, function (Exception $e) {
$this->addProblem('UNKNOWN', $e->getMessage());
$this->showOptionalTrace($e);
}),
function () {
$this->shutdown();
}
);
} else {
$this->shutdown();
}
Expand Down
28 changes: 28 additions & 0 deletions library/Vspheredb/Daemon/PromiseUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Icinga\Module\Vspheredb\Daemon;

use React\Promise\PromiseInterface;

/**
* ReactPHP Promise compatibility helpers
*/
class PromiseUtil
{
/**
* Register cleanup on ReactPHP Promise v2 and v3 promises
*
* @param PromiseInterface $promise Promise to register cleanup on
* @param callable $callback Callback receiving no arguments
*
* @return PromiseInterface
*/
public static function finally(PromiseInterface $promise, callable $callback): PromiseInterface
{
if (method_exists($promise, 'finally')) {
return $promise->finally($callback);
}

return $promise->always($callback);
}
}
23 changes: 14 additions & 9 deletions library/Vspheredb/Monitoring/CheckPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Icinga\Module\Vspheredb\Monitoring;

use Icinga\Module\Vspheredb\Daemon\PromiseUtil;

class CheckPlugin
{
/** @var array */
Expand Down Expand Up @@ -34,15 +36,18 @@ protected function run($callable)
}

if ($result instanceof PromiseInterface) {
$result->then(function () {
echo "as\n";
}, function (Exception $e) {
var_dump('whut');
$this->addProblem('UNKNOWN', $e->getMessage());
})->finally(function () {
var_dump('Shut after res');
$this->shutdown();
});
PromiseUtil::finally(
$result->then(function () {
echo "as\n";
}, function (Exception $e) {
var_dump('whut');
$this->addProblem('UNKNOWN', $e->getMessage());
}),
function () {
var_dump('Shut after res');
$this->shutdown();
}
);
} else {
$this->shutdown();
}
Expand Down
Loading