Fix job dispatch and show log output
All checks were successful
Deploy App / deploy (push) Successful in 11s
All checks were successful
Deploy App / deploy (push) Successful in 11s
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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', [
|
||||
|
||||
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user