summaryrefslogtreecommitdiffstats
path: root/library/Reporting/Schedule.php
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--library/Reporting/Schedule.php177
1 files changed, 86 insertions, 91 deletions
diff --git a/library/Reporting/Schedule.php b/library/Reporting/Schedule.php
index e0ffa9f..ddd8bd3 100644
--- a/library/Reporting/Schedule.php
+++ b/library/Reporting/Schedule.php
@@ -1,160 +1,155 @@
<?php
+
// Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2
namespace Icinga\Module\Reporting;
-class Schedule
-{
- /** @var int */
- protected $id;
+use Exception;
+use Icinga\Module\Reporting\Hook\ActionHook;
+use Icinga\Util\Json;
+use ipl\Scheduler\Common\TaskProperties;
+use ipl\Scheduler\Contract\Task;
+use Ramsey\Uuid\Uuid;
+use React\EventLoop\Loop;
+use React\Promise;
+use React\Promise\ExtendedPromiseInterface;
- /** @var int */
- protected $reportId;
+use function md5;
- /** @var \DateTime */
- protected $start;
+class Schedule implements Task
+{
+ use TaskProperties;
- /** @var string */
- protected $frequency;
+ /** @var int */
+ protected $id;
/** @var string */
protected $action;
/** @var array */
- protected $config;
+ protected $config = [];
- /**
- * @return int
- */
- public function getId()
+ /** @var Report */
+ protected $report;
+
+ public function __construct(string $name, string $action, array $config, Report $report)
{
- return $this->id;
+ $this->action = $action;
+ $this->config = $config;
+ ksort($this->config);
+
+ $this
+ ->setName($name)
+ ->setReport($report)
+ ->setUuid(Uuid::fromBytes($this->getChecksum()));
}
/**
- * @param int $id
+ * Create schedule from the given model
+ *
+ * @param Model\Schedule $scheduleModel
*
- * @return $this
+ * @return static
*/
- public function setId($id)
+
+ public static function fromModel(Model\Schedule $scheduleModel, Report $report): self
{
- $this->id = $id;
+ $config = Json::decode($scheduleModel->config ?? [], true);
+ $schedule = new static("Schedule{$scheduleModel->id}", $scheduleModel->action, $config, $report);
+ $schedule->id = $scheduleModel->id;
- return $this;
+ return $schedule;
}
/**
+ * Get the id of this schedule
+ *
* @return int
*/
- public function getReportId()
+ public function getId(): int
{
- return $this->reportId;
+ return $this->id;
}
/**
- * @param int $id
+ * Get the action hook class of this schedule
*
- * @return $this
- */
- public function setReportId($id)
- {
- $this->reportId = $id;
-
- return $this;
- }
-
- /**
- * @return \DateTime
+ * @return string
*/
- public function getStart()
+ public function getAction(): string
{
- return $this->start;
+ return $this->action;
}
/**
- * @param \DateTime $start
+ * Get the config of this schedule
*
- * @return $this
+ * @return array
*/
- public function setStart(\DateTime $start)
+ public function getConfig(): array
{
- $this->start = $start;
-
- return $this;
+ return $this->config;
}
/**
- * @return string
+ * Get the report this schedule belongs to
+ *
+ * @return Report
*/
- public function getFrequency()
+ public function getReport(): Report
{
- return $this->frequency;
+ return $this->report;
}
/**
- * @param string $frequency
+ * Set the report this schedule belongs to
*
- * @return $this
+ * @param Report $report
+ *
+ * @return $this
*/
- public function setFrequency($frequency)
+ public function setReport(Report $report): self
{
- $this->frequency = $frequency;
+ $this->report = $report;
return $this;
}
/**
+ * Get the checksum of this schedule
+ *
* @return string
*/
- public function getAction()
+ public function getChecksum(): string
{
- return $this->action;
+ return md5(
+ $this->getName() . $this->getReport()->getName() . $this->getAction() . Json::encode($this->getConfig()),
+ true
+ );
}
- /**
- * @param string $action
- *
- * @return $this
- */
- public function setAction($action)
+ public function run(): ExtendedPromiseInterface
{
- $this->action = $action;
+ $deferred = new Promise\Deferred();
+ Loop::futureTick(function () use ($deferred) {
+ $action = $this->getAction();
+ /** @var ActionHook $actionHook */
+ $actionHook = new $action();
- return $this;
- }
+ try {
+ $actionHook->execute($this->getReport(), $this->getConfig());
+ } catch (Exception $err) {
+ $deferred->reject($err);
- /**
- * @return array
- */
- public function getConfig()
- {
- return $this->config;
- }
+ return;
+ }
- /**
- * @param array $config
- *
- * @return $this
- */
- public function setConfig(array $config)
- {
- $this->config = $config;
+ $deferred->resolve();
+ });
- return $this;
- }
+ /** @var ExtendedPromiseInterface $promise */
+ $promise = $deferred->promise();
- /**
- * @return string
- */
- public function getChecksum()
- {
- return \md5(
- $this->getId()
- . $this->getReportId()
- . $this->getStart()->format('Y-m-d H:i:s')
- . $this->getAction()
- . $this->getFrequency()
- . \json_encode($this->getConfig())
- );
+ return $promise;
}
}