diff --git a/lib/Alchemy/Phrasea/Application/Api.php b/lib/Alchemy/Phrasea/Application/Api.php index a43bf7eb10..1921722dcd 100644 --- a/lib/Alchemy/Phrasea/Application/Api.php +++ b/lib/Alchemy/Phrasea/Application/Api.php @@ -182,10 +182,27 @@ return call_user_func(function() { } }; + /** + * Get scheduler informations + * + * Route : /monitor/scheduler/ + * + * Method : GET + * + * Parameters : + * + */ + $route = '/monitor/scheduler/'; + $app->get( + $route, function(SilexApplication $app, Request $request) { + return $app['api']->get_scheduler($app)->get_response(); + } + )->before($mustBeAdmin); + /** * Get all tasks information * - * Route : /monitor/phraseanet/ + * Route : /monitor/tasks/ * * Method : GET * @@ -200,7 +217,7 @@ return call_user_func(function() { /** * Get task informations * - * Route : /monitor/phraseanet/ + * Route : /monitor/task/{task_id}/ * * Method : GET * diff --git a/lib/classes/API/V1/adapter.class.php b/lib/classes/API/V1/adapter.class.php index c10ccf86c7..09199a8eb9 100644 --- a/lib/classes/API/V1/adapter.class.php +++ b/lib/classes/API/V1/adapter.class.php @@ -94,6 +94,33 @@ class API_V1_adapter extends API_V1_Abstract 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['phraseanet.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 * diff --git a/lib/classes/task/manager.class.php b/lib/classes/task/manager.class.php index a504498b04..6f4d7e4505 100755 --- a/lib/classes/task/manager.class.php +++ b/lib/classes/task/manager.class.php @@ -119,6 +119,7 @@ class task_manager $appbox = appbox::get_instance(\bootstrap::getCore()); $sql = "SELECT UNIX_TIMESTAMP()-UNIX_TIMESTAMP(schedqtime) AS qdelay + , schedqtime AS updated_on , schedstatus AS status FROM sitepreff"; $stmt = $this->appbox->get_connection()->prepare($sql); $stmt->execute(); @@ -139,6 +140,12 @@ class task_manager 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') { // auto fix $this->appbox->get_connection()->exec('UPDATE sitepreff SET schedstatus=\'stopped\''); diff --git a/tests/Alchemy/Phrasea/Application/ApiAbstract.inc b/tests/Alchemy/Phrasea/Application/ApiAbstract.inc index 84254b31b2..643fda9a09 100644 --- a/tests/Alchemy/Phrasea/Application/ApiAbstract.inc +++ b/tests/Alchemy/Phrasea/Application/ApiAbstract.inc @@ -197,6 +197,10 @@ abstract class ApiAbstract extends \PhraseanetWebTestCaseAbstract $content = $this->unserialize($this->client->getResponse()->getContent()); $this->assertEquals(401, $content['meta']['http_code']); + $this->client->request('GET', '/monitor/scheduler/', $this->getParameters(), 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/', $this->getParameters(), array(), array('HTTP_Accept' => $this->getAcceptMimeType())); $content = $this->unserialize($this->client->getResponse()->getContent()); $this->assertEquals(401, $content['meta']['http_code']); @@ -250,6 +254,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, $this->getParameters(), 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) { $this->assertArrayHasKey('id', $task);