ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilBackgroundTaskHub.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once "Services/BackgroundTask/classes/class.ilBackgroundTask.php";
5
14{
15 protected $task; // [ilBackgroundTask]
16 protected $handler; // [ilBackgroundTaskHandler]
17
23 public function __construct()
24 {
25 global $DIC;
26 $lng = $DIC['lng'];
27
28 $lng->loadLanguageModule("bgtask");
29
30 if ((int) $_REQUEST["tid"]) {
31 $this->task = new ilBackgroundTask((int) $_REQUEST["tid"]);
32 $this->handler = $this->task->getHandlerInstance();
33 }
34 }
35
36
37 //
38 // ajax
39 //
40
44 public function executeCommand()
45 {
46 global $DIC;
47 $ilCtrl = $DIC['ilCtrl'];
48
49 $next_class = $ilCtrl->getNextClass($this);
50 $cmd = $ilCtrl->getCmd("validate");
51
52 switch ($next_class) {
53 default:
54 if ($cmd == "deliver" ||
55 $ilCtrl->isAsynch()) {
56 $this->$cmd();
57 break;
58 }
59 }
60
61 // deliver file and ajax require exit
62 exit();
63 }
64
70 protected function sendJson(stdClass $a_json)
71 {
72 echo json_encode($a_json);
73 }
74
78 protected function validate()
79 {
80 $class = trim($_GET["hid"]);
81 $file = "Services/BackgroundTask/classes/class." . $class . ".php";
82 if (file_exists($file)) {
83 include_once $file;
84 $handler = new $class();
85 $json = $handler->init($_GET["par"]);
86
87 $this->sendJson($json);
88 }
89 }
90
95 protected function unblock()
96 {
97 global $DIC;
98 $ilUser = $DIC['ilUser'];
99
100 foreach (ilBackgroundTask::getActiveByUserId($ilUser->getId()) as $task_id) {
101 // leave current task alone
102 if ($task_id != $this->task->getId()) {
103 // emit cancelling status, running processes will cancel
104 $task = new ilBackgroundTask($task_id);
106 $task->save();
107 }
108 }
109
110 // init/start current task
111 $json = $this->handler->init();
112 $this->sendJson($json);
113 }
114
118 protected function process()
119 {
120 $this->task->setStatus(ilBackgroundTask::STATUS_PROCESSING);
121 $this->task->save();
122
123 if (!$this->isSOAPEnabled()) {
124 $this->handler->process();
125 } else {
126 require_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
127 $soap_client = new ilSoapClient();
128 $soap_client->setResponseTimeout(1);
129 $soap_client->enableWSDL(true);
130 $soap_client->init();
131 $soap_client->call('processBackgroundTask', array(
132 session_id() . '::' . $_COOKIE['ilClientId'],
133 $this->task->getId()
134 ));
135 }
136 }
137
143 public function isSOAPEnabled()
144 {
145 global $DIC;
146 $ilSetting = $DIC['ilSetting'];
147
148 // see ilMail
149 return (extension_loaded('curl') &&
150 $ilSetting->get('soap_user_administration') &&
152 }
153
157 protected function progress()
158 {
159 include_once "Services/BackgroundTask/classes/class.ilBackgroundTaskJson.php";
160
161 // if task has been finished, get result action
162 if ($this->task->getStatus() == ilBackgroundTask::STATUS_FINISHED) {
163 $result = $this->handler->finish();
164 $json = ilBackgroundTaskJson::getProgressJson($this->task, $result[0], $result[1]);
165 } else {
166 $json = ilBackgroundTaskJson::getProgressJson($this->task);
167 }
168
169 $this->sendJson($json);
170 }
171
175 protected function cancel()
176 {
177 // just emit cancelling status, (background) process will stop ASAP
178 $this->task->setStatus(ilBackgroundTask::STATUS_CANCELLING);
179 $this->task->save();
180 }
181
185 protected function deliver()
186 {
187 // :TODO: delete task?
188
189 $this->handler->deliver();
190 }
191}
$result
$_COOKIE['client_id']
Definition: server.php:9
$_GET["client_id"]
An exception for terminatinating execution or to throw for unit testing.
background task hub (aka ajax handler, GUI)
sendJson(stdClass $a_json)
Send Json to client.
executeCommand()
Execute current command.
cancel()
Cancel current task.
progress()
Check progress of current task.
process()
Process current task.
validate()
Validate given task.
unblock()
Cancel all other tasks, start current one.
static getProgressJson(ilBackgroundTask $a_task, $a_finished_cmd=null, $a_finished_result=null)
Get json for task progress.
static getActiveByUserId($a_user_id)
const CONTEXT_CRON
static getType()
Get context type.
global $ilCtrl
Definition: ilias.php:18
global $lng
Definition: privfeed.php:17
global $ilSetting
Definition: privfeed.php:17
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file
global $DIC
Definition: saml.php:7
$ilUser
Definition: imgupload.php:18