summaryrefslogtreecommitdiffstats
path: root/library/Reporting/Schedule.php
blob: ddd8bd3a3cbab70170772b799b6261ce7688cb8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php

// Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2

namespace Icinga\Module\Reporting;

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;

use function md5;

class Schedule implements Task
{
    use TaskProperties;

    /** @var int */
    protected $id;

    /** @var string */
    protected $action;

    /** @var array */
    protected $config = [];

    /** @var Report */
    protected $report;

    public function __construct(string $name, string $action, array $config, Report $report)
    {
        $this->action = $action;
        $this->config = $config;
        ksort($this->config);

        $this
            ->setName($name)
            ->setReport($report)
            ->setUuid(Uuid::fromBytes($this->getChecksum()));
    }

    /**
     * Create schedule from the given model
     *
     * @param Model\Schedule $scheduleModel
     *
     * @return static
     */

    public static function fromModel(Model\Schedule $scheduleModel, Report $report): self
    {
        $config = Json::decode($scheduleModel->config ?? [], true);
        $schedule = new static("Schedule{$scheduleModel->id}", $scheduleModel->action, $config, $report);
        $schedule->id = $scheduleModel->id;

        return $schedule;
    }

    /**
     * Get the id of this schedule
     *
     * @return  int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * Get the action hook class of this schedule
     *
     * @return  string
     */
    public function getAction(): string
    {
        return $this->action;
    }

    /**
     * Get the config of this schedule
     *
     * @return array
     */
    public function getConfig(): array
    {
        return $this->config;
    }

    /**
     * Get the report this schedule belongs to
     *
     * @return Report
     */
    public function getReport(): Report
    {
        return $this->report;
    }

    /**
     * Set the report this schedule belongs to
     *
     * @param Report $report
     *
     * @return $this
     */
    public function setReport(Report $report): self
    {
        $this->report = $report;

        return $this;
    }

    /**
     * Get the checksum of this schedule
     *
     * @return  string
     */
    public function getChecksum(): string
    {
        return md5(
            $this->getName() . $this->getReport()->getName() . $this->getAction() . Json::encode($this->getConfig()),
            true
        );
    }

    public function run(): ExtendedPromiseInterface
    {
        $deferred = new Promise\Deferred();
        Loop::futureTick(function () use ($deferred) {
            $action = $this->getAction();
            /** @var ActionHook $actionHook */
            $actionHook = new $action();

            try {
                $actionHook->execute($this->getReport(), $this->getConfig());
            } catch (Exception $err) {
                $deferred->reject($err);

                return;
            }

            $deferred->resolve();
        });

        /** @var ExtendedPromiseInterface $promise */
        $promise = $deferred->promise();

        return $promise;
    }
}