Show error
All checks were successful
Deploy App / deploy (push) Successful in 11s

This commit is contained in:
2025-08-31 22:41:48 +02:00
parent 7764af432a
commit 6a72b7150b
7 changed files with 49 additions and 39 deletions

View File

@@ -19,27 +19,23 @@ class GenericApi implements BotContract
public function run(): void public function run(): void
{ {
try { $this->client = new Client();
$this->client = new Client();
$options = []; $options = [];
if (!empty($this->config['headers'])) { if (!empty($this->config['headers'])) {
$options['headers'] = json_decode($this->config['headers'], true); $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['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 public static function configSchema(): array

View File

@@ -14,27 +14,30 @@ class RunBot implements ShouldQueue
/** /**
* Create a new job instance. * 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. * Execute the job.
*/ */
public function handle(): void 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 { try {
$class = new $bot->class($bot->config ?? []);
$class->run(); $class->run();
// Update the log entry on success // Update the log entry on success
$this->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
$this->log->update([ $log->update([
'finished_at' => now(), 'finished_at' => now(),
'status' => 'failed', 'status' => 'failed',
'error' => $e->getMessage(), 'error' => $e->getMessage(),

View File

@@ -4,6 +4,7 @@ namespace App\Livewire;
use App\Models\BotLog; use App\Models\BotLog;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Jantinnerezo\LivewireAlert\Facades\LivewireAlert;
use Livewire\Component; use Livewire\Component;
use Livewire\WithoutUrlPagination; use Livewire\WithoutUrlPagination;
use Livewire\WithPagination; use Livewire\WithPagination;
@@ -13,15 +14,18 @@ class BotLogs extends Component
use WithPagination; use WithPagination;
use WithoutUrlPagination; use WithoutUrlPagination;
// public function mount() public function showError(int $logId): void
// { {
// $this->getLogs(); $log = BotLog::find($logId);
// } if ($log) {
LivewireAlert::title('Error Details')
// public function getLogs(): void ->html("<pre class='whitespace-pre-wrap break-words'>{$log->error}</pre>")
// { ->warning()
// $this->logs = Auth::user()->bots->pluck('logs')->flatten()->sortByDesc('created_at'); ->timer(null)
// } ->withCancelButton('Close')
->show();
}
}
public function render() public function render()
{ {

View File

@@ -78,7 +78,7 @@ class BotsList extends Component
]); ]);
// Dispatch the job to run the bot // 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."); flash()->success("Bot '{$bot->name}' is being executed.");
} }

View File

@@ -26,7 +26,7 @@ class ViewBot extends Component
]); ]);
// Dispatch the job to run the bot // 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."); flash()->success("Bot '{$this->bot->name}' is being executed.");
} }

View File

@@ -26,7 +26,7 @@ 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, $log); dispatch(RunBot::class, $bot->id, $log->id);
$log->update([ $log->update([
'started_at' => now(), 'started_at' => now(),
'status' => 'running' 'status' => 'running'

View File

@@ -16,19 +16,26 @@
</td> </td>
<td <td
@class([ @class([
'text-red-400' => $log->status === 'failed', "text-red-400" => $log->status === "failed",
'text-green-400' => $log->status === 'success', "text-green-400" => $log->status === "success",
'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")
<i class="fas fa-exclamation-circle ml-1"></i>
@endif
</td> </td>
<td class="px-4 py-2"> <td class="px-4 py-2">
{{ $log->started_at ?? '' }} {{ $log->started_at ?? "" }}
</td> </td>
<td class="px-4 py-2"> <td class="px-4 py-2">
{{ $log->finished_at ?? '' }} {{ $log->finished_at ?? "" }}
</td> </td>
</tr> </tr>
@endforeach @endforeach