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 }} key: ${{ secrets.PROD_SSH_KEY }}
port: 22 port: 22
script: | script: |
systemctl restart laravel-worker
cd /var/www/scheduler cd /var/www/scheduler
git pull origin master git pull origin master
npm install npm install

View File

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

View File

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

View File

@@ -24,16 +24,20 @@ class RunBot implements ShouldQueue
$bot = Bot::findOrFail($this->bot_id); $bot = Bot::findOrFail($this->bot_id);
$log = BotLog::findOrFail($this->log_id); $log = BotLog::findOrFail($this->log_id);
$log->update([
'status' => 'running',
]);
try { try {
$class = new $bot->class($bot->config ?? []); $class = new $bot->class($bot->config ?? []);
$class->run(); $result = $class->run();
// Update the log entry on success // Update the log entry on success
$log->update([ $log->update([
'finished_at' => now(), 'finished_at' => now(),
'status' => 'success', '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) { } catch (\Throwable $e) {
// Log the error in the bot log // 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() public function render()
{ {
return view('livewire.bot-logs', [ return view('livewire.bot-logs', [

View File

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

View File

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