fileName = $fileName; $this->refreshTasks(); } /** * @inheritDoc * @throws TasksException */ public function addTask(Task $task): bool { // Check if the task doesn't exist yet $taskId = $task->getId(); foreach ($this->tasks as $t) { if ($t->getId() === $taskId) throw new TasksException("Could not add Task to TaskStorage. Task '$taskId' already exists."); } $this->tasks[] = $task; $this->commit(); return true; } /** * @inheritDoc */ public function readTasks(): array { return $this->tasks; } /** * @inheritDoc */ public function refreshTasks() { $this->tasks = []; $this->data = (array) include $this->fileName; if (empty($this->data)) $this->data = ['tasks' => []]; foreach ($this->data['tasks'] as $taskString) $this->tasks[] = unserialize($taskString); } /** * @inheritDoc * @throws TasksException */ public function getTaskById(string $identifier): Task { foreach ($this->tasks as $t) { if ($t->getId() === $identifier) return $t; } throw new TasksException("Could not get task by Id. Task not found."); } /** * @inheritDoc * @throws TasksException */ public function modifyTask(Task $task): bool { $taskId = $task->getId(); for ($i=0;$itasks);$i++) { if ($this->tasks[$i]->getId() === $taskId) { $this->tasks[$i] = $task; $this->commit(); return true; } } throw new TasksException("Could not modify task. Task '$taskId' doesn't exist."); } /** * @inheritDoc * @throws TasksException */ public function deleteTask(Task $task): bool { $taskId = $task->getId(); for ($i=0;$itasks);$i++) { if ($this->tasks[$i]->getId() === $taskId) { // Remove the task from the main storage unset($this->tasks[$i]); $this->commit(); // Remove all task output and post output $settings = $task->getSettings(); $maxRetries = $settings['maxRetries']; for ($j=0;$j<=$maxRetries;$j++) { // First remove all possible task output $outFile = dirname($this->fileName) . DS . 'task_' . md5($taskId) . '_' . $j . '_output.json'; if (file_exists($outFile)) unlink($outFile); $postFile = dirname($this->fileName) . DS . 'task_' . md5($taskId) . '_' . $j . '_post_output.json'; if (file_exists($postFile)) unlink($postFile); } return true; } } throw new TasksException("Could not delete task. Task '$taskId' doesn't exist."); } /** * @inheritDoc * @throws TasksException */ public function writeTaskOutput(Task $task, string $output, string $errors, int $statusCode, int $attempt = 0): bool { // Get the directory of the main storage $dir = dirname($this->fileName); $file = $dir . DS . 'task_' . md5($task->getId()) . '_' . $attempt . '_output.json'; $contents = json_encode(['taskId' => $task->getId(), 'output' => $output, 'errors' => $errors, 'statusCode' => $statusCode]); // If file already exists, panic if (file_exists($file)) throw new TasksException("Could not write task output. Output already written!"); if (!$this->write_file($file, $contents)) throw new TasksException("Could not write task output. File error."); return true; } /** * @inheritDoc * @throws TasksException */ public function writePostOutput(Task $task, string $output, string $errors, int $statusCode, int $attempt = 0): bool { // Get the directory of the main storage $dir = dirname($this->fileName); $file = $dir . DS . 'task_' . md5($task->getId()) . '_' . $attempt . '_post_output.json'; $contents = json_encode(['taskId' => $task->getId(), 'output' => $output, 'errors' => $errors, 'statusCode' => $statusCode]); // If file already exists, panic if (file_exists($file)) throw new TasksException("Could not write task output. Output already written!"); if (!$this->write_file($file, $contents)) throw new TasksException("Could not write task output. File error."); return true; } /** * @inheritDoc */ public function readTaskOutput(Task $task, int $attempt = 0): ?array { // Get the directory of the main storage $dir = dirname($this->fileName); $file = $dir . DS . 'task_' . md5($task->getId()) . '_' . $attempt . '_output.json'; // If file doesn't exist, return so if (!file_exists($file)) return null; // Decode the contents $contents = file_get_contents($file); $data = json_decode($contents, true); return ['output' => $data['output'], 'errors' => $data['errors'], 'statusCode' => $data['statusCode']]; } /** * @inheritDoc */ public function readPostOutput(Task $task, int $attempt = 0): ?array { // Get the directory of the main storage $dir = dirname($this->fileName); $file = $dir . DS . 'task_' . md5($task->getId()) . '_' . $attempt . '_post_output.json'; // If file doesn't exist, return so if (!file_exists($file)) return null; // Decode the contents $contents = file_get_contents($file); $data = json_decode($contents, true); return ['output' => $data['output'], 'errors' => $data['errors'], 'statusCode' => $data['statusCode']]; } /** * @inheritDoc * @throws TasksException */ public function reset(): bool { // Delete everything $this->refreshTasks(); for ($i=0;$itasks);$i++) { // Get the task $task = $this->tasks[$i]; $taskId = $task->getId(); // Remove all task output and post output $settings = $task->getSettings(); $maxRetries = $settings['maxRetries']; for ($j=0;$j<=$maxRetries;$j++) { // First remove all possible task output $outFile = dirname($this->fileName) . DS . 'task_' . md5($taskId) . '_' . $j . '_output.json'; if (file_exists($outFile)) unlink($outFile); $postFile = dirname($this->fileName) . DS . 'task_' . md5($taskId) . '_' . $j . '_post_output.json'; if (file_exists($postFile)) unlink($postFile); } // Remove the task from the main storage unset($this->tasks[$i]); } // And finally commit $this->commit(); return true; } private function commit() { $this->data = ['tasks' => []]; foreach ($this->tasks as $task) $this->data['tasks'][] = serialize($task); $content = var_export($this->data, true); file_put_contents($this->fileName, "