aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristoph Wurst <ChristophWurst@users.noreply.github.com>2023-11-03 13:11:07 +0100
committerGitHub <noreply@github.com>2023-11-03 13:11:07 +0100
commit50de7553b582744c7376ca5c7bcff0ba47c1e6b3 (patch)
tree7b095eeeba1f1de6ed6eb1a0ce5cd00e75cfa18e
parentb038dbe0aef8c8680bd4f9075f00d8303338f518 (diff)
parentee76cc512ccb9dcc968345e821f50f65454fbaae (diff)
downloadnextcloud-server-50de7553b582744c7376ca5c7bcff0ba47c1e6b3.tar.gz
nextcloud-server-50de7553b582744c7376ca5c7bcff0ba47c1e6b3.zip
Merge pull request #41211 from nextcloud/fix/text-to-image
fix(TextToImage): Consistently use the right method to get the preferred providers
-rw-r--r--lib/private/TextToImage/Manager.php59
1 files changed, 30 insertions, 29 deletions
diff --git a/lib/private/TextToImage/Manager.php b/lib/private/TextToImage/Manager.php
index 1553fed7545..f68f657277f 100644
--- a/lib/private/TextToImage/Manager.php
+++ b/lib/private/TextToImage/Manager.php
@@ -113,25 +113,15 @@ class Manager implements IManager {
if (!$this->hasProviders()) {
throw new PreConditionNotMetException('No text to image provider is installed that can handle this task');
}
- $providers = $this->getProviders();
-
- $json = $this->config->getAppValue('core', 'ai.text2image_provider', '');
- if ($json !== '') {
- try {
- $className = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
- $provider = current(array_filter($providers, fn ($provider) => $provider::class === $className));
- if ($provider !== false) {
- $providers = [$provider];
- }
- } catch (\JsonException $e) {
- $this->logger->warning('Failed to decode Text2Image setting `ai.text2image_provider`', ['exception' => $e]);
- }
- }
+ $providers = $this->getPreferredProviders();
foreach ($providers as $provider) {
$this->logger->debug('Trying to run Text2Image provider '.$provider::class);
try {
$task->setStatus(Task::STATUS_RUNNING);
+ $completionExpectedAt = new \DateTime('now');
+ $completionExpectedAt->add(new \DateInterval('PT'.$provider->getExpectedRuntime().'S'));
+ $task->setCompletionExpectedAt($completionExpectedAt);
if ($task->getId() === null) {
$this->logger->debug('Inserting Text2Image task into DB');
$taskEntity = $this->taskMapper->insert(DbTask::fromPublicTask($task));
@@ -217,6 +207,9 @@ class Manager implements IManager {
}
$this->logger->debug('Scheduling Text2Image Task');
$task->setStatus(Task::STATUS_SCHEDULED);
+ $completionExpectedAt = new \DateTime('now');
+ $completionExpectedAt->add(new \DateInterval('PT'.$this->getPreferredProviders()[0]->getExpectedRuntime().'S'));
+ $task->setCompletionExpectedAt($completionExpectedAt);
$taskEntity = DbTask::fromPublicTask($task);
$this->taskMapper->insert($taskEntity);
$task->setId($taskEntity->getId());
@@ -232,22 +225,9 @@ class Manager implements IManager {
if (!$this->hasProviders()) {
throw new PreConditionNotMetException('No text to image provider is installed that can handle this task');
}
- $providers = $this->getProviders();
-
- $json = $this->config->getAppValue('core', 'ai.text2image_provider', '');
- if ($json !== '') {
- try {
- $id = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
- $provider = current(array_filter($providers, fn ($provider) => $provider->getId() === $id));
- if ($provider !== false) {
- $providers = [$provider];
- }
- } catch (\JsonException $e) {
- $this->logger->warning('Failed to decode Text2Image setting `ai.text2image_provider`', ['exception' => $e]);
- }
- }
+ $providers = $this->getPreferredProviders();
$maxExecutionTime = (int) ini_get('max_execution_time');
- // Offload the tttttttask to a background job if the expected runtime of the likely provider is longer than 80% of our max execution time
+ // Offload the task to a background job if the expected runtime of the likely provider is longer than 80% of our max execution time
if ($providers[0]->getExpectedRuntime() > $maxExecutionTime * 0.8) {
$this->scheduleTask($task);
return;
@@ -331,4 +311,25 @@ class Manager implements IManager {
throw new RuntimeException('Failure while trying to find tasks by appId and identifier: ' . $e->getMessage(), 0, $e);
}
}
+
+ /**
+ * @return IProvider[]
+ */
+ private function getPreferredProviders() {
+ $providers = $this->getProviders();
+ $json = $this->config->getAppValue('core', 'ai.text2image_provider', '');
+ if ($json !== '') {
+ try {
+ $id = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
+ $provider = current(array_filter($providers, fn ($provider) => $provider->getId() === $id));
+ if ($provider !== false && $provider !== null) {
+ $providers = [$provider];
+ }
+ } catch (\JsonException $e) {
+ $this->logger->warning('Failed to decode Text2Image setting `ai.text2image_provider`', ['exception' => $e]);
+ }
+ }
+
+ return $providers;
+ }
}