Merge with master

This commit is contained in:
Romain Neutron
2012-01-31 18:28:49 +01:00
14 changed files with 270 additions and 231 deletions

View File

@@ -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);
@@ -61,14 +63,19 @@ class task_Scheduler
$password = $connexion->get('password');
$dbname = $connexion->get('dbname');
return new connection_pdo('appbox', $hostname, $port, $user, $password, $dbname);
if (!self::$connection)
{
self::$connection = new connection_pdo ('appbox', $hostname, $port, $user, $password, $dbname);
}
return self::$connection;
}
public function run(OutputInterface $output = null, $log_tasks = true)
{
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();
@@ -77,7 +84,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));
@@ -110,7 +117,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();
@@ -153,14 +160,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();
@@ -169,10 +176,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)
@@ -182,13 +189,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();
@@ -208,10 +215,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)
@@ -232,27 +239,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
@@ -260,17 +267,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;
}
@@ -291,8 +298,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)
@@ -300,21 +307,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:
@@ -333,31 +340,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"]))
@@ -370,12 +377,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++;
}
@@ -389,12 +396,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();
@@ -425,7 +432,7 @@ class task_Scheduler
if ($proc_status['running'])
$runningtask++;
else
$crashed = true;
$crashed = true;
}
else
{
@@ -447,11 +454,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()
)
);
@@ -473,7 +480,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`);
@@ -487,11 +494,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);
@@ -507,11 +514,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++;
}
@@ -537,6 +544,7 @@ class task_Scheduler
{
$conn->close();
unset($conn);
self::$connection = null;
$to_reopen = true;
}
sleep($sleeptime);
@@ -546,7 +554,7 @@ class task_Scheduler
}
}
$sql = "UPDATE sitepreff SET schedstatus='stopped', schedpid='0'";
$sql = "UPDATE sitepreff SET schedstatus='stopped', schedpid='0'";
$stmt = $conn->prepare($sql);
$stmt->execute();
$stmt->closeCursor();