Fix #865 : add route in API for scheduler infos

This commit is contained in:
Romain Neutron
2012-08-29 16:15:53 +02:00
parent d44c6fe6b3
commit def3a2b293
4 changed files with 97 additions and 2 deletions

View File

@@ -187,10 +187,27 @@ return call_user_func(function() {
} }
}; };
/**
* Get scheduler informations
*
* Route : /monitor/scheduler/
*
* Method : GET
*
* Parameters :
*
*/
$route = '/monitor/scheduler/';
$app->get(
$route, function(Application $app, Request $request) {
return $app['api']->get_scheduler($app)->get_response();
}
)->before($mustBeAdmin);
/** /**
* Get all tasks information * Get all tasks information
* *
* Route : /monitor/phraseanet/ * Route : /monitor/tasks/
* *
* Method : GET * Method : GET
* *
@@ -208,7 +225,7 @@ return call_user_func(function() {
* ******************************************************************* * *******************************************************************
* Get task informations * Get task informations
* *
* Route : /monitor/phraseanet/ * Route : /monitor/task/{task_id}/
* *
* Method : GET * Method : GET
* *

View File

@@ -94,6 +94,33 @@ class API_V1_adapter extends API_V1_Abstract
return $this->version; return $this->version;
} }
/**
* Return an array of key-values informations about scheduler
*
* @param Application $app The silex application
* @return \API_V1_result
*/
public function get_scheduler(Application $app)
{
$result = new \API_V1_result($app['request'], $this);
$appbox = \appbox::get_instance($app['Core']);
$taskManager = new \task_manager($appbox);
$ret = $taskManager->getSchedulerState();
$ret['state'] = $ret['status'];
unset($ret['qdelay'], $ret['status']);
if (null !== $ret['updated_on']) {
$ret['updated_on'] = $ret['updated_on']->format(DATE_ATOM);
}
$result->set_datas(array('scheduler' => $ret));
return $result;
}
/** /**
* Get a list of phraseanet tasks * Get a list of phraseanet tasks
* *

View File

@@ -119,6 +119,7 @@ class task_manager
$appbox = appbox::get_instance(\bootstrap::getCore()); $appbox = appbox::get_instance(\bootstrap::getCore());
$sql = "SELECT UNIX_TIMESTAMP()-UNIX_TIMESTAMP(schedqtime) AS qdelay $sql = "SELECT UNIX_TIMESTAMP()-UNIX_TIMESTAMP(schedqtime) AS qdelay
, schedqtime AS updated_on
, schedstatus AS status FROM sitepreff"; , schedstatus AS status FROM sitepreff";
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->appbox->get_connection()->prepare($sql);
$stmt->execute(); $stmt->execute();
@@ -139,6 +140,12 @@ class task_manager
fclose($schedlock); fclose($schedlock);
} }
if ($ret['updated_on'] == '0000-00-00 00:00:00') {
$ret['updated_on'] = null;
} else {
$ret['updated_on'] = new \DateTime($ret['updated_on']);
}
if ($pid === NULL && $ret['status'] !== 'stopped') { if ($pid === NULL && $ret['status'] !== 'stopped') {
// auto fix // auto fix
$this->appbox->get_connection()->exec('UPDATE sitepreff SET schedstatus=\'stopped\''); $this->appbox->get_connection()->exec('UPDATE sitepreff SET schedstatus=\'stopped\'');

View File

@@ -194,6 +194,10 @@ abstract class ApiAbstract extends \PhraseanetWebTestCaseAbstract
$content = $this->unserialize($this->client->getResponse()->getContent()); $content = $this->unserialize($this->client->getResponse()->getContent());
$this->assertEquals(401, $content['meta']['http_code']); $this->assertEquals(401, $content['meta']['http_code']);
$this->client->request('GET', '/monitor/scheduler/', array(), array(), array('HTTP_Accept' => $this->getAcceptMimeType()));
$content = $this->unserialize($this->client->getResponse()->getContent());
$this->assertEquals(401, $content['meta']['http_code']);
$this->client->request('GET', '/monitor/task/1/', array(), array(), array('HTTP_Accept' => $this->getAcceptMimeType())); $this->client->request('GET', '/monitor/task/1/', array(), array(), array('HTTP_Accept' => $this->getAcceptMimeType()));
$content = $this->unserialize($this->client->getResponse()->getContent()); $content = $this->unserialize($this->client->getResponse()->getContent());
$this->assertEquals(401, $content['meta']['http_code']); $this->assertEquals(401, $content['meta']['http_code']);
@@ -247,6 +251,46 @@ abstract class ApiAbstract extends \PhraseanetWebTestCaseAbstract
} }
} }
/**
* Route GET /API/V1/monitor/scheduler
* @covers API_V1_adapter::get_scheduler
*/
public function testGetScheduler()
{
$appbox = \appbox::get_instance(\bootstrap::getCore());
if (null === self::$adminToken) {
$this->markTestSkipped('there is no user with admin rights');
}
$this->setToken(self::$adminToken);
$route = '/monitor/scheduler/';
$this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE'));
$this->client->request('GET', $route, array(), array(), array('HTTP_Accept' => $this->getAcceptMimeType()));
$content = $this->unserialize($this->client->getResponse()->getContent());
$this->evaluateResponse200($this->client->getResponse());
$this->evaluateMeta200($content);
$response = $content['response'];
$this->assertInternalType('array', $response['scheduler']);
$this->assertArrayHasKey('state', $response['scheduler']);
$this->assertArrayHasKey('pid', $response['scheduler']);
$this->assertArrayHasKey('updated_on', $response['scheduler']);
$this->assertEquals(3, count($response['scheduler']));
if (null !== $response['scheduler']['updated_on']) {
$this->assertDateAtom($response['scheduler']['updated_on']);
}
if (null !== $response['scheduler']['pid']) {
$this->assertTrue(is_int($response['scheduler']['pid']));
}
$this->assertTrue('' !== $response['scheduler']['state']);
}
protected function evaluateGoodTask($task) protected function evaluateGoodTask($task)
{ {
$this->assertArrayHasKey('id', $task); $this->assertArrayHasKey('id', $task);