mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-13 13:03:20 +00:00
fix #1496
This commit is contained in:
@@ -34,11 +34,15 @@ class task_Scheduler
|
|||||||
private $method;
|
private $method;
|
||||||
private $dependencyContainer;
|
private $dependencyContainer;
|
||||||
|
|
||||||
|
private $schedstatus;
|
||||||
|
|
||||||
public function __construct(Application $application, Logger $logger)
|
public function __construct(Application $application, Logger $logger)
|
||||||
{
|
{
|
||||||
declare(ticks = 1);
|
declare(ticks = 1);
|
||||||
$this->dependencyContainer = $application;
|
$this->dependencyContainer = $application;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
|
|
||||||
|
$this->schedstatus = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function log($message)
|
protected function log($message)
|
||||||
@@ -54,10 +58,21 @@ class task_Scheduler
|
|||||||
*/
|
*/
|
||||||
public function sigHandler($signal)
|
public function sigHandler($signal)
|
||||||
{
|
{
|
||||||
$status = null;
|
switch ($signal) {
|
||||||
$pid = pcntl_wait($status);
|
case SIGCHLD:
|
||||||
$exitstatus = pcntl_wexitstatus($status);
|
$status = null;
|
||||||
$this->log(sprintf("sigchild %s received from pid=%s, status=%s, exitstatus=%s\n", $signal, $pid, var_export($status, true), $exitstatus));
|
$pid = pcntl_wait($status);
|
||||||
|
$exitstatus = pcntl_wexitstatus($status);
|
||||||
|
$this->log(sprintf("SIGCHLD (%s) received from pid=%s, status=%s, exitstatus=%s", $signal, $pid, var_export($status, true), $exitstatus));
|
||||||
|
break;
|
||||||
|
case SIGINT: // ctrl C
|
||||||
|
$this->log(sprintf("SIGINT (%s) Ctrl-C received, schedstatus='tostop'", $signal));
|
||||||
|
$this->schedstatus = 'tostop';
|
||||||
|
break;
|
||||||
|
case SIGTERM:
|
||||||
|
$this->log(sprintf("SIGTERM (%s) received but ignored, http timeout ?", $signal));
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run()
|
public function run()
|
||||||
@@ -87,6 +102,8 @@ class task_Scheduler
|
|||||||
// pcntl_signal(SIGCHLD, SIG_IGN); // no zombies but no returnValue
|
// pcntl_signal(SIGCHLD, SIG_IGN); // no zombies but no returnValue
|
||||||
// pcntl_signal(SIGCHLD, SIG_DFL); // with "declare(ticks=1)" returnValue ok but zombies
|
// pcntl_signal(SIGCHLD, SIG_DFL); // with "declare(ticks=1)" returnValue ok but zombies
|
||||||
pcntl_signal(SIGCHLD, array($this, 'sigHandler')); // ok
|
pcntl_signal(SIGCHLD, array($this, 'sigHandler')); // ok
|
||||||
|
pcntl_signal(SIGINT, array($this, 'sigHandler'));
|
||||||
|
pcntl_signal(SIGTERM, array($this, 'sigHandler'));
|
||||||
|
|
||||||
$this->method = self::METHOD_FORK;
|
$this->method = self::METHOD_FORK;
|
||||||
}
|
}
|
||||||
@@ -147,11 +164,11 @@ class task_Scheduler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$schedstatus = 'started';
|
$this->schedstatus = 'started';
|
||||||
$runningtask = 0;
|
$runningtask = 0;
|
||||||
$connwaslost = false;
|
$connwaslost = false;
|
||||||
|
|
||||||
while ($schedstatus == 'started' || $runningtask > 0) {
|
while ($this->schedstatus == 'started' || $runningtask > 0) {
|
||||||
while (1) {
|
while (1) {
|
||||||
try {
|
try {
|
||||||
assert(is_object($conn));
|
assert(is_object($conn));
|
||||||
@@ -189,23 +206,25 @@ class task_Scheduler
|
|||||||
$connwaslost = false;
|
$connwaslost = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$schedstatus = '';
|
if ($this->schedstatus == "started") {
|
||||||
$row = NULL;
|
$this->schedstatus = '';
|
||||||
try {
|
$row = NULL;
|
||||||
$sql = "SELECT schedstatus FROM sitepreff";
|
try {
|
||||||
$stmt = $conn->prepare($sql);
|
$sql = "SELECT schedstatus FROM sitepreff";
|
||||||
$stmt->execute();
|
$stmt = $conn->prepare($sql);
|
||||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
$stmt->execute();
|
||||||
$stmt->closeCursor();
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
} catch (ErrorException $e) {
|
$stmt->closeCursor();
|
||||||
continue;
|
} catch (ErrorException $e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($row) {
|
||||||
|
$this->schedstatus = $row["schedstatus"];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($row) {
|
if ($this->schedstatus == 'tostop') {
|
||||||
$schedstatus = $row["schedstatus"];
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($schedstatus == 'tostop') {
|
|
||||||
$sql = 'UPDATE sitepreff SET schedstatus = "stopping"';
|
$sql = 'UPDATE sitepreff SET schedstatus = "stopping"';
|
||||||
$stmt = $conn->prepare($sql);
|
$stmt = $conn->prepare($sql);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
@@ -327,7 +346,7 @@ class task_Scheduler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($schedstatus == 'started') {
|
if ($this->schedstatus == 'started') {
|
||||||
$taskPoll[$tkey]["task"]->setState(task_abstract::STATE_TOSTART);
|
$taskPoll[$tkey]["task"]->setState(task_abstract::STATE_TOSTART);
|
||||||
}
|
}
|
||||||
// trick to start the task immediatly : DON'T break if ending with 'tostart'
|
// trick to start the task immediatly : DON'T break if ending with 'tostart'
|
||||||
@@ -338,7 +357,7 @@ class task_Scheduler
|
|||||||
|
|
||||||
case task_abstract::STATE_TOSTART:
|
case task_abstract::STATE_TOSTART:
|
||||||
// if scheduler is 'tostop', don't launch a new task !
|
// if scheduler is 'tostop', don't launch a new task !
|
||||||
if ($schedstatus != 'started') {
|
if ($this->schedstatus != 'started') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -567,10 +586,16 @@ class task_Scheduler
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(function_exists('pcntl_sigprocmask')) {
|
||||||
for ($i = 0; $i < $sleeptime; $i ++) {
|
@pcntl_sigprocmask(SIG_BLOCK, array(SIGCHLD));
|
||||||
|
}
|
||||||
|
sleep(1);
|
||||||
|
for ($i = 0; $this->schedstatus=='started' && $i < $sleeptime; $i++) {
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}
|
}
|
||||||
|
if(function_exists('pcntl_sigprocmask')) {
|
||||||
|
@pcntl_sigprocmask(SIG_UNBLOCK, array(SIGCHLD));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$sql = "UPDATE sitepreff SET schedstatus='stopped', schedpid='0'";
|
$sql = "UPDATE sitepreff SET schedstatus='stopped', schedpid='0'";
|
||||||
|
@@ -161,6 +161,13 @@ class task_manager
|
|||||||
if (!in_array($status, $av_status))
|
if (!in_array($status, $av_status))
|
||||||
throw new Exception(sprintf('unknown status `%s` ', $status));
|
throw new Exception(sprintf('unknown status `%s` ', $status));
|
||||||
|
|
||||||
|
if ($status == self::STATE_TOSTOP && function_exists('posix_kill')) {
|
||||||
|
$gs = $this->getSchedulerState();
|
||||||
|
if ($gs['pid'] !== NULL) {
|
||||||
|
@posix_kill($gs['pid'], 2); // 2 = SIGINT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$sql = "UPDATE sitepreff SET schedstatus = :schedstatus, schedqtime=NOW()";
|
$sql = "UPDATE sitepreff SET schedstatus = :schedstatus, schedqtime=NOW()";
|
||||||
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
||||||
$stmt->execute(array(':schedstatus' => $status));
|
$stmt->execute(array(':schedstatus' => $status));
|
||||||
|
Reference in New Issue
Block a user