Skip to content

Commit

Permalink
Merge pull request #188 from oat-sa/release-2.20.0
Browse files Browse the repository at this point in the history
Release 2.20.0
  • Loading branch information
ssipasseuth committed Jun 6, 2016
2 parents dfa3810 + f185fbc commit c62c9d4
Show file tree
Hide file tree
Showing 17 changed files with 815 additions and 35 deletions.
28 changes: 5 additions & 23 deletions common/oatbox/action/ActionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,17 @@
*/
namespace oat\oatbox\action;


use oat\oatbox\service\ConfigurableService;
use oat\oatbox\service\ServiceNotFoundException;
use Zend\ServiceManager\ServiceLocatorAwareInterface;

/**
* @deprecated
*/
class ActionResolver extends ConfigurableService
{
/**
*
* @param unknown $string
* @return Action
* @deprecated please use ActionService
*/
public function resolve($actionIdentifier)
{
$action = null;
try {
$action = $this->getServiceManager()->get($actionIdentifier);
} catch (ServiceNotFoundException $e) {
if (class_exists($actionIdentifier)) {
$action = new $actionIdentifier();
if ($action instanceof ServiceLocatorAwareInterface) {
$action->setServiceLocator($this->getServiceLocator());
}
}
}
if (!is_null($action) && $action instanceof Action) {
return $action;
} else {
throw new ResolutionException('Unknown action '.$actionIdentifier);
}
return $this->getServiceManager()->get(ActionService::SERVICE_ID)->resolve($actionIdentifier);
}
}
92 changes: 92 additions & 0 deletions common/oatbox/action/ActionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
*/
namespace oat\oatbox\action;

use oat\oatbox\service\ConfigurableService;
use oat\oatbox\service\ServiceNotFoundException;
use Zend\ServiceManager\ServiceLocatorAwareInterface;

class ActionService extends ConfigurableService
{
const SERVICE_ID = 'generis/actionService';

static $blackList = array('\\oatbox\\composer\\ExtensionInstaller','\\oatbox\\composer\\ExtensionInstallerPlugin');

/**
*
* @param string $actionIdentifier
* @return Action
*/
public function resolve($actionIdentifier)
{
$action = null;
if ($this->getServiceLocator()->has($actionIdentifier)) {
$action = $this->getServiceManager()->get($actionIdentifier);
} elseif (class_exists($actionIdentifier) && is_subclass_of($actionIdentifier, Action::class)) {
$action = new $actionIdentifier();
if ($action instanceof ServiceLocatorAwareInterface) {
$action->setServiceLocator($this->getServiceLocator());
}
} else {
throw new ResolutionException('Unknown action '.$actionIdentifier);
}
return $action;
}

public function getAvailableActions()
{
if ($this->getCache()->has(__FUNCTION__)) {
$actions = $this->getCache()->get(__FUNCTION__);
} else {
$actions = array();
foreach (\common_ext_ExtensionsManager::singleton()->getInstalledExtensions() as $ext) {
$actions = array_merge($actions, $this->getActionsInDirectory($ext->getDir()));
}
$actions = array_merge($actions, $this->getActionsInDirectory(VENDOR_PATH.'oat-sa'));
$this->getCache()->put($actions, __FUNCTION__);
}
return $actions;
}

/**
* @return \common_cache_Cache
*/
protected function getCache()
{
return $this->getServiceManager()->get('generis/cache');
}

protected function getActionsInDirectory($dir)
{
$classNames = array();
$recIt = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir));
$regexIt = new \RegexIterator($recIt, '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH);
foreach ($regexIt as $entry) {
$info = \helpers_PhpTools::getClassInfo($entry[0]);
$fullname = empty($info['ns'])
? $info['class']
: $info['ns'].'\\'.$info['class'];
if (!in_array($fullname, self::$blackList) && is_subclass_of($fullname, Action::class)) {
$classNames[] = $fullname;
}
}
return $classNames;
}
}
39 changes: 39 additions & 0 deletions common/oatbox/action/Help.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
*/
namespace oat\oatbox\action;


use oat\oatbox\service\ServiceNotFoundException;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Zend\ServiceManager\ServiceLocatorAwareInterface;

class Help implements Action, ServiceLocatorAwareInterface
{
use ServiceLocatorAwareTrait;

public function __invoke($params) {
$actionResolver = $this->getServiceLocator()->get(ActionService::SERVICE_ID);
$report = new \common_report_Report(\common_report_Report::TYPE_INFO, __('Available Actions:'));
foreach ($actionResolver->getAvailableActions() as $actionClass) {
$report->add(new \common_report_Report(\common_report_Report::TYPE_INFO, ' '.$actionClass));
}
return $report;
}
}
14 changes: 10 additions & 4 deletions common/oatbox/task/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@
*/
namespace oat\oatbox\task;

use oat\oatbox\service\ConfigurableService;

interface Queue extends \IteratorAggregate
{
const CONFIG_ID = 'generis/taskqueue';

public function createTask($actionId, $parameters);

/**
* @param $actionId
* @param $parameters
* @param boolean $repeatedly Whether task created repeatedly (for example when execution of task was failed and task puts to the queue again).
* @return mixed
*/
public function createTask($actionId, $parameters, $repeatedly = false);

public function getIterator();

public function updateTaskStatus($taskId, $status);
}
40 changes: 40 additions & 0 deletions common/oatbox/task/RunTasks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2014 (original work) Open Assessment Technologies SA;
*
*
*/
namespace oat\oatbox\task;

use oat\oatbox\service\ConfigurableService;
use oat\Taskqueue\Persistence\RdsQueue;
use Doctrine\DBAL\Schema\SchemaException;
use oat\oatbox\service\ServiceManager;
use oat\oatbox\task\Queue;
use oat\oatbox\action\Action;
use oat\oatbox\task\TaskRunner;
use common_report_Report as Report;

class RunTasks extends ConfigurableService implements Action
{
public function __invoke($params) {
$taskService = new TaskService();
$taskService->setServiceLocator($this->getServiceLocator());
$report = $taskService->runQueue();
return $report;
}
}
2 changes: 1 addition & 1 deletion common/oatbox/task/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public function getInvocable();

public function getParameters();

public function setParameters();
public function setParameters(array $params);

}
20 changes: 14 additions & 6 deletions common/oatbox/task/TaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,34 @@
*/
namespace oat\oatbox\task;

use oat\oatbox\service\ConfigurableService;
use oat\oatbox\service\ServiceManager;
use oat\oatbox\action\ActionService;
use Zend\ServiceManager\ServiceLocatorAwareInterface;

class TaskRunner
{
public function run(Task $task) {


\common_Logger::d('Running task '.$task->getId());
$report = new \common_report_Report(\common_report_Report::TYPE_INFO, __('Running task %s', $task->getId()));
$queue = $this->getServiceLocator()->get(Queue::CONFIG_ID);
$queue->updateTaskStatus($task->getId(), Task::STATUS_RUNNING);
try {
$invocableName = $task->getInvocable();
$invocable = new $invocableName();
if ($invocable instanceof ServiceLocatorAwareInterface) {
$actionService = $this->getServiceLocator()->get(ActionService::SERVICE_ID);
$invocable = $task->getInvocable();
if (is_string($invocable)) {
$invocable = $actionService->resolve($task->getInvocable());
} else if ($invocable instanceof ServiceLocatorAwareInterface) {
$invocable->setServiceLocator($this->getServiceLocator());
}
$subReport = call_user_func($invocable, $task->getParameters());
$report->add($subReport);
} catch (\Exception $e) {
$report = new \common_report_Report(\common_report_Report::TYPE_ERROR, __('Unable to run task %s', $task->getId()));
$message = 'Task ' . $task->getId() . ' failed. Error message: ' . $e->getMessage();
\common_Logger::e($message);
$report = new \common_report_Report(\common_report_Report::TYPE_ERROR, $message);
}
$queue->updateTaskStatus($task->getId(), Task::STATUS_FINISHED, $report);
return $report;
}

Expand Down
81 changes: 81 additions & 0 deletions common/oatbox/task/TaskService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2014 (original work) Open Assessment Technologies SA;
*
*
*/
namespace oat\oatbox\task;

use oat\oatbox\service\ConfigurableService;
use common_report_Report as Report;

class TaskService extends ConfigurableService
{
/**
* @var TaskRunner
*/
private $taskRunner;

/**
* @param Task $task
* @return Report
*/
public function runTask(Task $task)
{
$taskRunner = $this->getTaskRunner();
return $taskRunner->run($task);
}

/**
* @return Report
* @throws \common_exception_Error
*/
public function runQueue()
{
$statistics = array();
$queue = $this->getServiceManager()->get(Queue::CONFIG_ID);
$report = new Report(Report::TYPE_SUCCESS);
foreach ($queue as $task) {
$subReport = $this->runTask($task);
$statistics[$subReport->getType()] = isset($statistics[$subReport->getType()])
? $statistics[$subReport->getType()] + 1
: 1;
$report->add($subReport);
}

if (empty($statistics)) {
$report = new Report(Report::TYPE_INFO, __('No tasks to run'));
} else {
if (isset($statistics[Report::TYPE_ERROR]) || isset($statistics[Report::TYPE_WARNING])) {
$report->setType(Report::TYPE_WARNING);
}
$report->setMessage(__('Ran %s task(s):', array_sum($statistics)));
}
return $report;
}

/**
* @return TaskRunner
*/
private function getTaskRunner()
{
if ($this->taskRunner === null) {
$this->taskRunner = new TaskRunner();
}
return $this->taskRunner;
}
}
Loading

0 comments on commit c62c9d4

Please sign in to comment.