Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Add a specific status for task STATUS_TEMPLATE : do not publish event…
Browse files Browse the repository at this point in the history
…s for this type of tasks.
  • Loading branch information
cdujeu committed Jul 20, 2016
1 parent b63e19a commit 454dc0b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 84 deletions.
111 changes: 27 additions & 84 deletions core/src/plugins/action.scheduler/src/Scheduler.php
Expand Up @@ -104,9 +104,6 @@ public function performChecks()
if (!ConfService::backgroundActionsSupported()) {
throw new Exception("The command line must be supported. See 'Pydio Core Options'.");
}
if (!is_dir(dirname($this->getDbFile()))) {
throw new Exception("Could not create the db folder!");
}
}

/**
Expand Down Expand Up @@ -136,71 +133,6 @@ public function parseSpecificContributions(ContextInterface $ctx, \DOMNode &$con
$paramNode->attributes->getNamedItem("default")->nodeValue = $ctx->getUser()->getId();
}

/**
* @param $tId
* @return mixed
* @throws Exception
*/
public function getTaskById($tId)
{
$tasks = FileHelper::loadSerialFile($this->getDbFile(), false, "json");
foreach ($tasks as $task) {
if (!empty($task["task_id"]) && $task["task_id"] == $tId) {
return $task;
}
}
throw new Exception("Cannot find task");
}

/**
* @param $taskId
* @param $status
* @param string $statusMessage
*/
public function setTaskStatus($taskId, $status, $statusMessage)
{
$tData = $this->getTaskById($taskId);
if(isSet($tData["job_id"])){
$runningTask = TaskService::getInstance()->getTaskById($tData["job_id"]);
$runningTask->setStatus($status);
$runningTask->setStatusMessage($statusMessage);
TaskService::getInstance()->updateTask($runningTask);
}
}

/**
* @param $taskId
* @return array|bool
*/
public function getTaskStatus($taskId)
{
$tData = $this->getTaskById($taskId);
if(isSet($tData["job_id"])) {
$runningTask = TaskService::getInstance()->getTaskById($tData["job_id"]);
if($runningTask === null){
return [Task::STATUS_PENDING, "Pending"];
}
return [$runningTask->getStatus(), $runningTask->getStatusMessage()];
}
return [Task::STATUS_PENDING, "Pending"];
}

/**
* @return int
*/
public function countCurrentlyRunning()
{
$tasks = FileHelper::loadSerialFile($this->getDbFile(), false, "json");
$count = 0;
foreach ($tasks as $task) {
$s = $this->getTaskStatus($task["task_id"]);
if ($s !== false && $s[0] == "RUNNING") {
$count++;
}
}
return $count;
}

/**
* @param ContextInterface $ctx
* @param $taskId
Expand Down Expand Up @@ -274,6 +206,7 @@ public function switchAction(ServerRequestInterface $requestInterface, ResponseI
case "scheduler_runAll":

$message = "";
$this->migrateLegacyTasks();
$tasks = TaskService::getInstance()->getScheduledTasks();
foreach($tasks as $task){
$res = $this->runTask($ctx, $task->getId());
Expand Down Expand Up @@ -365,7 +298,7 @@ public function handleTasks(ServerRequestInterface $requestInterface, ResponseIn
$task->setId(StringHelper::createGUID());
$task->setType(Task::TYPE_ADMIN);
}
$task->setStatus(Task::STATUS_PENDING);
$task->setStatus(Task::STATUS_TEMPLATE);
$task->setStatusMessage("Scheduled");
$task->setAction($actionName);
$task->setParameters($parameters);
Expand Down Expand Up @@ -516,6 +449,7 @@ public function listTasks($httpVars, $rootPath, $relativePath, $paginationHash =
->appendColumn("action.scheduler.13", "STATUS");

$basePath = "/$rootPath/$relativePath";
$this->migrateLegacyTasks();
$tasks = TaskService::getInstance()->getScheduledTasks();
foreach($tasks as $task){

Expand Down Expand Up @@ -581,6 +515,30 @@ protected function taskToNode(Task $task, $basePath, $dateFormat, $isChild = fal
return new AJXP_Node($key, $meta);
}

/**
* Migrate old JSON file format to TaskService
*/
protected function migrateLegacyTasks(){
$dbFile = $this->getDbFile();
if(!file_exists($dbFile)) return;
$tasks = FileHelper::loadSerialFile($dbFile, false, "json");
foreach ($tasks as $tData){
$t = new Task();
$t->setId(StringHelper::createGUID());
$t->setLabel($tData["label"]);
$t->setAction($tData["action_name"]);
$t->setParameters($tData["PARAMS"]);
$t->setSchedule(new Schedule(Schedule::TYPE_RECURRENT, $tData["schedule"]));
$t->setUserId($tData["user_id"]);
$t->setWsId($tData["repository_id"]);
$t->setType(Task::TYPE_ADMIN);
$t->setStatus(Task::STATUS_TEMPLATE);
$t->setStatusMessage("Scheduled");
TaskService::getInstance()->createTask($t, $t->getSchedule());
}
@unlink($dbFile);
}

/**
* @param $taskId
* @param $label
Expand Down Expand Up @@ -618,20 +576,5 @@ public function addOrUpdateTask($taskId, $label, $schedule, $actionName, $reposi

}

/**
* @param $taskId
* @throws Exception
*/
public function removeTask($taskId)
{
$tasks = FileHelper::loadSerialFile($this->getDbFile(), false, "json");
foreach ($tasks as $index => $task) {
if ($task["task_id"] == $taskId) {
unset($tasks[$index]);
break;
}
}
FileHelper::saveSerialFile($this->getDbFile(), $tasks, true, false, "json");
}

}
5 changes: 5 additions & 0 deletions core/src/plugins/core.tasks/src/Task.php
Expand Up @@ -26,6 +26,10 @@

defined('AJXP_EXEC') or die('Access not allowed');

/**
* Class Task
* @package Pydio\Tasks
*/
class Task
{
const TYPE_USER = 1;
Expand All @@ -36,6 +40,7 @@ class Task
const STATUS_COMPLETE = 4;
const STATUS_FAILED = 8;
const STATUS_PAUSED = 16;
const STATUS_TEMPLATE = 32;

const FLAG_STOPPABLE = 1;
const FLAG_RESUMABLE = 2;
Expand Down
3 changes: 3 additions & 0 deletions core/src/plugins/core.tasks/src/TaskService.php
Expand Up @@ -117,6 +117,9 @@ public function enqueueTask(Task $task, ServerRequestInterface $request = null,
*/
protected function publishTaskUpdate(Task $task){

if($task->getStatus() === Task::STATUS_TEMPLATE){
return;
}
$json = StringHelper::xmlEntities(json_encode($task));
if(count($task->nodes)){
$nodesDiff = new NodesDiff();
Expand Down

0 comments on commit 454dc0b

Please sign in to comment.