diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index f5c7846..640b569 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -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 diff --git a/app/Bots/BotContract.php b/app/Bots/BotContract.php index 8379329..66b6ba2 100644 --- a/app/Bots/BotContract.php +++ b/app/Bots/BotContract.php @@ -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; } diff --git a/app/Bots/GenericApi.php b/app/Bots/GenericApi.php index f7e2a78..e9285d3 100644 --- a/app/Bots/GenericApi.php +++ b/app/Bots/GenericApi.php @@ -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 diff --git a/app/Jobs/RunBot.php b/app/Jobs/RunBot.php index e3574f6..08bda43 100644 --- a/app/Jobs/RunBot.php +++ b/app/Jobs/RunBot.php @@ -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 diff --git a/app/Livewire/BotLogs.php b/app/Livewire/BotLogs.php index efd3bb1..b1307b1 100644 --- a/app/Livewire/BotLogs.php +++ b/app/Livewire/BotLogs.php @@ -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("
{$output}")
+                ->info()
+                ->timer(null)
+                ->withCancelButton('Close')
+                ->show();
+        }
+    }
+
     public function render()
     {
         return view('livewire.bot-logs', [
diff --git a/app/Services/BotService.php b/app/Services/BotService.php
index 2cc0cb2..fc32022 100644
--- a/app/Services/BotService.php
+++ b/app/Services/BotService.php
@@ -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(),
diff --git a/resources/views/livewire/bot-logs.blade.php b/resources/views/livewire/bot-logs.blade.php
index 8dd6502..16ab78d 100644
--- a/resources/views/livewire/bot-logs.blade.php
+++ b/resources/views/livewire/bot-logs.blade.php
@@ -10,7 +10,16 @@
         
         
             @foreach ($logs as $log)
-                
+                status === "failed")
+                        wire:click="showError({{ $log->id }})"
+                        style="cursor: pointer;"
+                    @elseif ($log->status === "success")
+                        wire:click="showOutput({{ $log->id }})"
+                        style="cursor: pointer;"
+                    @endif
+                >
                     
                         {{ $log->bot->name }}
                     
@@ -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")