ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 $lng;
26
27 $lng->loadLanguageModule("bgtask");
28
29 if((int)$_REQUEST["tid"])
30 {
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 $ilCtrl;
47
48 $next_class = $ilCtrl->getNextClass($this);
49 $cmd = $ilCtrl->getCmd("validate");
50
51 switch ($next_class)
52 {
53 default:
54 if($cmd == "deliver" ||
55 $ilCtrl->isAsynch())
56 {
57 $this->$cmd();
58 break;
59 }
60 }
61
62 // deliver file and ajax require exit
63 exit();
64 }
65
71 protected function sendJson(stdClass $a_json)
72 {
73 echo json_encode($a_json);
74 }
75
79 protected function validate()
80 {
81 $class = trim($_GET["hid"]);
82 $file = "Services/BackgroundTask/classes/class.".$class.".php";
83 if(file_exists($file))
84 {
85 include_once $file;
86 $handler = new $class();
87 $json = $handler->init($_GET["par"]);
88
89 $this->sendJson($json);
90 }
91 }
92
97 protected function unblock()
98 {
99 global $ilUser;
100
101 foreach(ilBackgroundTask::getActiveByUserId($ilUser->getId()) as $task_id)
102 {
103 // leave current task alone
104 if($task_id != $this->task->getId())
105 {
106 // emit cancelling status, running processes will cancel
107 $task = new ilBackgroundTask($task_id);
109 $task->save();
110 }
111 }
112
113 // init/start current task
114 $json = $this->handler->init();
115 $this->sendJson($json);
116 }
117
121 protected function process()
122 {
123 $this->task->setStatus(ilBackgroundTask::STATUS_PROCESSING);
124 $this->task->save();
125
126 if(!$this->isSOAPEnabled())
127 {
128 $this->handler->process();
129 }
130 else
131 {
132 require_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
133 $soap_client = new ilSoapClient();
134 $soap_client->setResponseTimeout(1);
135 $soap_client->enableWSDL(true);
136 $soap_client->init();
137 $soap_client->call('processBackgroundTask', array(
138 session_id() . '::' . $_COOKIE['ilClientId'],
139 $this->task->getId()
140 ));
141 }
142 }
143
149 public function isSOAPEnabled()
150 {
151 global $ilSetting;
152
153 // see ilMail
154 return (extension_loaded('curl') &&
155 $ilSetting->get('soap_user_administration') &&
157 }
158
162 protected function progress()
163 {
164 include_once "Services/BackgroundTask/classes/class.ilBackgroundTaskJson.php";
165
166 // if task has been finished, get result action
167 if($this->task->getStatus() == ilBackgroundTask::STATUS_FINISHED)
168 {
169 $result = $this->handler->finish();
170 $json = ilBackgroundTaskJson::getProgressJson($this->task, $result[0], $result[1]);
171 }
172 else
173 {
174 $json = ilBackgroundTaskJson::getProgressJson($this->task);
175 }
176
177 $this->sendJson($json);
178 }
179
183 protected function cancel()
184 {
185 // just emit cancelling status, (background) process will stop ASAP
186 $this->task->setStatus(ilBackgroundTask::STATUS_CANCELLING);
187 $this->task->save();
188 }
189
193 protected function deliver()
194 {
195 // :TODO: delete task?
196
197 $this->handler->deliver();
198 }
199}
$_COOKIE['ilClientId']
Definition: BPMN2Parser.php:15
$result
$_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
$cmd
Definition: sahs_server.php:35
$ilUser
Definition: imgupload.php:18