[CORE] Use monotonic time via hrtime() where applicable

The realtime clock is not reliable when calculating elapsed time.
This commit is contained in:
Alexei Sorokin
2020-01-07 19:48:13 +03:00
committed by Diogo Peralta Cordeiro
parent d467370efb
commit 3951ccbef7
11 changed files with 364 additions and 290 deletions

View File

@@ -1,17 +1,34 @@
<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/**
* GNU social cron-on-visit class
*
* Keeps track, through Config dataobject class, of relative time since the
* Keeps track, through Config dataobject class, of relative time since the
* last run in order to to run event handlers with certain intervals.
*
* @category Cron
* @package GNUsocial
* @author Mikael Nordfeldth <mmn@hethane.se>
* @copyright 2013 Free Software Foundation, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
* @copyright 2013 Free Software Foundation, Inc http://www.fsf.or
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
defined('GNUSOCIAL') || die();
class Cronish
{
/**
@@ -28,7 +45,7 @@ class Cronish
'daily' => 86400,
'weekly' => 604800);
foreach($timers as $name=>$interval) {
foreach ($timers as $name => $interval) {
$run = false;
$lastrun = new Config();
@@ -37,15 +54,15 @@ class Cronish
$found = $lastrun->find(true);
if (!$found) {
$lastrun->value = time();
$lastrun->value = hrtime(true);
if ($lastrun->insert() === false) {
common_log(LOG_WARNING, "Could not save 'cron' setting '{$name}'");
continue;
}
$run = true;
} elseif ($lastrun->value < time() - $interval) {
} elseif ($lastrun->value < hrtime(true) - $interval * 1000000000) {
$orig = clone($lastrun);
$lastrun->value = time();
$lastrun->value = hrtime(true);
$lastrun->update($orig);
$run = true;
}