1<?php 2 3namespace ILIAS\BackgroundTasks\Implementation\TaskManager; 4 5use ILIAS\BackgroundTasks\Bucket; 6use ILIAS\BackgroundTasks\Implementation\Bucket\State; 7use ILIAS\BackgroundTasks\Implementation\Tasks\UserInteraction\UserInteractionRequiredException; 8use ILIAS\BackgroundTasks\Implementation\Tasks\UserInteraction\UserInteractionSkippedException; 9use ILIAS\BackgroundTasks\Persistence; 10use ILIAS\BackgroundTasks\Task\UserInteraction; 11 12/** 13 * Class BasicTaskManager 14 * 15 * @package ILIAS\BackgroundTasks\Implementation 16 * 17 * @author Oskar Truffer <ot@studer-raimann.ch> 18 * 19 * Basic Task manager. Will execute tasks immediately. 20 * 21 * Some important infos: 22 * - The bucket and its tasks are not saved into the db upon execution 23 * - The percentage and current task are not updated during execution. 24 * - The bucket and its tasks inkl. percentage and current task are only saved into the DB 25 * when a user interaction occurs. 26 * 27 */ 28class SyncTaskManager extends BasicTaskManager 29{ 30 31 /** 32 * @var Persistence 33 */ 34 protected $persistence; 35 36 37 public function __construct(Persistence $persistence) 38 { 39 $this->persistence = $persistence; 40 } 41 42 43 /** 44 * This will add an Observer of the Task and start running the task. 45 * 46 * @param Bucket $bucket 47 * 48 * @return mixed|void 49 * @throws \Exception 50 * 51 */ 52 public function run(Bucket $bucket) 53 { 54 $task = $bucket->getTask(); 55 $bucket->setCurrentTask($task); 56 $observer = new NonPersistingObserver($bucket); 57 58 try { 59 $task = $this->executeTask($task, $observer); 60 if($task instanceof UserInteraction && $task->canBeSkipped($task->getInput())) { 61 throw new UserInteractionSkippedException('can be skipped'); 62 } 63 $bucket->setState(State::FINISHED); 64 } catch (UserInteractionSkippedException $e) { 65 $bucket->setState(State::FINISHED); 66 } catch (UserInteractionRequiredException $e) { 67 // We're okay! 68 $this->persistence->saveBucketAndItsTasks($bucket); 69 } 70 } 71} 72