mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-18 07:23:13 +00:00
Merge with master
This commit is contained in:
@@ -19,9 +19,11 @@ use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class task_Scheduler
|
||||
{
|
||||
|
||||
const TASKDELAYTOQUIT = 60;
|
||||
|
||||
protected $output;
|
||||
protected static $connection;
|
||||
|
||||
protected function log($message)
|
||||
{
|
||||
@@ -31,12 +33,12 @@ class task_Scheduler
|
||||
}
|
||||
|
||||
$registry = registry::get_instance();
|
||||
$logdir = $registry->get('GV_RootPath') . 'logs/';
|
||||
$logdir = $registry->get('GV_RootPath') . 'logs/';
|
||||
|
||||
logs::rotate($logdir . "scheduler.log");
|
||||
|
||||
$date_obj = new DateTime();
|
||||
$message = sprintf("%s %s \n", $date_obj->format(DATE_ATOM), $message);
|
||||
$message = sprintf("%s %s \n", $date_obj->format(DATE_ATOM), $message);
|
||||
|
||||
file_put_contents($logdir . "scheduler.log", $message, FILE_APPEND);
|
||||
|
||||
@@ -52,7 +54,7 @@ class task_Scheduler
|
||||
{
|
||||
require_once __DIR__ . '/../../bootstrap.php';
|
||||
$this->output = $output;
|
||||
$appbox = appbox::get_instance();
|
||||
$appbox = appbox::get_instance();
|
||||
$registry = $appbox->get_registry();
|
||||
|
||||
$system = system_server::get_platform();
|
||||
@@ -61,7 +63,7 @@ class task_Scheduler
|
||||
|
||||
for ($try = 0; true; $try++)
|
||||
{
|
||||
$schedlock = fopen(($lockfile = ($lockdir . 'scheduler.lock')), 'a+');
|
||||
$schedlock = fopen(($lockfile = ($lockdir . 'scheduler.lock')), 'a+');
|
||||
if (flock($schedlock, LOCK_EX | LOCK_NB) != true)
|
||||
{
|
||||
$this->log(sprintf("failed to lock '%s' (try=%s/4)", $lockfile, $try));
|
||||
@@ -94,7 +96,7 @@ class task_Scheduler
|
||||
|
||||
$sleeptime = 3;
|
||||
|
||||
$sql = "UPDATE sitepreff SET schedstatus='started', schedpid = :pid";
|
||||
$sql = "UPDATE sitepreff SET schedstatus='started', schedpid = :pid";
|
||||
$stmt = $conn->prepare($sql);
|
||||
$stmt->execute(array(':pid' => getmypid()));
|
||||
$stmt->closeCursor();
|
||||
@@ -137,14 +139,14 @@ class task_Scheduler
|
||||
$this->log(sprintf("Warning : abox connection lost, restarting in 10 min."));
|
||||
}
|
||||
sleep(60 * 10);
|
||||
$conn = self::get_connection();
|
||||
$conn = self::get_connection();
|
||||
$connwaslost = true;
|
||||
}
|
||||
if ($connwaslost)
|
||||
{
|
||||
$this->log("abox connection restored");
|
||||
|
||||
$sql = 'UPDATE task SET crashed=0';
|
||||
$sql = 'UPDATE task SET crashed=0';
|
||||
$stmt = $conn->prepare($sql);
|
||||
$stmt->execute();
|
||||
$stmt->closeCursor();
|
||||
@@ -153,10 +155,10 @@ class task_Scheduler
|
||||
}
|
||||
|
||||
$schedstatus = '';
|
||||
$sql = "SELECT schedstatus FROM sitepreff";
|
||||
$stmt = $conn->prepare($sql);
|
||||
$sql = "SELECT schedstatus FROM sitepreff";
|
||||
$stmt = $conn->prepare($sql);
|
||||
$stmt->execute();
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
$stmt->closeCursor();
|
||||
|
||||
if ($row)
|
||||
@@ -166,13 +168,13 @@ class task_Scheduler
|
||||
|
||||
if ($schedstatus == 'tostop')
|
||||
{
|
||||
$sql = 'UPDATE sitepreff SET schedstatus = "stopping"';
|
||||
$sql = 'UPDATE sitepreff SET schedstatus = "stopping"';
|
||||
$stmt = $conn->prepare($sql);
|
||||
$stmt->execute();
|
||||
$stmt->closeCursor();
|
||||
|
||||
// if scheduler is stopped, stop the tasks
|
||||
$sql = 'UPDATE task2 SET status="tostop" WHERE status != "stopped" and status != "manual"';
|
||||
$sql = 'UPDATE task2 SET status="tostop" WHERE status != "stopped" and status != "manual"';
|
||||
$stmt = $conn->prepare($sql);
|
||||
$stmt->execute();
|
||||
$stmt->closeCursor();
|
||||
@@ -192,10 +194,10 @@ class task_Scheduler
|
||||
$ttask[$tkey]["todel"] = true;
|
||||
}
|
||||
|
||||
$sql = "SELECT * FROM task2";
|
||||
$sql = "SELECT * FROM task2";
|
||||
$stmt = $conn->prepare($sql);
|
||||
$stmt->execute();
|
||||
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
$stmt->closeCursor();
|
||||
|
||||
foreach ($task_manager->get_tasks(true) as $task)
|
||||
@@ -216,27 +218,27 @@ class task_Scheduler
|
||||
case "WINDOWS":
|
||||
case "LINUX":
|
||||
$cmd = $phpcli . ' -f '
|
||||
. $registry->get('GV_RootPath')
|
||||
. "bin/console task:run "
|
||||
. $task->get_task_id()
|
||||
. " --runner=scheduler ";
|
||||
. $registry->get('GV_RootPath')
|
||||
. "bin/console task:run "
|
||||
. $task->get_task_id()
|
||||
. " --runner=scheduler ";
|
||||
break;
|
||||
}
|
||||
|
||||
$ttask[$tkey] = array(
|
||||
"task" => $task,
|
||||
"current_status" => $task->get_status(),
|
||||
"process" => null,
|
||||
"cmd" => $cmd,
|
||||
"killat" => null,
|
||||
"pipes" => null
|
||||
"task" => $task,
|
||||
"current_status" => $task->get_status(),
|
||||
"process" => null,
|
||||
"cmd" => $cmd,
|
||||
"killat" => null,
|
||||
"pipes" => null
|
||||
);
|
||||
$this->log(
|
||||
sprintf(
|
||||
"new Task %s, status=%s"
|
||||
, $ttask[$tkey]["task"]->get_task_id()
|
||||
, $task->get_status()
|
||||
)
|
||||
sprintf(
|
||||
"new Task %s, status=%s"
|
||||
, $ttask[$tkey]["task"]->get_task_id()
|
||||
, $task->get_status()
|
||||
)
|
||||
);
|
||||
}
|
||||
else
|
||||
@@ -244,17 +246,17 @@ class task_Scheduler
|
||||
if ($ttask[$tkey]["current_status"] != $task->get_status())
|
||||
{
|
||||
$this->log(
|
||||
sprintf(
|
||||
"Task %s, oldstatus=%s, newstatus=%s"
|
||||
, $ttask[$tkey]["task"]->get_task_id()
|
||||
, $ttask[$tkey]["current_status"]
|
||||
, $task->get_status()
|
||||
)
|
||||
sprintf(
|
||||
"Task %s, oldstatus=%s, newstatus=%s"
|
||||
, $ttask[$tkey]["task"]->get_task_id()
|
||||
, $ttask[$tkey]["current_status"]
|
||||
, $task->get_status()
|
||||
)
|
||||
);
|
||||
$ttask[$tkey]["current_status"] = $task->get_status();
|
||||
}
|
||||
|
||||
$ttask[$tkey]["task"] = $task;
|
||||
$ttask[$tkey]["task"] = $task;
|
||||
}
|
||||
$ttask[$tkey]["todel"] = false;
|
||||
}
|
||||
@@ -275,8 +277,8 @@ class task_Scheduler
|
||||
$runningtask = 0;
|
||||
|
||||
$common_status = array(
|
||||
task_abstract::STATUS_STARTED
|
||||
, task_abstract::RETURNSTATUS_STOPPED
|
||||
task_abstract::STATUS_STARTED
|
||||
, task_abstract::RETURNSTATUS_STOPPED
|
||||
);
|
||||
|
||||
foreach ($ttask as $tkey => $tv)
|
||||
@@ -284,21 +286,21 @@ class task_Scheduler
|
||||
if (!in_array($ttask[$tkey]["task"]->get_status(), $common_status))
|
||||
{
|
||||
$this->log(
|
||||
sprintf(
|
||||
'task %s has status %s'
|
||||
, $ttask[$tkey]["task"]->get_task_id()
|
||||
, $ttask[$tkey]["task"]->get_status()
|
||||
)
|
||||
sprintf(
|
||||
'task %s has status %s'
|
||||
, $ttask[$tkey]["task"]->get_task_id()
|
||||
, $ttask[$tkey]["task"]->get_status()
|
||||
)
|
||||
);
|
||||
}
|
||||
switch ($ttask[$tkey]["task"]->get_status())
|
||||
{
|
||||
default:
|
||||
$this->log(
|
||||
sprintf(
|
||||
'Unknow status `%s`'
|
||||
, $ttask[$tkey]["task"]->get_status()
|
||||
)
|
||||
sprintf(
|
||||
'Unknow status `%s`'
|
||||
, $ttask[$tkey]["task"]->get_status()
|
||||
)
|
||||
);
|
||||
break;
|
||||
case task_abstract::RETURNSTATUS_TORESTART:
|
||||
@@ -317,31 +319,31 @@ class task_Scheduler
|
||||
if ($schedstatus == 'started' && !$ttask[$tkey]["process"])
|
||||
{
|
||||
$descriptors = array(
|
||||
1 => array("pipe", "w")
|
||||
, 2 => array("pipe", "w")
|
||||
1 => array("pipe", "w")
|
||||
, 2 => array("pipe", "w")
|
||||
);
|
||||
|
||||
if ($log_tasks === true)
|
||||
{
|
||||
$descriptors[1] = array(
|
||||
"file"
|
||||
, $logdir . "task_$tkey.log"
|
||||
, "a+"
|
||||
"file"
|
||||
, $logdir . "task_$tkey.log"
|
||||
, "a+"
|
||||
);
|
||||
$descriptors[2] = array(
|
||||
"file"
|
||||
, $logdir . "task_$tkey.error.log"
|
||||
, "a+"
|
||||
"file"
|
||||
, $logdir . "task_$tkey.error.log"
|
||||
, "a+"
|
||||
);
|
||||
}
|
||||
|
||||
$ttask[$tkey]["process"] = proc_open(
|
||||
$ttask[$tkey]["cmd"]
|
||||
, $descriptors
|
||||
, $ttask[$tkey]["pipes"]
|
||||
, $registry->get('GV_RootPath') . "bin/"
|
||||
, null
|
||||
, array('bypass_shell' => true)
|
||||
$ttask[$tkey]["cmd"]
|
||||
, $descriptors
|
||||
, $ttask[$tkey]["pipes"]
|
||||
, $registry->get('GV_RootPath') . "bin/"
|
||||
, null
|
||||
, array('bypass_shell' => true)
|
||||
);
|
||||
|
||||
if (is_resource($ttask[$tkey]["process"]))
|
||||
@@ -354,12 +356,12 @@ class task_Scheduler
|
||||
if ($ttask[$tkey]['task']->get_pid() !== null)
|
||||
{
|
||||
$this->log(
|
||||
sprintf(
|
||||
"Task %s '%s' started (pid=%s)"
|
||||
, $ttask[$tkey]['task']->get_task_id()
|
||||
, $ttask[$tkey]["cmd"]
|
||||
, $ttask[$tkey]['task']->get_pid()
|
||||
)
|
||||
sprintf(
|
||||
"Task %s '%s' started (pid=%s)"
|
||||
, $ttask[$tkey]['task']->get_task_id()
|
||||
, $ttask[$tkey]["cmd"]
|
||||
, $ttask[$tkey]['task']->get_pid()
|
||||
)
|
||||
);
|
||||
$runningtask++;
|
||||
}
|
||||
@@ -373,12 +375,12 @@ class task_Scheduler
|
||||
$ttask[$tkey]["process"] = null;
|
||||
|
||||
$this->log(
|
||||
sprintf(
|
||||
"Task %s '%s' failed to start %d times"
|
||||
, $ttask[$tkey]["task"]->get_task_id()
|
||||
, $ttask[$tkey]["cmd"]
|
||||
, $ttask[$tkey]["task"]->get_crash_counter()
|
||||
)
|
||||
sprintf(
|
||||
"Task %s '%s' failed to start %d times"
|
||||
, $ttask[$tkey]["task"]->get_task_id()
|
||||
, $ttask[$tkey]["cmd"]
|
||||
, $ttask[$tkey]["task"]->get_crash_counter()
|
||||
)
|
||||
);
|
||||
|
||||
$ttask[$tkey]["task"]->increment_crash_counter();
|
||||
@@ -409,7 +411,7 @@ class task_Scheduler
|
||||
if ($proc_status['running'])
|
||||
$runningtask++;
|
||||
else
|
||||
$crashed = true;
|
||||
$crashed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -431,11 +433,11 @@ class task_Scheduler
|
||||
$ttask[$tkey]["process"] = null;
|
||||
|
||||
$this->log(
|
||||
sprintf(
|
||||
"Task %s crashed %d times"
|
||||
, $ttask[$tkey]["task"]->get_task_id()
|
||||
, $ttask[$tkey]["task"]->get_crash_counter()
|
||||
)
|
||||
sprintf(
|
||||
"Task %s crashed %d times"
|
||||
, $ttask[$tkey]["task"]->get_task_id()
|
||||
, $ttask[$tkey]["task"]->get_crash_counter()
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
@@ -457,7 +459,7 @@ class task_Scheduler
|
||||
{
|
||||
if ($ttask[$tkey]["killat"] === NULL)
|
||||
$ttask[$tkey]["killat"] = time() + self::TASKDELAYTOQUIT;
|
||||
if (($dt = $ttask[$tkey]["killat"] - time()) < 0)
|
||||
if (($dt = $ttask[$tkey]["killat"] - time()) < 0)
|
||||
{
|
||||
$ppid = $ttask[$tkey]['task']->get_pid();
|
||||
$pids = preg_split('/\s+/', `ps -o pid --no-heading --ppid $ppid`);
|
||||
@@ -471,11 +473,11 @@ class task_Scheduler
|
||||
}
|
||||
|
||||
$this->log(
|
||||
sprintf(
|
||||
"SIGKILL sent to task %s (pid=%s)"
|
||||
, $ttask[$tkey]["task"]->get_task_id()
|
||||
, $ttask[$tkey]["task"]->get_pid()
|
||||
)
|
||||
sprintf(
|
||||
"SIGKILL sent to task %s (pid=%s)"
|
||||
, $ttask[$tkey]["task"]->get_task_id()
|
||||
, $ttask[$tkey]["task"]->get_pid()
|
||||
)
|
||||
);
|
||||
|
||||
proc_terminate($ttask[$tkey]["process"], 9);
|
||||
@@ -491,11 +493,11 @@ class task_Scheduler
|
||||
else
|
||||
{
|
||||
$this->log(
|
||||
sprintf(
|
||||
"waiting task %s to quit (kill in %d seconds)"
|
||||
, $ttask[$tkey]["task"]->get_task_id()
|
||||
, $dt
|
||||
)
|
||||
sprintf(
|
||||
"waiting task %s to quit (kill in %d seconds)"
|
||||
, $ttask[$tkey]["task"]->get_task_id()
|
||||
, $dt
|
||||
)
|
||||
);
|
||||
$runningtask++;
|
||||
}
|
||||
@@ -519,7 +521,7 @@ class task_Scheduler
|
||||
sleep($sleeptime);
|
||||
}
|
||||
|
||||
$sql = "UPDATE sitepreff SET schedstatus='stopped', schedpid='0'";
|
||||
$sql = "UPDATE sitepreff SET schedstatus='stopped', schedpid='0'";
|
||||
$stmt = $conn->prepare($sql);
|
||||
$stmt->execute();
|
||||
$stmt->closeCursor();
|
||||
|
Reference in New Issue
Block a user