1
0

Task.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\TaskProcessing;
  8. use DateTime;
  9. use OCP\TaskProcessing\Exception\ValidationException;
  10. /**
  11. * This is a task processing task
  12. *
  13. * @since 30.0.0
  14. */
  15. final class Task implements \JsonSerializable {
  16. protected ?int $id = null;
  17. protected ?DateTime $completionExpectedAt = null;
  18. protected ?array $output = null;
  19. protected ?string $errorMessage = null;
  20. protected ?float $progress = null;
  21. protected int $lastUpdated;
  22. protected ?string $webhookUri = null;
  23. protected ?string $webhookMethod = null;
  24. /**
  25. * @since 30.0.0
  26. */
  27. public const STATUS_CANCELLED = 5;
  28. /**
  29. * @since 30.0.0
  30. */
  31. public const STATUS_FAILED = 4;
  32. /**
  33. * @since 30.0.0
  34. */
  35. public const STATUS_SUCCESSFUL = 3;
  36. /**
  37. * @since 30.0.0
  38. */
  39. public const STATUS_RUNNING = 2;
  40. /**
  41. * @since 30.0.0
  42. */
  43. public const STATUS_SCHEDULED = 1;
  44. /**
  45. * @since 30.0.0
  46. */
  47. public const STATUS_UNKNOWN = 0;
  48. /**
  49. * @psalm-var self::STATUS_*
  50. */
  51. protected int $status = self::STATUS_UNKNOWN;
  52. protected ?int $scheduledAt = null;
  53. protected ?int $startedAt = null;
  54. protected ?int $endedAt = null;
  55. /**
  56. * @param string $taskTypeId
  57. * @param array<string,list<numeric|string>|numeric|string> $input
  58. * @param string $appId
  59. * @param string|null $userId
  60. * @param null|string $customId An arbitrary customId for this task. max length: 255 chars
  61. * @since 30.0.0
  62. */
  63. final public function __construct(
  64. protected readonly string $taskTypeId,
  65. protected array $input,
  66. protected readonly string $appId,
  67. protected readonly ?string $userId,
  68. protected readonly ?string $customId = '',
  69. ) {
  70. $this->lastUpdated = time();
  71. }
  72. /**
  73. * @since 30.0.0
  74. */
  75. final public function getTaskTypeId(): string {
  76. return $this->taskTypeId;
  77. }
  78. /**
  79. * @psalm-return self::STATUS_*
  80. * @since 30.0.0
  81. */
  82. final public function getStatus(): int {
  83. return $this->status;
  84. }
  85. /**
  86. * @psalm-param self::STATUS_* $status
  87. * @since 30.0.0
  88. */
  89. final public function setStatus(int $status): void {
  90. $this->status = $status;
  91. }
  92. /**
  93. * @param ?DateTime $at
  94. * @since 30.0.0
  95. */
  96. final public function setCompletionExpectedAt(?DateTime $at): void {
  97. $this->completionExpectedAt = $at;
  98. }
  99. /**
  100. * @return ?DateTime
  101. * @since 30.0.0
  102. */
  103. final public function getCompletionExpectedAt(): ?DateTime {
  104. return $this->completionExpectedAt;
  105. }
  106. /**
  107. * @return int|null
  108. * @since 30.0.0
  109. */
  110. final public function getId(): ?int {
  111. return $this->id;
  112. }
  113. /**
  114. * @param int|null $id
  115. * @since 30.0.0
  116. */
  117. final public function setId(?int $id): void {
  118. $this->id = $id;
  119. }
  120. /**
  121. * @param null|array<array-key, list<numeric|string>|numeric|string> $output
  122. * @since 30.0.0
  123. */
  124. final public function setOutput(?array $output): void {
  125. $this->output = $output;
  126. }
  127. /**
  128. * @return array<array-key, list<numeric|string>|numeric|string>|null
  129. * @since 30.0.0
  130. */
  131. final public function getOutput(): ?array {
  132. return $this->output;
  133. }
  134. /**
  135. * @return array<array-key, list<numeric|string>|numeric|string>
  136. * @since 30.0.0
  137. */
  138. final public function getInput(): array {
  139. return $this->input;
  140. }
  141. /**
  142. * @return string
  143. * @since 30.0.0
  144. */
  145. final public function getAppId(): string {
  146. return $this->appId;
  147. }
  148. /**
  149. * @return null|string
  150. * @since 30.0.0
  151. */
  152. final public function getCustomId(): ?string {
  153. return $this->customId;
  154. }
  155. /**
  156. * @return string|null
  157. * @since 30.0.0
  158. */
  159. final public function getUserId(): ?string {
  160. return $this->userId;
  161. }
  162. /**
  163. * @return int
  164. * @since 30.0.0
  165. */
  166. final public function getLastUpdated(): int {
  167. return $this->lastUpdated;
  168. }
  169. /**
  170. * @param int $lastUpdated
  171. * @since 30.0.0
  172. */
  173. final public function setLastUpdated(int $lastUpdated): void {
  174. $this->lastUpdated = $lastUpdated;
  175. }
  176. /**
  177. * @return int|null
  178. * @since 30.0.0
  179. */
  180. final public function getScheduledAt(): ?int {
  181. return $this->scheduledAt;
  182. }
  183. /**
  184. * @param int|null $scheduledAt
  185. * @since 30.0.0
  186. */
  187. final public function setScheduledAt(?int $scheduledAt): void {
  188. $this->scheduledAt = $scheduledAt;
  189. }
  190. /**
  191. * @return int|null
  192. * @since 30.0.0
  193. */
  194. final public function getStartedAt(): ?int {
  195. return $this->startedAt;
  196. }
  197. /**
  198. * @param int|null $startedAt
  199. * @since 30.0.0
  200. */
  201. final public function setStartedAt(?int $startedAt): void {
  202. $this->startedAt = $startedAt;
  203. }
  204. /**
  205. * @return int|null
  206. * @since 30.0.0
  207. */
  208. final public function getEndedAt(): ?int {
  209. return $this->endedAt;
  210. }
  211. /**
  212. * @param int|null $endedAt
  213. * @since 30.0.0
  214. */
  215. final public function setEndedAt(?int $endedAt): void {
  216. $this->endedAt = $endedAt;
  217. }
  218. /**
  219. * @psalm-return array{id: ?int, lastUpdated: int, type: string, status: 'STATUS_CANCELLED'|'STATUS_FAILED'|'STATUS_SUCCESSFUL'|'STATUS_RUNNING'|'STATUS_SCHEDULED'|'STATUS_UNKNOWN', userId: ?string, appId: string, input: array<array-key, list<numeric|string>|numeric|string>, output: ?array<array-key, list<numeric|string>|numeric|string>, customId: ?string, completionExpectedAt: ?int, progress: ?float, scheduledAt: ?int, startedAt: ?int, endedAt: ?int}
  220. * @since 30.0.0
  221. */
  222. final public function jsonSerialize(): array {
  223. return [
  224. 'id' => $this->getId(),
  225. 'type' => $this->getTaskTypeId(),
  226. 'lastUpdated' => $this->getLastUpdated(),
  227. 'status' => self::statusToString($this->getStatus()),
  228. 'userId' => $this->getUserId(),
  229. 'appId' => $this->getAppId(),
  230. 'input' => $this->getInput(),
  231. 'output' => $this->getOutput(),
  232. 'customId' => $this->getCustomId(),
  233. 'completionExpectedAt' => $this->getCompletionExpectedAt()?->getTimestamp(),
  234. 'progress' => $this->getProgress(),
  235. 'scheduledAt' => $this->getScheduledAt(),
  236. 'startedAt' => $this->getStartedAt(),
  237. 'endedAt' => $this->getEndedAt(),
  238. ];
  239. }
  240. /**
  241. * @param string|null $error
  242. * @return void
  243. * @since 30.0.0
  244. */
  245. final public function setErrorMessage(?string $error) {
  246. $this->errorMessage = $error;
  247. }
  248. /**
  249. * @return string|null
  250. * @since 30.0.0
  251. */
  252. final public function getErrorMessage(): ?string {
  253. return $this->errorMessage;
  254. }
  255. /**
  256. * @param array $input
  257. * @return void
  258. * @since 30.0.0
  259. */
  260. final public function setInput(array $input): void {
  261. $this->input = $input;
  262. }
  263. /**
  264. * @param float|null $progress
  265. * @return void
  266. * @throws ValidationException
  267. * @since 30.0.0
  268. */
  269. final public function setProgress(?float $progress): void {
  270. if ($progress < 0 || $progress > 1.0) {
  271. throw new ValidationException('Progress must be between 0.0 and 1.0 inclusively; ' . $progress . ' given');
  272. }
  273. $this->progress = $progress;
  274. }
  275. /**
  276. * @return float|null
  277. * @since 30.0.0
  278. */
  279. final public function getProgress(): ?float {
  280. return $this->progress;
  281. }
  282. /**
  283. * @return null|string
  284. * @since 30.0.0
  285. */
  286. final public function getWebhookUri(): ?string {
  287. return $this->webhookUri;
  288. }
  289. /**
  290. * @param string|null $webhookUri
  291. * @return void
  292. * @since 30.0.0
  293. */
  294. final public function setWebhookUri(?string $webhookUri): void {
  295. $this->webhookUri = $webhookUri;
  296. }
  297. /**
  298. * @return null|string
  299. * @since 30.0.0
  300. */
  301. final public function getWebhookMethod(): ?string {
  302. return $this->webhookMethod;
  303. }
  304. /**
  305. * @param string|null $webhookMethod
  306. * @return void
  307. * @since 30.0.0
  308. */
  309. final public function setWebhookMethod(?string $webhookMethod): void {
  310. $this->webhookMethod = $webhookMethod;
  311. }
  312. /**
  313. * @param int $status
  314. * @return 'STATUS_CANCELLED'|'STATUS_FAILED'|'STATUS_SUCCESSFUL'|'STATUS_RUNNING'|'STATUS_SCHEDULED'|'STATUS_UNKNOWN'
  315. * @since 30.0.0
  316. */
  317. final public static function statusToString(int $status): string {
  318. return match ($status) {
  319. self::STATUS_CANCELLED => 'STATUS_CANCELLED',
  320. self::STATUS_FAILED => 'STATUS_FAILED',
  321. self::STATUS_SUCCESSFUL => 'STATUS_SUCCESSFUL',
  322. self::STATUS_RUNNING => 'STATUS_RUNNING',
  323. self::STATUS_SCHEDULED => 'STATUS_SCHEDULED',
  324. default => 'STATUS_UNKNOWN',
  325. };
  326. }
  327. }