summaryrefslogtreecommitdiffstats
path: root/library/Reporting/Scheduler.php
blob: 1b8d9f6de3252641cac7a8fd861e4b6f0e9c8e31 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
// Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2

namespace Icinga\Module\Reporting;

use Cron\CronExpression;
use ipl\Sql\Connection;
use ipl\Sql\Select;
use React\EventLoop\Factory as Loop;

function datetime_get_time_of_day(\DateTime $dateTime)
{
    $midnight = clone $dateTime;
    $midnight->modify('midnight');

    $diff = $midnight->diff($dateTime);

    return $diff->h * 60 * 60 + $diff->i * 60 + $diff->s;
}

class Scheduler
{
    protected $db;

    protected $loop;

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

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

    public function __construct(Connection $db)
    {
        $this->db = $db;
        $this->loop = Loop::create();
    }

    public function run()
    {
        $updateTimers = function () use (&$updateTimers) {
            $this->updateTimers();

            $this->loop->addTimer(60, $updateTimers);
        };

        $this->loop->futureTick($updateTimers);

        $this->loop->run();
    }

    protected function fetchSchedules()
    {
        $schedules = [];

        $select = (new Select())
            ->from('schedule')
            ->columns('*');

        foreach ($this->db->select($select) as $row) {
            $schedule = (new Schedule())
                ->setId((int) $row->id)
                ->setReportId((int) $row->report_id)
                ->setAction($row->action)
                ->setConfig(\json_decode($row->config, true))
                ->setStart((new \DateTime())->setTimestamp((int) $row->start / 1000))
                ->setFrequency($row->frequency);

            $schedules[$schedule->getChecksum()] = $schedule;
        }

        return $schedules;
    }

    protected function updateTimers()
    {
        $schedules = $this->fetchSchedules();

        $remove = \array_diff_key($this->schedules, $schedules);

        foreach ($remove as $schedule) {
            printf("Removing job %s.\n", "Schedule {$schedule->getId()}");

            $checksum = $schedule->getChecksum();

            if (isset($this->timers[$checksum])) {
                $this->loop->cancelTimer($this->timers[$checksum]);
                unset($this->timers[$checksum]);
            } else {
                printf("Can't find timer for job %s.\n", $checksum);
            }
        }

        $add = \array_diff_key($schedules, $this->schedules);

        foreach ($add as $schedule) {
            $this->add($schedule);
        }

        $this->schedules = $schedules;
    }


    protected function add(Schedule $schedule)
    {
        $name = "Schedule {$schedule->getId()}";
        $frequency = $schedule->getFrequency();
        $start = clone $schedule->getStart();
        $callback = function () use ($schedule) {
            $actionClass = $schedule->getAction();
            /** @var ActionHook $action */
            $action = new $actionClass;

            $action->execute(
                Report::fromDb($schedule->getReportId()),
                $schedule->getConfig()
            );
        };

        switch ($frequency) {
            case 'minutely':
                $modify = '+1 minute';
                break;
            case 'hourly':
                $modify = '+1 hour';
                break;
            case 'daily':
                $modify = '+1 day';
                break;
            case 'weekly':
                $modify = '+1 week';
                break;
            case 'monthly':
                $modify = '+1 month';
                break;
            default:
                throw new \InvalidArgumentException('Invalid frequency.');
        }

        $now = new \DateTime();

        if ($start < $now) {
//            printf("Scheduling job %s to run immediately.\n", $name);
//            $this->loop->futureTick($callback);

            while ($start < $now) {
                $start->modify($modify);
            }
        }

        $next = clone $start;
        $next->modify($modify);
        $interval = $next->getTimestamp() - $start->getTimestamp();

        $current = $start->getTimestamp() - $now->getTimestamp();

        printf("Scheduling job %s to run at %s.\n", $name, $start->format('Y-m-d H:i:s'));

        $loop = function () use (&$loop, $name, $callback, $interval, $schedule) {
            $callback();

            $nextRun = (new \DateTime())
                ->add(new \DateInterval("PT{$interval}S"));

            printf("Scheduling job %s to run at %s.\n", $name, $nextRun->format('Y-m-d H:i:s'));

            $timer = $this->loop->addTimer($interval, $loop);

            $this->timers[$schedule->getChecksum()] = $timer;
        };

        $timer = $this->loop->addTimer($current, $loop);

        $this->timers[$schedule->getChecksum()] = $timer;
    }
}