xref: /webtrees/app/Services/TimeoutService.php (revision ac71572d8462e396ed5a307f05b29381e49f9e6e)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Services;
21
22use Fisharebest\Webtrees\Registry;
23
24/**
25 * Check for PHP timeouts.
26 */
27class TimeoutService
28{
29    //Long-running scripts run in small chunks
30    private const TIME_LIMIT = 1.5;
31
32    // Seconds until we run out of time
33    private const TIME_UP_THRESHOLD = 3.0;
34
35    // The start time of the request
36    private float $start_time;
37
38    /**
39     * TimeoutService constructor.
40     *
41     * @param float|null $start_time
42     */
43    public function __construct(float $start_time = null)
44    {
45        $this->start_time = $start_time ?? Registry::timeFactory()->now();
46    }
47
48    /**
49     * Some long-running scripts need to know when to stop.
50     *
51     * @param float $threshold
52     *
53     * @return bool
54     */
55    public function isTimeNearlyUp(float $threshold = self::TIME_UP_THRESHOLD): bool
56    {
57        $max_execution_time = (int) ini_get('max_execution_time');
58
59        // If there's no time limit, then we can't run out of time.
60        if ($max_execution_time === 0) {
61            return false;
62        }
63
64        $now = Registry::timeFactory()->now();
65
66        return $now + $threshold > $this->start_time + (float) $max_execution_time;
67    }
68
69    /**
70     * Some long-running scripts are broken down into small chunks.
71     *
72     * @param float $limit
73     *
74     * @return bool
75     */
76    public function isTimeLimitUp(float $limit = self::TIME_LIMIT): bool
77    {
78        $now = Registry::timeFactory()->now();
79
80        return $now > $this->start_time + $limit;
81    }
82}
83