This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
TaskService.php
237 lines (213 loc) · 6.86 KB
/
TaskService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/*
* Copyright 2007-2015 Abstrium <contact (at) pydio.com>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/
namespace Pydio\Tasks;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Pydio\Access\Core\Model\AJXP_Node;
use Pydio\Access\Core\Model\NodesDiff;
use Pydio\Access\Core\Model\Repository;
use Pydio\Conf\Core\AbstractAjxpUser;
use Pydio\Core\Controller\Controller;
use Pydio\Core\Exception\PydioException;
use Pydio\Core\PluginFramework\SqlTableProvider;
use Pydio\Core\Services\AuthService;
use Pydio\Core\Services\ConfService;
use Pydio\Core\Utils\Utils;
defined('AJXP_EXEC') or die('Access not allowed');
class TaskService implements ITasksProvider
{
/**
* @var ITasksProvider
*/
private $realProvider;
/**
* @var TaskService
*/
private static $instance;
public function setProvider(ITasksProvider $provider){
$this->realProvider = $provider;
}
/**
* @return TaskService
*/
public static function getInstance(){
if(!isSet(self::$instance)){
self::$instance = new TaskService();
}
return self::$instance;
}
public function enqueueTask(Task $task, ServerRequestInterface $request, ResponseInterface &$response){
if(ConfService::backgroundActionsSupported()){
Controller::applyTaskInBackground($task);
}else{
$params = $task->getParameters();
$action = $task->getAction();
$id = $task->getId();
$request = $request
->withAttribute("action", $action)
->withAttribute("pydio-task-id", $id)
->withParsedBody($params);
$response = Controller::run($request);
}
}
protected function publishTaskUpdate(Task $task){
$json = Utils::xmlEntities(json_encode($task));
if(count($task->nodes)){
$nodesDiff = new NodesDiff();
foreach($task->nodes as $url){
$n = new AJXP_Node($url);
$n->loadNodeInfo(true, false, "all");
Controller::applyHook("node.meta_change", array(&$n));
$nodesDiff->update($n);
}
}
$xmlString = "";
if(isSet($nodesDiff)){
$xmlString = $nodesDiff->toXML();
}
Controller::applyHook("msg.instant", array("<task id='".$task->getId()."' data='".$json."'/>".$xmlString, $task->getWsId(), $task->getUserId()));
}
public static function actionAsTask($actionName, $parameters, $repoId = "", $user = "", $nodePathes = [], $flags = 0){
if (empty($user)) {
if(AuthService::usersEnabled() && AuthService::getLoggedUser() !== null) {
$user = AuthService::getLoggedUser()->getId();
}else {
$user = "shared";
}
}
if(empty($repoId)){
$repoId = ConfService::getCurrentRepositoryId();
}
$task = new Task();
$task->setLabel("Launching action ".$actionName);
$task->setId(Utils::createGUID());
$task->setUserId($user);
$task->setWsId($repoId);
$task->setStatus(Task::STATUS_PENDING);
$task->setStatusMessage("Starting...");
$task->setAction($actionName);
$task->setParameters($parameters);
$task->setFlags($flags);
if(count($nodePathes)){
array_map(function ($node) use ($task){
$task->attachToNode($node);
}, $nodePathes);
}
TaskService::getInstance()->createTask($task, Schedule::scheduleNow());
return $task;
}
/**
* @param string $taskId
* @param integer $status
* @param string $message
* @param bool|null $stoppable
* @param integer|null $progress
* @return Task
* @throws PydioException
*/
public function updateTaskStatus($taskId, $status, $message, $stoppable = null, $progress = null){
$t = self::getTaskById($taskId);
if(empty($t)){
throw new PydioException("Cannot find task with this id");
}
$t->setStatus($status);
$t->setStatusMessage($message);
if($stoppable !== null){
$f = $t->isResumable() ? Task::FLAG_RESUMABLE : 0;
$f = $t->hasProgress() ? $f | Task::FLAG_HAS_PROGRESS : $f;
$f = $stoppable ? $f | Task::FLAG_STOPPABLE : $f;
$t->setFlags($f);
}
if($progress !== null){
$t->setProgress($progress);
}
return self::updateTask($t);
}
/**
* @param Task $task
* @param Schedule $when
* @return Task
*/
public function createTask(Task $task, Schedule $when)
{
$res = $this->realProvider->createTask($task, $when);
$this->publishTaskUpdate($task);
return $res;
}
/**
* @param string $taskId
* @return Task
*/
public function getTaskById($taskId)
{
return $this->realProvider->getTaskById($taskId);
}
/**
* @param Task $task
* @return Task
*/
public function updateTask(Task $task)
{
$res = $this->realProvider->updateTask($task);
$this->publishTaskUpdate($task);
return $res;
}
/**
* @param string $taskId
* @return bool
*/
public function deleteTask($taskId)
{
return $this->realProvider->deleteTask($taskId);
}
/**
* @return Task[]
*/
public function getPendingTasks()
{
return $this->realProvider->getPendingTasks();
}
/**
* @inheritdoc
*/
public function getCurrentRunningTasks($user, $repository)
{
return $this->realProvider->getCurrentRunningTasks($user, $repository);
}
/**
* @param AJXP_Node $node
* @return Task[]
*/
public function getActiveTasksForNode(AJXP_Node $node)
{
return $this->realProvider->getActiveTasksForNode($node);
}
/**
* @param AbstractAjxpUser $user
* @param Repository $repository
* @param int $status
* @return Task[]
*/
public function getTasks($user = null, $repository = null, $status = -1)
{
return $this->realProvider->getTasks($user, $repository, $status);
}
}