Fix job dispatch and show log output
All checks were successful
Deploy App / deploy (push) Successful in 11s

This commit is contained in:
2025-09-04 17:38:30 +02:00
parent 6a72b7150b
commit 1565df568d
7 changed files with 45 additions and 15 deletions

View File

@@ -52,6 +52,7 @@ jobs:
key: ${{ secrets.PROD_SSH_KEY }}
port: 22
script: |
systemctl restart laravel-worker
cd /var/www/scheduler
git pull origin master
npm install

View File

@@ -2,8 +2,10 @@
namespace App\Bots;
use Illuminate\Http\JsonResponse;
interface BotContract
{
public function run(): void;
public function run(): JsonResponse;
public static function configSchema(): array;
}

View File

@@ -3,8 +3,7 @@
namespace App\Bots;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\JsonResponse;
class GenericApi implements BotContract
{
@@ -17,7 +16,7 @@ class GenericApi implements BotContract
$this->config = $config;
}
public function run(): void
public function run(): JsonResponse
{
$this->client = new Client();
@@ -31,11 +30,16 @@ class GenericApi implements BotContract
$options['json'] = json_decode($this->config['body'], true);
}
$this->client->request(
$response = $this->client->request(
$this->config['method'] ?? 'GET',
$this->config['url'],
$options
);
return response()->json([
'status' => $response->getStatusCode(),
'body' => json_decode((string) $response->getBody(), true),
]);
}
public static function configSchema(): array

View File

@@ -24,16 +24,20 @@ class RunBot implements ShouldQueue
$bot = Bot::findOrFail($this->bot_id);
$log = BotLog::findOrFail($this->log_id);
$log->update([
'status' => 'running',
]);
try {
$class = new $bot->class($bot->config ?? []);
$class->run();
$result = $class->run();
// Update the log entry on success
$log->update([
'finished_at' => now(),
'status' => 'success',
// 'output' => is_string($result) ? $result : json_encode($result, JSON_PRETTY_PRINT),
'output' => is_string($result) ? $result : json_encode($result, JSON_PRETTY_PRINT),
]);
} catch (\Throwable $e) {
// Log the error in the bot log

View File

@@ -27,6 +27,20 @@ class BotLogs extends Component
}
}
public function showOutput(int $logId): void
{
$log = BotLog::find($logId);
if ($log) {
$output = $log->output ?? 'No output';
LivewireAlert::title('Output Details')
->html("<pre class='text-left rounded p-4 bg-gray-200 whitespace-pre-wrap break-words'><code>{$output}</code</pre>")
->info()
->timer(null)
->withCancelButton('Close')
->show();
}
}
public function render()
{
return view('livewire.bot-logs', [

View File

@@ -26,14 +26,14 @@ class BotService
$instance = app($bot->class, ['config' => $bot->config]);
if ($instance instanceof BotContract) {
dispatch(RunBot::class, $bot->id, $log->id);
RunBot::dispatch(bot_id: $bot->id, log_id: $log->id);
$log->update([
'started_at' => now(),
'status' => 'running'
'status' => 'pending'
]);
}
} catch (\Throwable $e) {
Log::error("Bot [{$bot->name}] failed: " . $e->getMessage());
Log::error("Bot [{$bot->name}] failed: " . $e->getMessage(), ['exception' => $e]);
$log->update([
'finished_at' => now(),

View File

@@ -10,7 +10,16 @@
</thead>
<tbody class="text-center">
@foreach ($logs as $log)
<tr>
<tr
class="hover:bg-zinc-700"
@if ($log->status === "failed")
wire:click="showError({{ $log->id }})"
style="cursor: pointer;"
@elseif ($log->status === "success")
wire:click="showOutput({{ $log->id }})"
style="cursor: pointer;"
@endif
>
<td class="px-4 py-2">
{{ $log->bot->name }}
</td>
@@ -21,10 +30,6 @@
"text-yellow-400" => $log->status === "pending",
]),
class="px-4 py-2"
@if ($log->status === "failed")
wire:click="showError({{ $log->id }})"
style="cursor: pointer;"
@endif
>
{{ $log->status }}
@if ($log->status === "failed")