Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TextToImage): Consistently use the right method to get the preferred providers #41211

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 24 additions & 29 deletions lib/private/TextToImage/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,7 @@ public function runTask(Task $task): void {
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);
Expand Down Expand Up @@ -232,22 +219,9 @@ public function runOrScheduleTask(Task $task) : void {
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
marcelklehr marked this conversation as resolved.
Show resolved Hide resolved
if ($providers[0]->getExpectedRuntime() > $maxExecutionTime * 0.8) {
$this->scheduleTask($task);
return;
Expand Down Expand Up @@ -331,4 +305,25 @@ public function getUserTasksByApp(?string $userId, string $appId, ?string $ident
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;
}
}
Loading