ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
BasicTaskManager.php
Go to the documentation of this file.
1 <?php
2 
4 
16 
33 class BasicTaskManager implements TaskManager
34 {
35 
39  protected $persistence;
40 
41 
43  {
44  $this->persistence = $persistence;
45  }
46 
47 
55  public function executeTask(Task $task, Observer $observer)
56  {
57  $observer->notifyState(State::RUNNING);
59  $values = $task->getInput();
60  $final_values = [];
61  $replace_thunk_values = false;
62  foreach ($values as $value) {
63  if (is_a($value, ThunkValue::class)) {
64  $value = $this->executeTask($value->getParentTask(), $observer);
65  $replace_thunk_values = true;
66  }
67  $final_values[] = $value;
68  }
69 
70  if ($replace_thunk_values) {
71  $task->setInput($final_values);
72  }
73 
74  if (is_a($task, Task\Job::class)) {
76  $job = $task;
77  $observer->notifyCurrentTask($job);
78  $value = $job->run($final_values, $observer);
79  if (!$value->getType()->isExtensionOf($job->getOutputType())) {
80  throw new Exception("The job " . $job->getType()
81  . " did state to output a value of type "
82  . $job->getOutputType() . " but outputted a value of type "
83  . $value->getType());
84  }
85  $observer->notifyPercentage($job, 100);
86 
87  return $value;
88  }
89 
90  if (is_a($task, Task\UserInteraction::class)) {
92  $userInteraction = $task;
93  $observer->notifyCurrentTask($userInteraction);
95  throw new UserInteractionRequiredException("User interaction required.");
96  }
97 
98  throw new Exception("You need to execute a Job or a UserInteraction.");
99  }
100 
101 
111  public function run(Bucket $bucket)
112  {
113  $task = $bucket->getTask();
114  $bucket->setCurrentTask($task);
115  $observer = new NonPersistingObserver($bucket);
116 
117  try {
118  $this->executeTask($task, $observer);
119  $bucket->setState(State::FINISHED);
120  } catch (UserInteractionRequiredException $e) {
121  // We're okay!
122  $this->persistence->saveBucketAndItsTasks($bucket);
123  }
124  }
125 
126 
135  public function continueTask(Bucket $bucket, Option $option)
136  {
137  // We do the user interaction
138  $bucket->userInteraction($option);
139  if ($bucket->getState() != State::FINISHED) { // The job is not done after the user interaction, so we continue to run it.
140  $this->run($bucket);
141  } else {
142  $this->persistence->deleteBucket($bucket);
143  }
144  }
145 
146 
150  public function quitBucket(Bucket $bucket)
151  {
152  $this->persistence->deleteBucket($bucket);
153  }
154 }
continueTask(Bucket $bucket, Option $option)
Continue a task with a given option.
notifyPercentage(Task $task, $percentage)
You can change the progress of a currently running task.
executeTask(Task $task, Observer $observer)
Actually executes a task.
notifyState($state)
If the bucket goes into another state notify the observer.
quitBucket(Bucket $bucket)
Quits and deletes a Bucket with all it&#39;s Jobs.
run(Bucket $bucket)
This will add an Observer of the Task and start running the task.
userInteraction(Option $option)
Let the user interact with the bucket task queue.
notifyCurrentTask(Task $task)
If the current task changes notify the observer.