Files
Scheduler/app/Jobs/RunBot.php
Oskar-Mikael 1565df568d
All checks were successful
Deploy App / deploy (push) Successful in 11s
Fix job dispatch and show log output
2025-09-04 17:38:30 +02:00

52 lines
1.2 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\Bot;
use App\Models\BotLog;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
class RunBot implements ShouldQueue
{
use Queueable;
/**
* Create a new job instance.
*/
public function __construct(private int $bot_id, private int $log_id) {}
/**
* Execute the job.
*/
public function handle(): void
{
$bot = Bot::findOrFail($this->bot_id);
$log = BotLog::findOrFail($this->log_id);
$log->update([
'status' => 'running',
]);
try {
$class = new $bot->class($bot->config ?? []);
$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),
]);
} catch (\Throwable $e) {
// Log the error in the bot log
$log->update([
'finished_at' => now(),
'status' => 'failed',
'error' => $e->getMessage(),
]);
}
}
}