1
0

Manager.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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\TextToImage;
  8. use OC\AppFramework\Bootstrap\Coordinator;
  9. use OC\TextToImage\Db\Task as DbTask;
  10. use OC\TextToImage\Db\TaskMapper;
  11. use OCP\AppFramework\Db\DoesNotExistException;
  12. use OCP\AppFramework\Db\MultipleObjectsReturnedException;
  13. use OCP\BackgroundJob\IJobList;
  14. use OCP\DB\Exception;
  15. use OCP\Files\AppData\IAppDataFactory;
  16. use OCP\Files\IAppData;
  17. use OCP\Files\NotFoundException;
  18. use OCP\Files\NotPermittedException;
  19. use OCP\IConfig;
  20. use OCP\IServerContainer;
  21. use OCP\PreConditionNotMetException;
  22. use OCP\TextToImage\Exception\TaskFailureException;
  23. use OCP\TextToImage\Exception\TaskNotFoundException;
  24. use OCP\TextToImage\IManager;
  25. use OCP\TextToImage\IProvider;
  26. use OCP\TextToImage\IProviderWithUserId;
  27. use OCP\TextToImage\Task;
  28. use Psr\Log\LoggerInterface;
  29. use RuntimeException;
  30. use Throwable;
  31. class Manager implements IManager {
  32. /** @var ?list<IProvider> */
  33. private ?array $providers = null;
  34. private IAppData $appData;
  35. public function __construct(
  36. private IServerContainer $serverContainer,
  37. private Coordinator $coordinator,
  38. private LoggerInterface $logger,
  39. private IJobList $jobList,
  40. private TaskMapper $taskMapper,
  41. private IConfig $config,
  42. IAppDataFactory $appDataFactory,
  43. ) {
  44. $this->appData = $appDataFactory->get('core');
  45. }
  46. /**
  47. * @inheritDoc
  48. */
  49. public function getProviders(): array {
  50. $context = $this->coordinator->getRegistrationContext();
  51. if ($context === null) {
  52. return [];
  53. }
  54. if ($this->providers !== null) {
  55. return $this->providers;
  56. }
  57. $this->providers = [];
  58. foreach ($context->getTextToImageProviders() as $providerServiceRegistration) {
  59. $class = $providerServiceRegistration->getService();
  60. try {
  61. /** @var IProvider $provider */
  62. $provider = $this->serverContainer->get($class);
  63. $this->providers[] = $provider;
  64. } catch (Throwable $e) {
  65. $this->logger->error('Failed to load Text to image provider ' . $class, [
  66. 'exception' => $e,
  67. ]);
  68. }
  69. }
  70. return $this->providers;
  71. }
  72. /**
  73. * @inheritDoc
  74. */
  75. public function hasProviders(): bool {
  76. $context = $this->coordinator->getRegistrationContext();
  77. if ($context === null) {
  78. return false;
  79. }
  80. return count($context->getTextToImageProviders()) > 0;
  81. }
  82. /**
  83. * @inheritDoc
  84. */
  85. public function runTask(Task $task): void {
  86. $this->logger->debug('Running TextToImage Task');
  87. if (!$this->hasProviders()) {
  88. throw new PreConditionNotMetException('No text to image provider is installed that can handle this task');
  89. }
  90. $providers = $this->getPreferredProviders();
  91. foreach ($providers as $provider) {
  92. $this->logger->debug('Trying to run Text2Image provider ' . $provider::class);
  93. try {
  94. $task->setStatus(Task::STATUS_RUNNING);
  95. $completionExpectedAt = new \DateTime('now');
  96. $completionExpectedAt->add(new \DateInterval('PT' . $provider->getExpectedRuntime() . 'S'));
  97. $task->setCompletionExpectedAt($completionExpectedAt);
  98. if ($task->getId() === null) {
  99. $this->logger->debug('Inserting Text2Image task into DB');
  100. $taskEntity = $this->taskMapper->insert(DbTask::fromPublicTask($task));
  101. $task->setId($taskEntity->getId());
  102. } else {
  103. $this->logger->debug('Updating Text2Image task in DB');
  104. $this->taskMapper->update(DbTask::fromPublicTask($task));
  105. }
  106. try {
  107. $folder = $this->appData->getFolder('text2image');
  108. } catch (NotFoundException) {
  109. $this->logger->debug('Creating folder in appdata for Text2Image results');
  110. $folder = $this->appData->newFolder('text2image');
  111. }
  112. try {
  113. $folder = $folder->getFolder((string)$task->getId());
  114. } catch (NotFoundException) {
  115. $this->logger->debug('Creating new folder in appdata Text2Image results folder');
  116. $folder = $folder->newFolder((string)$task->getId());
  117. }
  118. $this->logger->debug('Creating result files for Text2Image task');
  119. $resources = [];
  120. $files = [];
  121. for ($i = 0; $i < $task->getNumberOfImages(); $i++) {
  122. $file = $folder->newFile((string)$i);
  123. $files[] = $file;
  124. $resource = $file->write();
  125. if ($resource !== false && $resource !== true && is_resource($resource)) {
  126. $resources[] = $resource;
  127. } else {
  128. throw new RuntimeException('Text2Image generation using provider "' . $provider->getName() . '" failed: Couldn\'t open file to write.');
  129. }
  130. }
  131. $this->logger->debug('Calling Text2Image provider\'s generate method');
  132. if ($provider instanceof IProviderWithUserId) {
  133. $provider->setUserId($task->getUserId());
  134. }
  135. $provider->generate($task->getInput(), $resources);
  136. for ($i = 0; $i < $task->getNumberOfImages(); $i++) {
  137. if (is_resource($resources[$i])) {
  138. // If $resource hasn't been closed yet, we'll do that here
  139. fclose($resources[$i]);
  140. }
  141. }
  142. $task->setStatus(Task::STATUS_SUCCESSFUL);
  143. $this->logger->debug('Updating Text2Image task in DB');
  144. $this->taskMapper->update(DbTask::fromPublicTask($task));
  145. return;
  146. } catch (\RuntimeException|\Throwable $e) {
  147. for ($i = 0; $i < $task->getNumberOfImages(); $i++) {
  148. if (isset($files, $files[$i])) {
  149. try {
  150. $files[$i]->delete();
  151. } catch (NotPermittedException $e) {
  152. $this->logger->warning('Failed to clean up Text2Image result file after error', ['exception' => $e]);
  153. }
  154. }
  155. }
  156. $this->logger->info('Text2Image generation using provider "' . $provider->getName() . '" failed', ['exception' => $e]);
  157. $task->setStatus(Task::STATUS_FAILED);
  158. try {
  159. $this->taskMapper->update(DbTask::fromPublicTask($task));
  160. } catch (Exception $e) {
  161. $this->logger->warning('Failed to update database after Text2Image error', ['exception' => $e]);
  162. }
  163. throw new TaskFailureException('Text2Image generation using provider "' . $provider->getName() . '" failed: ' . $e->getMessage(), 0, $e);
  164. }
  165. }
  166. $task->setStatus(Task::STATUS_FAILED);
  167. try {
  168. $this->taskMapper->update(DbTask::fromPublicTask($task));
  169. } catch (Exception $e) {
  170. $this->logger->warning('Failed to update database after Text2Image error', ['exception' => $e]);
  171. }
  172. throw new TaskFailureException('Could not run task');
  173. }
  174. /**
  175. * @inheritDoc
  176. */
  177. public function scheduleTask(Task $task): void {
  178. if (!$this->hasProviders()) {
  179. throw new PreConditionNotMetException('No text to image provider is installed that can handle this task');
  180. }
  181. $this->logger->debug('Scheduling Text2Image Task');
  182. $task->setStatus(Task::STATUS_SCHEDULED);
  183. $completionExpectedAt = new \DateTime('now');
  184. $completionExpectedAt->add(new \DateInterval('PT' . $this->getPreferredProviders()[0]->getExpectedRuntime() . 'S'));
  185. $task->setCompletionExpectedAt($completionExpectedAt);
  186. $taskEntity = DbTask::fromPublicTask($task);
  187. $this->taskMapper->insert($taskEntity);
  188. $task->setId($taskEntity->getId());
  189. $this->jobList->add(TaskBackgroundJob::class, [
  190. 'taskId' => $task->getId()
  191. ]);
  192. }
  193. /**
  194. * @inheritDoc
  195. */
  196. public function runOrScheduleTask(Task $task) : void {
  197. if (!$this->hasProviders()) {
  198. throw new PreConditionNotMetException('No text to image provider is installed that can handle this task');
  199. }
  200. $providers = $this->getPreferredProviders();
  201. $maxExecutionTime = (int)ini_get('max_execution_time');
  202. // Offload the task to a background job if the expected runtime of the likely provider is longer than 80% of our max execution time
  203. if ($providers[0]->getExpectedRuntime() > $maxExecutionTime * 0.8) {
  204. $this->scheduleTask($task);
  205. return;
  206. }
  207. $this->runTask($task);
  208. }
  209. /**
  210. * @inheritDoc
  211. */
  212. public function deleteTask(Task $task): void {
  213. $taskEntity = DbTask::fromPublicTask($task);
  214. $this->taskMapper->delete($taskEntity);
  215. $this->jobList->remove(TaskBackgroundJob::class, [
  216. 'taskId' => $task->getId()
  217. ]);
  218. }
  219. /**
  220. * Get a task from its id
  221. *
  222. * @param int $id The id of the task
  223. * @return Task
  224. * @throws RuntimeException If the query failed
  225. * @throws TaskNotFoundException If the task could not be found
  226. */
  227. public function getTask(int $id): Task {
  228. try {
  229. $taskEntity = $this->taskMapper->find($id);
  230. return $taskEntity->toPublicTask();
  231. } catch (DoesNotExistException $e) {
  232. throw new TaskNotFoundException('Could not find task with the provided id');
  233. } catch (MultipleObjectsReturnedException $e) {
  234. throw new RuntimeException('Could not uniquely identify task with given id', 0, $e);
  235. } catch (Exception $e) {
  236. throw new RuntimeException('Failure while trying to find task by id: ' . $e->getMessage(), 0, $e);
  237. }
  238. }
  239. /**
  240. * Get a task from its user id and task id
  241. * If userId is null, this can only get a task that was scheduled anonymously
  242. *
  243. * @param int $id The id of the task
  244. * @param string|null $userId The user id that scheduled the task
  245. * @return Task
  246. * @throws RuntimeException If the query failed
  247. * @throws TaskNotFoundException If the task could not be found
  248. */
  249. public function getUserTask(int $id, ?string $userId): Task {
  250. try {
  251. $taskEntity = $this->taskMapper->findByIdAndUser($id, $userId);
  252. return $taskEntity->toPublicTask();
  253. } catch (DoesNotExistException $e) {
  254. throw new TaskNotFoundException('Could not find task with the provided id and user id');
  255. } catch (MultipleObjectsReturnedException $e) {
  256. throw new RuntimeException('Could not uniquely identify task with given id and user id', 0, $e);
  257. } catch (Exception $e) {
  258. throw new RuntimeException('Failure while trying to find task by id and user id: ' . $e->getMessage(), 0, $e);
  259. }
  260. }
  261. /**
  262. * Get a list of tasks scheduled by a specific user for a specific app
  263. * and optionally with a specific identifier.
  264. * This cannot be used to get anonymously scheduled tasks
  265. *
  266. * @param string $userId
  267. * @param string $appId
  268. * @param string|null $identifier
  269. * @return Task[]
  270. * @throws RuntimeException
  271. */
  272. public function getUserTasksByApp(?string $userId, string $appId, ?string $identifier = null): array {
  273. try {
  274. $taskEntities = $this->taskMapper->findUserTasksByApp($userId, $appId, $identifier);
  275. return array_map(static function (DbTask $taskEntity) {
  276. return $taskEntity->toPublicTask();
  277. }, $taskEntities);
  278. } catch (Exception $e) {
  279. throw new RuntimeException('Failure while trying to find tasks by appId and identifier: ' . $e->getMessage(), 0, $e);
  280. }
  281. }
  282. /**
  283. * @return list<IProvider>
  284. */
  285. private function getPreferredProviders() {
  286. $providers = $this->getProviders();
  287. $json = $this->config->getAppValue('core', 'ai.text2image_provider', '');
  288. if ($json !== '') {
  289. try {
  290. $id = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
  291. $provider = current(array_filter($providers, fn ($provider) => $provider->getId() === $id));
  292. if ($provider !== false && $provider !== null) {
  293. $providers = [$provider];
  294. }
  295. } catch (\JsonException $e) {
  296. $this->logger->warning('Failed to decode Text2Image setting `ai.text2image_provider`', ['exception' => $e]);
  297. }
  298. }
  299. return $providers;
  300. }
  301. }