diff --git a/lib/classes/task/Scheduler.php b/lib/classes/task/Scheduler.php index 32513b2fe9..0c2c69538a 100644 --- a/lib/classes/task/Scheduler.php +++ b/lib/classes/task/Scheduler.php @@ -47,6 +47,18 @@ class task_Scheduler return $this; } + protected function sleep($nsec) + { + $nsec = (integer) $nsec; + if ($nsec < 0) { + throw new \InvalidArgumentException(sprintf("(%s) is not > 0")); + } + + $end = microtime(true) + $nsec; + while (microtime(true) < $end) { + usleep(10000); + } + } /** * @throws Exception if scheduler is already running @@ -106,7 +118,7 @@ class task_Scheduler return; } else { - sleep(2); + $this->sleep(2); } } else { // locked @@ -167,9 +179,7 @@ class task_Scheduler if (! $connwaslost) { $this->log(sprintf("Warning : abox connection lost, restarting in 10 min.")); } - for ($i = 0; $i < 60 * 10; $i ++) { - sleep(1); - } + $this->sleep(60 * 10); try { $conn = $this->dependencyContainer['phraseanet.appbox']->get_connection(); } catch (ErrorException $e) { @@ -362,7 +372,7 @@ class task_Scheduler ); if (is_resource($taskPoll[$tkey]["process"])) { - sleep(2); // let the process lock and write it's pid + $this->sleep(2); } if (is_resource($taskPoll[$tkey]["process"]) && ($pid = $taskPoll[$tkey]['task']->getPID()) !== null) { @@ -568,9 +578,7 @@ class task_Scheduler } } - for ($i = 0; $i < $sleeptime; $i ++) { - sleep(1); - } + $this->sleep($sleeptime); } $sql = "UPDATE sitepreff SET schedstatus='stopped', schedpid='0'"; diff --git a/lib/classes/task/abstract.php b/lib/classes/task/abstract.php index 365f9f1923..93ddae7954 100644 --- a/lib/classes/task/abstract.php +++ b/lib/classes/task/abstract.php @@ -544,13 +544,14 @@ abstract class task_abstract if ($this->running) { // && $this->records_done == 0) $when_started = time() - $when_started; if ($when_started < $this->period) { - for ($t = $this->period - $when_started; $this->running && $t > 0; $t --) { // DON'T do sleep($this->period - $when_started) because it prevents ticks ! + for ($t = $this->period - $when_started; $this->running && $t > 0; $t --) { + // DON'T do sleep($this->period - $when_started) because it prevents ticks ! $s = $this->getState(); if ($s == self::STATE_TOSTOP) { $this->setState(self::STATE_STOPPED); $this->running = FALSE; } else { - sleep(1); + $this->sleep(1); } } } @@ -570,8 +571,9 @@ abstract class task_abstract throw new \InvalidArgumentException(sprintf("(%s) is not > 0")); } - while ($this->running && $nsec -- > 0) { - sleep(1); + $end = microtime(true) + $nsec; + while ($this->running && microtime(true) < $end) { + usleep(10000); } } diff --git a/lib/classes/task/appboxAbstract.php b/lib/classes/task/appboxAbstract.php index 38cf9db296..fc79b69b15 100644 --- a/lib/classes/task/appboxAbstract.php +++ b/lib/classes/task/appboxAbstract.php @@ -34,10 +34,7 @@ abstract class task_appboxAbstract extends task_abstract if ($this->getRunner() == self::RUNNER_SCHEDULER) { $this->log(("Warning : abox connection lost, restarting in 10 min.")); - // DON'T do sleep(600) because it prevents ticks ! - for ($t = 60 * 10; $this->running && $t; $t --) { - sleep(1); - } + $this->sleep(60 * 10); // because connection is lost we cannot change status to 'torestart' // anyway the current status 'running' with no pid // will enforce the scheduler to restart the task diff --git a/lib/classes/task/databoxAbstract.php b/lib/classes/task/databoxAbstract.php index c8258fb6e0..c8eb02befd 100644 --- a/lib/classes/task/databoxAbstract.php +++ b/lib/classes/task/databoxAbstract.php @@ -38,10 +38,7 @@ abstract class task_databoxAbstract extends task_abstract if ($this->getRunner() == self::RUNNER_SCHEDULER) { $this->log("appbox connection lost, restarting in 10 min.", self::LOG_ERROR); - // DON'T do sleep(600) because it prevents ticks ! - for ($t = 60 * 10; $this->running && $t; $t--) { - sleep(1); - } + $this->sleep(60 * 10); // because connection is lost we cannot change status to 'torestart' // anyway the current status 'running' with no pid // will enforce the scheduler to restart the task diff --git a/lib/classes/task/period/cindexer.php b/lib/classes/task/period/cindexer.php index 399000bd2c..716769ba6e 100644 --- a/lib/classes/task/period/cindexer.php +++ b/lib/classes/task/period/cindexer.php @@ -524,7 +524,7 @@ class task_period_cindexer extends task_abstract socket_write($sock, 'Q', 1); socket_write($sock, "\r\n", strlen("\r\n")); for ($i = 0; $this->running && $i < 5; $i ++) { - sleep(1); + $this->sleep(1); } $qsent = 'Q'; $timetokill = time() + 10; @@ -562,7 +562,7 @@ class task_period_cindexer extends task_abstract } } for ($i = 0; $this->running && $i < 5; $i ++) { - sleep(1); + $this->sleep(1); } } @@ -594,7 +594,7 @@ class task_period_cindexer extends task_abstract } else { chdir(dirname(__FILE__)); pcntl_exec($cmd, $args); - sleep(2); + $this->sleep(2); } } else { // parent @@ -617,7 +617,7 @@ class task_period_cindexer extends task_abstract if ($this->getState() == self::STATE_TOSTOP) { posix_kill($pid, ($sigsent = SIGINT)); $timetokill = time() + 10; - sleep(2); + $this->sleep(2); } $status = NULL; @@ -641,7 +641,7 @@ class task_period_cindexer extends task_abstract $this->log('Kill signal sent to Phrasea indexer'); posix_kill($pid, ($sigsent = SIGKILL)); } - sleep(2); + $this->sleep(2); } } // while running } @@ -650,7 +650,7 @@ class task_period_cindexer extends task_abstract private function run_with_exec($cmd, $args, $args_nopwd) { pcntl_exec($cmd, $args); - sleep(2); + $this->sleep(2); } /**