Remove sleep calls

This commit is contained in:
Romain Neutron
2013-09-04 14:51:00 +02:00
parent c8d415c562
commit 5ed53ccda3
5 changed files with 30 additions and 26 deletions

View File

@@ -47,6 +47,18 @@ class task_Scheduler
return $this; 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 * @throws Exception if scheduler is already running
@@ -106,7 +118,7 @@ class task_Scheduler
return; return;
} else { } else {
sleep(2); $this->sleep(2);
} }
} else { } else {
// locked // locked
@@ -167,9 +179,7 @@ class task_Scheduler
if (! $connwaslost) { if (! $connwaslost) {
$this->log(sprintf("Warning : abox connection lost, restarting in 10 min.")); $this->log(sprintf("Warning : abox connection lost, restarting in 10 min."));
} }
for ($i = 0; $i < 60 * 10; $i ++) { $this->sleep(60 * 10);
sleep(1);
}
try { try {
$conn = $this->dependencyContainer['phraseanet.appbox']->get_connection(); $conn = $this->dependencyContainer['phraseanet.appbox']->get_connection();
} catch (ErrorException $e) { } catch (ErrorException $e) {
@@ -362,7 +372,7 @@ class task_Scheduler
); );
if (is_resource($taskPoll[$tkey]["process"])) { 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) { 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 ++) { $this->sleep($sleeptime);
sleep(1);
}
} }
$sql = "UPDATE sitepreff SET schedstatus='stopped', schedpid='0'"; $sql = "UPDATE sitepreff SET schedstatus='stopped', schedpid='0'";

View File

@@ -544,13 +544,14 @@ abstract class task_abstract
if ($this->running) { // && $this->records_done == 0) if ($this->running) { // && $this->records_done == 0)
$when_started = time() - $when_started; $when_started = time() - $when_started;
if ($when_started < $this->period) { 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(); $s = $this->getState();
if ($s == self::STATE_TOSTOP) { if ($s == self::STATE_TOSTOP) {
$this->setState(self::STATE_STOPPED); $this->setState(self::STATE_STOPPED);
$this->running = FALSE; $this->running = FALSE;
} else { } else {
sleep(1); $this->sleep(1);
} }
} }
} }
@@ -570,8 +571,9 @@ abstract class task_abstract
throw new \InvalidArgumentException(sprintf("(%s) is not > 0")); throw new \InvalidArgumentException(sprintf("(%s) is not > 0"));
} }
while ($this->running && $nsec -- > 0) { $end = microtime(true) + $nsec;
sleep(1); while ($this->running && microtime(true) < $end) {
usleep(10000);
} }
} }

View File

@@ -34,10 +34,7 @@ abstract class task_appboxAbstract extends task_abstract
if ($this->getRunner() == self::RUNNER_SCHEDULER) { if ($this->getRunner() == self::RUNNER_SCHEDULER) {
$this->log(("Warning : abox connection lost, restarting in 10 min.")); $this->log(("Warning : abox connection lost, restarting in 10 min."));
// DON'T do sleep(600) because it prevents ticks ! $this->sleep(60 * 10);
for ($t = 60 * 10; $this->running && $t; $t --) {
sleep(1);
}
// because connection is lost we cannot change status to 'torestart' // because connection is lost we cannot change status to 'torestart'
// anyway the current status 'running' with no pid // anyway the current status 'running' with no pid
// will enforce the scheduler to restart the task // will enforce the scheduler to restart the task

View File

@@ -38,10 +38,7 @@ abstract class task_databoxAbstract extends task_abstract
if ($this->getRunner() == self::RUNNER_SCHEDULER) { if ($this->getRunner() == self::RUNNER_SCHEDULER) {
$this->log("appbox connection lost, restarting in 10 min.", self::LOG_ERROR); $this->log("appbox connection lost, restarting in 10 min.", self::LOG_ERROR);
// DON'T do sleep(600) because it prevents ticks ! $this->sleep(60 * 10);
for ($t = 60 * 10; $this->running && $t; $t--) {
sleep(1);
}
// because connection is lost we cannot change status to 'torestart' // because connection is lost we cannot change status to 'torestart'
// anyway the current status 'running' with no pid // anyway the current status 'running' with no pid
// will enforce the scheduler to restart the task // will enforce the scheduler to restart the task

View File

@@ -524,7 +524,7 @@ class task_period_cindexer extends task_abstract
socket_write($sock, 'Q', 1); socket_write($sock, 'Q', 1);
socket_write($sock, "\r\n", strlen("\r\n")); socket_write($sock, "\r\n", strlen("\r\n"));
for ($i = 0; $this->running && $i < 5; $i ++) { for ($i = 0; $this->running && $i < 5; $i ++) {
sleep(1); $this->sleep(1);
} }
$qsent = 'Q'; $qsent = 'Q';
$timetokill = time() + 10; $timetokill = time() + 10;
@@ -562,7 +562,7 @@ class task_period_cindexer extends task_abstract
} }
} }
for ($i = 0; $this->running && $i < 5; $i ++) { 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 { } else {
chdir(dirname(__FILE__)); chdir(dirname(__FILE__));
pcntl_exec($cmd, $args); pcntl_exec($cmd, $args);
sleep(2); $this->sleep(2);
} }
} else { } else {
// parent // parent
@@ -617,7 +617,7 @@ class task_period_cindexer extends task_abstract
if ($this->getState() == self::STATE_TOSTOP) { if ($this->getState() == self::STATE_TOSTOP) {
posix_kill($pid, ($sigsent = SIGINT)); posix_kill($pid, ($sigsent = SIGINT));
$timetokill = time() + 10; $timetokill = time() + 10;
sleep(2); $this->sleep(2);
} }
$status = NULL; $status = NULL;
@@ -641,7 +641,7 @@ class task_period_cindexer extends task_abstract
$this->log('Kill signal sent to Phrasea indexer'); $this->log('Kill signal sent to Phrasea indexer');
posix_kill($pid, ($sigsent = SIGKILL)); posix_kill($pid, ($sigsent = SIGKILL));
} }
sleep(2); $this->sleep(2);
} }
} // while running } // while running
} }
@@ -650,7 +650,7 @@ class task_period_cindexer extends task_abstract
private function run_with_exec($cmd, $args, $args_nopwd) private function run_with_exec($cmd, $args, $args_nopwd)
{ {
pcntl_exec($cmd, $args); pcntl_exec($cmd, $args);
sleep(2); $this->sleep(2);
} }
/** /**