diff --git a/app/Bots/GenericApi.php b/app/Bots/GenericApi.php index a53b5d9..f7e2a78 100644 --- a/app/Bots/GenericApi.php +++ b/app/Bots/GenericApi.php @@ -19,27 +19,23 @@ class GenericApi implements BotContract public function run(): void { - try { - $this->client = new Client(); + $this->client = new Client(); - $options = []; + $options = []; - if (!empty($this->config['headers'])) { - $options['headers'] = json_decode($this->config['headers'], true); - } - - if (!empty($this->config['body'])) { - $options['json'] = json_decode($this->config['body'], true); - } - - $this->client->request( - $this->config['method'] ?? 'GET', - $this->config['url'], - $options - ); - } catch (GuzzleException $e) { - Log::error("Call to {$this->config['url']} failed" . $e->getMessage()); + if (!empty($this->config['headers'])) { + $options['headers'] = json_decode($this->config['headers'], true); } + + if (!empty($this->config['body'])) { + $options['json'] = json_decode($this->config['body'], true); + } + + $this->client->request( + $this->config['method'] ?? 'GET', + $this->config['url'], + $options + ); } public static function configSchema(): array diff --git a/app/Jobs/RunBot.php b/app/Jobs/RunBot.php index 59dc610..e3574f6 100644 --- a/app/Jobs/RunBot.php +++ b/app/Jobs/RunBot.php @@ -14,27 +14,30 @@ class RunBot implements ShouldQueue /** * Create a new job instance. */ - public function __construct(private Bot $bot, private BotLog $log) {} + public function __construct(private int $bot_id, private int $log_id) {} /** * Execute the job. */ public function handle(): void { - $class = new $this->bot->class($this->bot->config ?? []); + $bot = Bot::findOrFail($this->bot_id); + $log = BotLog::findOrFail($this->log_id); try { + $class = new $bot->class($bot->config ?? []); + $class->run(); // Update the log entry on success - $this->log->update([ + $log->update([ 'finished_at' => now(), 'status' => 'success', // 'output' => is_string($result) ? $result : json_encode($result, JSON_PRETTY_PRINT), ]); } catch (\Throwable $e) { // Log the error in the bot log - $this->log->update([ + $log->update([ 'finished_at' => now(), 'status' => 'failed', 'error' => $e->getMessage(), diff --git a/app/Livewire/BotLogs.php b/app/Livewire/BotLogs.php index f199c8e..efd3bb1 100644 --- a/app/Livewire/BotLogs.php +++ b/app/Livewire/BotLogs.php @@ -4,6 +4,7 @@ namespace App\Livewire; use App\Models\BotLog; use Illuminate\Support\Facades\Auth; +use Jantinnerezo\LivewireAlert\Facades\LivewireAlert; use Livewire\Component; use Livewire\WithoutUrlPagination; use Livewire\WithPagination; @@ -13,15 +14,18 @@ class BotLogs extends Component use WithPagination; use WithoutUrlPagination; - // public function mount() - // { - // $this->getLogs(); - // } - - // public function getLogs(): void - // { - // $this->logs = Auth::user()->bots->pluck('logs')->flatten()->sortByDesc('created_at'); - // } + public function showError(int $logId): void + { + $log = BotLog::find($logId); + if ($log) { + LivewireAlert::title('Error Details') + ->html("
{$log->error}")
+ ->warning()
+ ->timer(null)
+ ->withCancelButton('Close')
+ ->show();
+ }
+ }
public function render()
{
diff --git a/app/Livewire/BotsList.php b/app/Livewire/BotsList.php
index cc13936..9664bd9 100644
--- a/app/Livewire/BotsList.php
+++ b/app/Livewire/BotsList.php
@@ -78,7 +78,7 @@ class BotsList extends Component
]);
// Dispatch the job to run the bot
- dispatch(new RunBot($bot, $log));
+ dispatch(new RunBot($bot->id, $log->id));
flash()->success("Bot '{$bot->name}' is being executed.");
}
diff --git a/app/Livewire/ViewBot.php b/app/Livewire/ViewBot.php
index e2c239b..449bdc0 100644
--- a/app/Livewire/ViewBot.php
+++ b/app/Livewire/ViewBot.php
@@ -26,7 +26,7 @@ class ViewBot extends Component
]);
// Dispatch the job to run the bot
- dispatch(new RunBot($this->bot, $log));
+ dispatch(new RunBot($this->bot->id, $log->id));
flash()->success("Bot '{$this->bot->name}' is being executed.");
}
diff --git a/app/Services/BotService.php b/app/Services/BotService.php
index 8074e98..2cc0cb2 100644
--- a/app/Services/BotService.php
+++ b/app/Services/BotService.php
@@ -26,7 +26,7 @@ class BotService
$instance = app($bot->class, ['config' => $bot->config]);
if ($instance instanceof BotContract) {
- dispatch(RunBot::class, $bot, $log);
+ dispatch(RunBot::class, $bot->id, $log->id);
$log->update([
'started_at' => now(),
'status' => 'running'
diff --git a/resources/views/livewire/bot-logs.blade.php b/resources/views/livewire/bot-logs.blade.php
index 28b59cb..8dd6502 100644
--- a/resources/views/livewire/bot-logs.blade.php
+++ b/resources/views/livewire/bot-logs.blade.php
@@ -16,19 +16,26 @@