taskId = $identifier; $this->handlerClass = $handlerClass; $this->usePostHandler = $usePostHandler; if (func_num_args() > 3) $args = array_slice(func_get_args(), 3); else $args = []; foreach ($args as $arg) if (!$this->isSerializable($arg)) throw new TasksException("Could not create Task. Provided arguments are not serializable."); $this->arguments = $args; } /** * Returns the unique ID of this task * * @return string */ public function getId(): string { return $this->taskId; } /** * Gets the name of the class that shall process this task * * @return string */ public function getHandlerClass(): string { return $this->handlerClass; } /** * Whether the postHandler on the handlerClass should be invoked after processing the initial task. * * @return bool */ public function getUsePostHandler(): bool { return $this->usePostHandler; } /** * Gets the arguments to be provided to the method of the class that shall process this task * * @return array */ public function getArguments(): array { return $this->arguments; } /** * Add a constraint to this Task; in order to make the execution conditional. * * @param Constraint $constraint */ public function addConstraint(Constraint $constraint) { $this->constraints[] = $constraint; } /** * Gets the constraints to this job. * * @return Constraint[] */ public function getConstraints(): array { return $this->constraints; } /** * Gets the current status of this Task * * @return int */ public function getStatus(): int { return $this->status; } /** * Sets the status of this Task. * * Must be one of the constants of this Task class * * @param int $status */ public function setStatus(int $status) { $this->status = $status; } /** * Set the delay time to the Task * * @param int $delayTime */ public function setDelayTime(int $delayTime) { $this->delayTime = $delayTime; } /** * Receive the delay time for this task * * @return int */ public function getDelayTime(): int { return $this->delayTime; } /** * Fetch an attribute of this task * * @param string $key * @return mixed */ public function attribute(string $key) { if (!isset($this->attributes[$key])) return null; return $this->attributes[$key]; } /** * Adds an attribute to this task. * * @param string $key * @param $value * @throws TasksException */ public function addAttribute(string $key, $value) { if (!$this->isSerializable($value)) throw new TasksException("Could not set Task '$this->taskId' attribute '$key'. Value not serializable."); $this->attributes[$key] = $value; } /** * Return the output of this task execution * * @return string|null */ public function getOutput(): ?string { return $this->output; } public function getPostOutput(): ?string { return $this->postOutput; } /** * Return the errors of this task execution * * @return string|null */ public function getErrors(): ?string { return $this->errors; } public function getPostErrors(): ?string { return $this->postErrors; } /** * @todo Handle output from multiple attempts * @param string $output * @param string $errors */ public function setOutput(string $output, string $errors) { $this->output = $output; $this->errors = $errors; } /** * @todo Handle output from multiple attempts * @param string $output * @param string $errors */ public function setPostOutput(string $output, string $errors) { $this->postOutput = $output; $this->postErrors = $errors; } /** * Sets the initial process for this task. * * To be set by Executor * * @param Process $process */ public function setProcess(Process $process) { $this->process = $process; } /** * Returns the initial process for this task * * @return Process|null */ public function getProcess(): ?Process { return $this->process; } public function removeProcess(): bool { if ($this->process instanceof Process) { $this->process = null; return true; } return false; } /** * Set whether this task should retry after a failure, and how many times * * @param bool $retryOnFail * @param int $maxRetries * @param bool $retryRegularFailures * @param bool $retryProcessFailures * @param bool $retryPostFailures */ public function setRetrySettings(bool $retryOnFail, int $maxRetries = 2, bool $retryRegularFailures = true, bool $retryProcessFailures = true, bool $retryPostFailures = true) { $this->retryOnFail = $retryOnFail; $this->maxRetries = $maxRetries; $this->retryPFailures = $retryProcessFailures; $this->retryRFailures = $retryRegularFailures; $this->retryPostFailures = $retryPostFailures; } /** * Returns the failure retry settings * * @return array */ public function getRetrySettings(): array { return [ 'retryOnFail' => $this->retryOnFail, 'maxRetries' => $this->maxRetries, 'retryPFailures' => $this->retryPFailures, 'retryRFailures' => $this->retryRFailures, 'retryPostFailures' => $this->retryPostFailures ]; } /** * Add a retry to the retry counter */ public function addRetry() { $this->retries++; } /** * Reset the retry counter back to 0 */ public function resetRetries() { $this->retries = 0; } /** * Receive the amount of retries already attempted * * @return int */ public function getRetries(): int { return $this->retries; } /** * Checks whether an object can be serialized * * @param $value * @return bool */ private function isSerializable ($value) { $return = true; $arr = array($value); array_walk_recursive($arr, function ($element) use (&$return) { if (is_object($element) && get_class($element) == 'Closure') { $return = false; } }); return $return; } }