mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-17 15:03:25 +00:00
add some tests
This commit is contained in:
@@ -128,8 +128,6 @@ class task_Scheduler
|
||||
// set every 'auto-start' task to start
|
||||
foreach ($task_manager->getTasks() as $task) {
|
||||
if ($task->isActive()) {
|
||||
$tid = $task->getID();
|
||||
|
||||
if ( ! $task->getPID()) {
|
||||
/* @var $task task_abstract */
|
||||
$task->resetCrashCounter();
|
||||
|
@@ -93,6 +93,11 @@ abstract class task_abstract
|
||||
"--help" => array("set" => false, "values" => array(), "usage" => " (no help available)")
|
||||
);
|
||||
|
||||
/**
|
||||
* get the state of the task (task_abstract::STATE_*)
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public function getState()
|
||||
{
|
||||
$conn = connection::getPDOConnection();
|
||||
@@ -118,11 +123,22 @@ abstract class task_abstract
|
||||
return false;
|
||||
}
|
||||
|
||||
public function printInterfaceHTML()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getGraphicForm()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the state of the task (task_abstract::STATE_*)
|
||||
*
|
||||
* @param String $status
|
||||
* @throws Exception_InvalidArgument
|
||||
*/
|
||||
public function setState($status)
|
||||
{
|
||||
$av_status = array(
|
||||
@@ -179,6 +195,10 @@ abstract class task_abstract
|
||||
|
||||
public function setSettings($settings)
|
||||
{
|
||||
if (@simplexml_load_string($settings) === FALSE) {
|
||||
throw new Exception_InvalidArgument('Bad XML');
|
||||
}
|
||||
|
||||
$conn = connection::getPDOConnection();
|
||||
|
||||
$sql = 'UPDATE task2 SET settings = :settings WHERE task_id = :taskid';
|
||||
@@ -301,6 +321,10 @@ abstract class task_abstract
|
||||
|
||||
public function setRunner($runner)
|
||||
{
|
||||
if ($runner != self::RUNNER_MANUAL && $runner != self::RUNNER_SCHEDULER) {
|
||||
throw new Exception_InvalidArgument(sprintf('unknown runner `%s`', $runner));
|
||||
}
|
||||
|
||||
$this->runner = $runner;
|
||||
|
||||
$conn = connection::getPDOConnection();
|
||||
@@ -364,12 +388,10 @@ abstract class task_abstract
|
||||
public function getPID()
|
||||
{
|
||||
$pid = NULL;
|
||||
$taskid = $this->getID();
|
||||
|
||||
$registry = registry::get_instance();
|
||||
system_file::mkdir($lockdir = $registry->get('GV_RootPath') . 'tmp/locks/');
|
||||
$lockfile = $this->getLockfilePath();
|
||||
|
||||
if (($fd = fopen(($lockfile = ($lockdir . 'task_' . $taskid . '.lock')), 'a+')) != FALSE) {
|
||||
if (($fd = fopen($lockfile, 'a+')) != FALSE) {
|
||||
if (flock($fd, LOCK_EX | LOCK_NB) === FALSE) {
|
||||
// already locked ? : task running
|
||||
$pid = fgets($fd);
|
||||
@@ -407,36 +429,50 @@ abstract class task_abstract
|
||||
}
|
||||
}
|
||||
|
||||
private function getLockfilePath()
|
||||
{
|
||||
$registry = registry::get_instance();
|
||||
$lockdir = $registry->get('GV_RootPath') . 'tmp/locks/';
|
||||
|
||||
system_file::mkdir($lockdir);
|
||||
$lockfile = ($lockdir . 'task_' . $this->getID() . '.lock');
|
||||
|
||||
return($lockfile);
|
||||
}
|
||||
|
||||
private function lockTask()
|
||||
{
|
||||
$lockfile = $this->getLockfilePath();
|
||||
|
||||
$lockFD = fopen($lockfile, 'a+');
|
||||
|
||||
$locker = true;
|
||||
if (flock($lockFD, LOCK_EX | LOCK_NB, $locker) === FALSE) {
|
||||
$this->log("runtask::ERROR : task already running.");
|
||||
fclose($lockFD);
|
||||
|
||||
throw new Exception('task already running.', self::ERR_ALREADY_RUNNING);
|
||||
}
|
||||
|
||||
// here we run the task
|
||||
ftruncate($lockFD, 0);
|
||||
fwrite($lockFD, '' . getmypid());
|
||||
fflush($lockFD);
|
||||
|
||||
// for windows : unlock then lock shared to allow OTHER processes to read the file
|
||||
// too bad : no critical section nor atomicity
|
||||
flock($lockFD, LOCK_UN);
|
||||
flock($lockFD, LOCK_SH);
|
||||
|
||||
return $lockFD;
|
||||
}
|
||||
|
||||
final public function run($runner, $input = null, $output = null)
|
||||
{
|
||||
$this->input = $input;
|
||||
$this->output = $output;
|
||||
|
||||
$taskid = $this->getID();
|
||||
|
||||
$registry = registry::get_instance();
|
||||
system_file::mkdir($lockdir = $registry->get('GV_RootPath') . 'tmp/locks/');
|
||||
$locker = true;
|
||||
$lockfile = ($lockdir . 'task_' . $taskid . '.lock');
|
||||
$tasklock = fopen($lockfile, 'a+');
|
||||
|
||||
if (flock($tasklock, LOCK_EX | LOCK_NB, $locker) === FALSE) {
|
||||
$this->log("runtask::ERROR : task already running.");
|
||||
fclose($tasklock);
|
||||
|
||||
throw new Exception('task already running.', self::ERR_ALREADY_RUNNING);
|
||||
return;
|
||||
}
|
||||
|
||||
// here we run the task
|
||||
ftruncate($tasklock, 0);
|
||||
fwrite($tasklock, '' . getmypid());
|
||||
fflush($tasklock);
|
||||
|
||||
// for windows : unlock then lock shared to allow OTHER processes to read the file
|
||||
// too bad : no critical section nor atomicity
|
||||
flock($tasklock, LOCK_UN);
|
||||
flock($tasklock, LOCK_SH);
|
||||
$lockFD = $this->lockTask();
|
||||
|
||||
$this->setRunner($runner);
|
||||
$this->setState(self::STATE_STARTED);
|
||||
@@ -450,9 +486,21 @@ abstract class task_abstract
|
||||
}
|
||||
|
||||
// in any case, exception or not, the task is ending so unlock the pid file
|
||||
flock($tasklock, LOCK_UN | LOCK_NB);
|
||||
ftruncate($tasklock, 0);
|
||||
fclose($tasklock);
|
||||
$this->unlockTask($lockFD);
|
||||
|
||||
// if something went wrong, report
|
||||
if ($exception) {
|
||||
throw($exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function unlockTask($lockFD)
|
||||
{
|
||||
flock($lockFD, LOCK_UN | LOCK_NB);
|
||||
ftruncate($lockFD, 0);
|
||||
fclose($lockFD);
|
||||
|
||||
$lockfile = $this->getLockfilePath();
|
||||
@unlink($lockfile);
|
||||
|
||||
switch ($this->getState()) {
|
||||
@@ -463,11 +511,6 @@ abstract class task_abstract
|
||||
$this->setState(self::STATE_STOPPED);
|
||||
break;
|
||||
}
|
||||
|
||||
// if something went wrong, report
|
||||
if ($exception) {
|
||||
throw($exception);
|
||||
}
|
||||
}
|
||||
|
||||
abstract protected function run2();
|
||||
|
@@ -16,10 +16,10 @@
|
||||
*/
|
||||
class task_manager
|
||||
{
|
||||
const STATUS_SCHED_STOPPED = 'stopped';
|
||||
const STATUS_SCHED_STOPPING = 'stopping';
|
||||
const STATUS_SCHED_STARTED = 'started';
|
||||
const STATUS_SCHED_TOSTOP = 'tostop';
|
||||
const STATE_STOPPED = 'stopped';
|
||||
const STATE_STOPPING = 'stopping';
|
||||
const STATE_STARTED = 'started';
|
||||
const STATE_TOSTOP = 'tostop';
|
||||
|
||||
protected $appbox;
|
||||
protected $tasks;
|
||||
@@ -91,7 +91,7 @@ class task_manager
|
||||
$tasks = $this->getTasks();
|
||||
|
||||
if ( ! isset($tasks[$task_id])) {
|
||||
throw new Exception_NotFound('Unknown task_id');
|
||||
throw new Exception_NotFound('Unknown task_id ' . $task_id);
|
||||
}
|
||||
|
||||
return $tasks[$task_id];
|
||||
@@ -100,10 +100,10 @@ class task_manager
|
||||
public function setSchedulerState($status)
|
||||
{
|
||||
$av_status = array(
|
||||
self::STATUS_SCHED_STARTED
|
||||
, self::STATUS_SCHED_STOPPED
|
||||
, self::STATUS_SCHED_STOPPING
|
||||
, self::STATUS_SCHED_TOSTOP
|
||||
self::STATE_STARTED,
|
||||
self::STATE_STOPPED,
|
||||
self::STATE_STOPPING,
|
||||
self::STATE_TOSTOP
|
||||
);
|
||||
|
||||
if ( ! in_array($status, $av_status))
|
||||
|
Reference in New Issue
Block a user