GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AbstractWorker::getScheduler()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace Scheduler\Worker;
4
5
use DateInterval;
6
use DateTime;
7
use Scheduler\Exception\SchedulerException;
8
use Scheduler\JobRunner\JobRunnerInterface;
9
use Scheduler\SchedulerInterface;
10
11
/**
12
 * Class AbstractWorker
13
 * @package Scheduler\Worker
14
 * @author Aleh Hutnikau, <[email protected]>
15
 */
16
abstract class AbstractWorker implements WorkerInterface
17
{
18
    const DEFAULT_ITERATION_INTERVAL = 'PT60S';
19
20
    /** @var DateTime */
21
    private $from;
22
23
    /** @var DateInterval */
24
    private $interval;
25
26
    /** @var bool */
27
    private $shutdown = false;
28
29
    /** @var integer */
30
    private $iteration = 0;
31
32
    /** @var integer */
33
    private $maxIterations = 1000;
34
35
    /**
36
     * @param integer $startTime
37
     * @param string $interval
38
     * @return mixed|string
39
     * @throws SchedulerException
40
     */
41 2
    public function run($startTime, $interval)
42
    {
43 2
        $this->init($startTime, $interval);
44 1
        $jobRunner = $this->getJobRunner();
45
46 1
        $from = clone($this->from);
47 1
        $oneSecondInterval = new \DateInterval('PT1S');
48 1
        $reports = [];
49 1
        while ($this->isRunning()) {
50 1
            $to = new DateTime();
51 1
            $to->setTimestamp(time());
52 1
            $to->setTimezone(new \DateTimeZone('UTC'));
53 1
            $reports = $jobRunner->run($this->getScheduler(), $from, $to, true);
54 1
            $from = clone($to);
55 1
            $from->add($oneSecondInterval);
56 1
            sleep($this->getSeconds($this->interval));
57 1
            $this->iteration++;
58 1
        }
59
60 1
        return $reports;
61
    }
62
63
    /**
64
     * @return JobRunnerInterface
65
     */
66
    abstract protected function getJobRunner();
67
68
    /**
69
     * @return SchedulerInterface
70
     */
71
    abstract protected function getScheduler();
72
73
    /**
74
     * @return bool
75
     */
76 1
    public function isRunning()
77
    {
78 1
        if (function_exists('pcntl_signal_dispatch')) {pcntl_signal_dispatch();};
79
80 1
        if ($this->iteration >= $this->getMaxIterations()) {
81 1
            $this->shutdown();
82 1
        }
83
84 1
        if ($this->shutdown) {
85 1
            return false;
86
        }
87
88 1
        return true;
89
    }
90
91
    /**
92
     * Set marker to shutdown after finishing current iteration
93
     */
94 1
    public function shutdown()
95
    {
96 1
        $this->shutdown = true;
97 1
    }
98
99
    /**
100
     * @param $iterations
101
     * @throws SchedulerException
102
     * @return mixed|void
103
     */
104 2
    public function setMaxIterations($iterations)
105
    {
106 2
        if (!is_integer($iterations)) {
107 1
            throw new SchedulerException('$iterations parameter must be integer');
108
        }
109 1
        $this->maxIterations = $iterations;
110 1
    }
111
112
    /**
113
     * Get amount of seconds in date interval
114
     *
115
     * @param DateInterval $interval
116
     * @return int
117
     * @throws \Exception
118
     */
119 1
    private function getSeconds(DateInterval $interval)
120
    {
121 1
        $date = new \DateTimeImmutable('now', new \DateTimeZone('UTC'));
122 1
        $date2 = $date->add($interval);
123 1
        return $date2->getTimestamp() - $date->getTimestamp();
124
    }
125
126
    /**
127
     * @param integer $startTime
128
     * @param string $interval
129
     * @throws
130
     */
131 2
    private function init($startTime, $interval)
132
    {
133 2
        if (!is_numeric($startTime)) {
134 1
            throw new SchedulerException('Start time parameter must be numeric');
135
        }
136 1
        $this->from = new DateTime();
137 1
        $this->from->setTimestamp($startTime);
138 1
        $this->from->setTimezone(new \DateTimeZone('UTC'));
139
140 1
        $this->interval = new \DateInterval(self::DEFAULT_ITERATION_INTERVAL);
141 1
        if ($interval !== null) {
142 1
            $this->interval = new \DateInterval($interval);
143 1
        }
144
145 1
        $this->registerSigHandlers();
146 1
    }
147
148
    /**
149
     * Register signal handlers that a worker should respond to.
150
     *
151
     * TERM/INT/QUIT: Shutdown after the current job is finished then exit.
152
     */
153 1
    private function registerSigHandlers()
154
    {
155
        declare(ticks = 1);
156 1
        if (function_exists('pcntl_signal')) {pcntl_signal(SIGTERM, [$this, 'shutdown']);}
157 1
        if (function_exists('pcntl_signal')) {pcntl_signal(SIGINT, [$this, 'shutdown']);}
158 1
        if (function_exists('pcntl_signal')) {pcntl_signal(SIGQUIT, [$this, 'shutdown']);}
159 1
    }
160
161
    /**
162
     * @return integer
163
     */
164 1
    public function getMaxIterations()
165
    {
166 1
        return $this->maxIterations;
167
    }
168
}