爬虫开发
Some checks failed
Tests / PHP 8.2 (push) Has been cancelled
Tests / PHP 8.3 (push) Has been cancelled
Tests / PHP 8.4 (push) Has been cancelled

This commit is contained in:
cjd
2026-02-18 12:56:36 +08:00
parent a98bc6f13c
commit 260460df03
45 changed files with 4091 additions and 8 deletions

View File

@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Services\Crawler;
use App\Models\CrawlRule;
use Carbon\CarbonImmutable;
use Cron\CronExpression;
class CrawlRuleScheduleService
{
public function isDue(CrawlRule $rule, ?CarbonImmutable $now = null): bool
{
if (! $rule->enabled) {
return false;
}
$now ??= CarbonImmutable::now($rule->timezone ?: 'Asia/Shanghai');
try {
$cron = new CronExpression($rule->cron_expression);
} catch (\Throwable) {
return false;
}
return $cron->isDue($now);
}
public function nextRunAt(CrawlRule $rule, ?CarbonImmutable $from = null): ?CarbonImmutable
{
$from ??= CarbonImmutable::now($rule->timezone ?: 'Asia/Shanghai');
try {
$cron = new CronExpression($rule->cron_expression);
$next = CarbonImmutable::instance($cron->getNextRunDate($from));
} catch (\Throwable) {
return null;
}
return $next->setTimezone('UTC');
}
}