Initial work
This commit is contained in:
33
app/Services/BotService.php
Normal file
33
app/Services/BotService.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user