34 lines
843 B
PHP
34 lines
843 B
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Bot;
|
|
use App\Bots\BotContract;
|
|
use Cron\CronExpression;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class BotService
|
|
{
|
|
public function run(): void
|
|
{
|
|
$bots = Bot::where('enabled', 1)->get();
|
|
|
|
foreach ($bots as $bot) {
|
|
$cron = new CronExpression($bot->schedule);
|
|
if ($cron->isDue()) {
|
|
try {
|
|
$instance = app($bot->class, ['config' => $bot->config]);
|
|
|
|
if ($instance instanceof BotContract) {
|
|
$instance->run();
|
|
}
|
|
} catch (\Throwable $e) {
|
|
Log::error("Bot [{$bot->name}] failed: " . $e->getMessage());
|
|
}
|
|
|
|
Log::info("Bot [{$bot->name}] executed successfully.");
|
|
}
|
|
}
|
|
}
|
|
}
|