Skip to content

Commit

Permalink
optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor committed Mar 18, 2024
1 parent 8b622bb commit d20e730
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public static function execute(Task $task)
{
$discordId = $task->discordId() ?: null;
if (!$instance = static::getIdleInstance($discordId)) {
Log::debug("DISCORD No idle instance found");
Log::debug("TASK:{$task->id()} DISCORD No idle instance found");
return;
}
$instance->lastSubmitTime = microtime(true);
Expand Down
43 changes: 20 additions & 23 deletions src/Service/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Webman\Midjourney\Service;

use Throwable;
use Webman\Midjourney\Config;
use Webman\Midjourney\Discord;
use Webman\Midjourney\Task;
Expand Down Expand Up @@ -32,14 +33,7 @@ protected static function payloadJson(Task $task, Discord $discord, array $param
'payload_json' => json_encode($params)
],
'success' => function ($response) use ($task, $discord) {
$content = (string)$response->getBody();
$json = $content ? json_decode($content, true) : null;
if ($content === '' || ($json['retry_after'] ?? 0)) {
$task->status(Task::STATUS_SUBMITTED)->save();
Discord::notify($task);
return;
}
Discord::failed($task, $json ? json_encode($json, JSON_UNESCAPED_UNICODE) : $content);
static::successCallback($response, $task);
},
'error' => function ($error) use ($task, $discord) {
Discord::failed($task, $error->getMessage());
Expand All @@ -57,21 +51,30 @@ protected static function json(Task $task, Discord $discord, array $params)
],
'data' => json_encode($params),
'success' => function ($response) use ($task, $discord) {
$content = (string)$response->getBody();
$json = $content ? json_decode($content, true) : null;
if ($content === '' || ($json['retry_after'] ?? 0)) {
$task->status(Task::STATUS_SUBMITTED)->save();
Discord::notify($task);
return;
}
Discord::failed($task, $json ? json_encode($json, JSON_UNESCAPED_UNICODE) : $content);
static::successCallback($response, $task);
},
'error' => function ($error) use ($task, $discord) {
Discord::failed($task, $error->getMessage());
}
];
}

protected static function successCallback($response, Task $task)
{
try {
$content = (string)$response->getBody();
$json = $content ? json_decode($content, true) : null;
if ($content === '' || ($json['retry_after'] ?? 0)) {
$task->status(Task::STATUS_SUBMITTED)->save();
Discord::notify($task);
return;
}
Discord::failed($task, $json ? json_encode($json, JSON_UNESCAPED_UNICODE) : $content);
} catch (Throwable $e) {
Discord::failed($task, (string)$e);
}
}

public static function change(Task $task, Discord $discord)
{
$params = static::getParams($task, $discord);
Expand Down Expand Up @@ -142,13 +145,7 @@ public static function cancelJob(Task $task, Discord $discord)
],
'data' => json_encode($params),
'success' => function ($response) use ($task, $discord) {
$content = (string)$response->getBody();
$json = $content ? json_decode($content, true) : null;
if ($content === '' || ($json['retry_after'] ?? 0)) {
Discord::finished($task);
return;
}
Discord::failed($task, $json ? json_encode($json, JSON_UNESCAPED_UNICODE) : $content);
static::successCallback($response, $task);
},
'error' => function ($error) use ($task, $discord) {
Discord::failed($task, $error->getMessage());
Expand Down

0 comments on commit d20e730

Please sign in to comment.