1
0

Task.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\TaskProcessing\Db;
  8. use OCP\AppFramework\Db\Entity;
  9. use OCP\TaskProcessing\Task as OCPTask;
  10. /**
  11. * @method setType(string $type)
  12. * @method string getType()
  13. * @method setLastUpdated(int $lastUpdated)
  14. * @method int getLastUpdated()
  15. * @method setStatus(int $status)
  16. * @method int getStatus()
  17. * @method setOutput(string $output)
  18. * @method string getOutput()
  19. * @method setInput(string $input)
  20. * @method string getInput()
  21. * @method setUserId(?string $userId)
  22. * @method string|null getUserId()
  23. * @method setAppId(string $type)
  24. * @method string getAppId()
  25. * @method setCustomId(string $customId)
  26. * @method string getCustomId()
  27. * @method setCompletionExpectedAt(null|\DateTime $completionExpectedAt)
  28. * @method null|\DateTime getCompletionExpectedAt()
  29. * @method setErrorMessage(null|string $error)
  30. * @method null|string getErrorMessage()
  31. * @method setProgress(null|float $progress)
  32. * @method null|float getProgress()
  33. * @method setWebhookUri(string $webhookUri)
  34. * @method string getWebhookUri()
  35. * @method setWebhookMethod(string $webhookMethod)
  36. * @method string getWebhookMethod()
  37. * @method setScheduledAt(int $scheduledAt)
  38. * @method int getScheduledAt()
  39. * @method setStartedAt(int $startedAt)
  40. * @method int getStartedAt()
  41. * @method setEndedAt(int $endedAt)
  42. * @method int getEndedAt()
  43. */
  44. class Task extends Entity {
  45. protected $lastUpdated;
  46. protected $type;
  47. protected $input;
  48. protected $output;
  49. protected $status;
  50. protected $userId;
  51. protected $appId;
  52. protected $customId;
  53. protected $completionExpectedAt;
  54. protected $errorMessage;
  55. protected $progress;
  56. protected $webhookUri;
  57. protected $webhookMethod;
  58. protected $scheduledAt;
  59. protected $startedAt;
  60. protected $endedAt;
  61. /**
  62. * @var string[]
  63. */
  64. public static array $columns = ['id', 'last_updated', 'type', 'input', 'output', 'status', 'user_id', 'app_id', 'custom_id', 'completion_expected_at', 'error_message', 'progress', 'webhook_uri', 'webhook_method', 'scheduled_at', 'started_at', 'ended_at'];
  65. /**
  66. * @var string[]
  67. */
  68. public static array $fields = ['id', 'lastUpdated', 'type', 'input', 'output', 'status', 'userId', 'appId', 'customId', 'completionExpectedAt', 'errorMessage', 'progress', 'webhookUri', 'webhookMethod', 'scheduledAt', 'startedAt', 'endedAt'];
  69. public function __construct() {
  70. // add types in constructor
  71. $this->addType('id', 'integer');
  72. $this->addType('lastUpdated', 'integer');
  73. $this->addType('type', 'string');
  74. $this->addType('input', 'string');
  75. $this->addType('output', 'string');
  76. $this->addType('status', 'integer');
  77. $this->addType('userId', 'string');
  78. $this->addType('appId', 'string');
  79. $this->addType('customId', 'string');
  80. $this->addType('completionExpectedAt', 'datetime');
  81. $this->addType('errorMessage', 'string');
  82. $this->addType('progress', 'float');
  83. $this->addType('webhookUri', 'string');
  84. $this->addType('webhookMethod', 'string');
  85. $this->addType('scheduledAt', 'integer');
  86. $this->addType('startedAt', 'integer');
  87. $this->addType('endedAt', 'integer');
  88. }
  89. public function toRow(): array {
  90. return array_combine(self::$columns, array_map(function ($field) {
  91. return $this->{'get' . ucfirst($field)}();
  92. }, self::$fields));
  93. }
  94. public static function fromPublicTask(OCPTask $task): self {
  95. /** @var Task $taskEntity */
  96. $taskEntity = self::fromParams([
  97. 'id' => $task->getId(),
  98. 'type' => $task->getTaskTypeId(),
  99. 'lastUpdated' => time(),
  100. 'status' => $task->getStatus(),
  101. 'input' => json_encode($task->getInput(), JSON_THROW_ON_ERROR),
  102. 'output' => json_encode($task->getOutput(), JSON_THROW_ON_ERROR),
  103. 'errorMessage' => $task->getErrorMessage(),
  104. 'userId' => $task->getUserId(),
  105. 'appId' => $task->getAppId(),
  106. 'customId' => $task->getCustomId(),
  107. 'completionExpectedAt' => $task->getCompletionExpectedAt(),
  108. 'progress' => $task->getProgress(),
  109. 'webhookUri' => $task->getWebhookUri(),
  110. 'webhookMethod' => $task->getWebhookMethod(),
  111. 'scheduledAt' => $task->getScheduledAt(),
  112. 'startedAt' => $task->getStartedAt(),
  113. 'endedAt' => $task->getEndedAt(),
  114. ]);
  115. return $taskEntity;
  116. }
  117. /**
  118. * @return OCPTask
  119. * @throws \JsonException
  120. */
  121. public function toPublicTask(): OCPTask {
  122. $task = new OCPTask($this->getType(), json_decode($this->getInput(), true, 512, JSON_THROW_ON_ERROR), $this->getAppId(), $this->getuserId(), $this->getCustomId());
  123. $task->setId($this->getId());
  124. $task->setStatus($this->getStatus());
  125. $task->setLastUpdated($this->getLastUpdated());
  126. $task->setOutput(json_decode($this->getOutput(), true, 512, JSON_THROW_ON_ERROR));
  127. $task->setCompletionExpectedAt($this->getCompletionExpectedAt());
  128. $task->setErrorMessage($this->getErrorMessage());
  129. $task->setProgress($this->getProgress());
  130. $task->setWebhookUri($this->getWebhookUri());
  131. $task->setWebhookMethod($this->getWebhookMethod());
  132. $task->setScheduledAt($this->getScheduledAt());
  133. $task->setStartedAt($this->getStartedAt());
  134. $task->setEndedAt($this->getEndedAt());
  135. return $task;
  136. }
  137. }