dd/app/Services/Cron.php
2026-05-15 14:43:44 +08:00

127 lines
4.0 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace App\Services;
use App\Services\Cron\HttpQueue as CronHttpQueue;
use App\Services\Cron\Book as CronBook;
use App\Services\Cron\FreenodeCrawl as CronFreenodeCrawl;
use App\Services\Cron\FreenodePool as CronFreenodePool;
use App\Services\Cron\FreenodeFile as CronFreenodeFile;
use App\Services\Cron\FreenodeMerge as CronFreenodeMerge;
use App\Services\Cron\FreenodeLog as CronFreenodeLog;
use App\Services\Cron\FreenodeSync as CronFreenodeSync;
use App\Services\Cron\ArticleSchema as CronArticleSchema;
use App\Services\Cron\FnTgPublish as CronFnTgPublish;
use App\Services\TomTool\Telegram\Slave as TeleSlave;
use App\Services\CacheTg as ServiceCacheTg;
use App\Services\Cron\FnSitePublish as CronFnSitePublish;
final class Cron
{
public bool $bIsTest = false;
public ?int $iTestH = null;
public ?int $iTestI = null;
public ?int $iTestS = null;
public function run(): void
{
try {
$h = (int) date("H");
$i = (int) date("i");
$s = (int) date("s");
if ($this->bIsTest) {
$h = (int) ($this->iTestH ?? $h);
$i = (int) ($this->iTestI ?? $i);
$s = (int) ($this->iTestS ?? $s);
}
echo "cron开始执行 \n";
if ($h % 8 === 0 && $i === 3) {
CronBook::randbox_notify(); // note提醒
}
if ($h % 6 === 0 && $i === 3) {
CronFreenodeCrawl::run(); // 1.爬免费节点
CronFreenodePool::save(); // 2.格式化入库
CronFreenodeMerge::save(); // 3.加工成文件
CronFreenodeMerge::baseToSite(); // 4.文件分散为各站用
}
if ($h % 3 === 0 && $i === 3) {
CronFreenodeSync::nodehub(); // fn分到到nodehub
CronFreenodeSync::run(); // fn分发到各到各站旧版
}
if ($h == 11 && $i == 3) {
CronFnSitePublish::fn_b(); // 新版直接生成完整fn文章推到各站
}
if ($h == 5 && $i === 3) {
CronFreenodeFile::clear(); // 清理fn
}
if ($h == 13 && $i == 3) { // 中午12点03发布fn的tg
CronFnTgPublish::run(); // fn发布tg频道
CronFnTgPublish::run_b(); // 新版fn发布tg频道
}
if ($i % 3 === 0) {
ServiceCacheTg::cron_saveToSchema(); // 文章从tg接收
CronArticleSchema::cacheTgSave(); // 文章入库
CronArticleSchema::pushToSlave(); // 文章发送到各站
CronHttpQueue::pop();
}
} catch (\Throwable $e) {
$sMsg = "";
$sMsg .= "--- cron 异常 ---\n";
$sMsg .= "类型: " . get_class($e) . "\n";
$sMsg .= "信息: " . $e->getMessage() . "\n";
$sMsg .= "文件: " . $e->getFile() . " 在第 " . $e->getLine() . "\n";
$sMsg .= "代码跟踪:\n" . $e->getTraceAsString() . "\n";
$sMsg .= "-----------------------------\n";
echo $sMsg;
$sMsg = "";
$sMsg .= "--- cron 异常 ---\n";
$sMsg .= "类型: " . get_class($e) . "\n";
$sMsg .= "信息: " . $e->getMessage() . "\n";
$sMsg .= "文件: " . $e->getFile() . " 在第 " . $e->getLine() . "\n";
TeleSlave::fail()->send($sMsg);
}
}
public function setTestH(int $h): void
{
$this->enableTestMode();
$this->iTestH = $h;
}
public function setTestI(int $i): void
{
$this->enableTestMode();
$this->iTestI = $i;
}
public function setTestS(int $s): void
{
$this->enableTestMode();
$this->iTestS = $s;
}
public function enableTestMode(bool $enable = true): void
{
$this->bIsTest = $enable;
}
}