admin/app/process/CronKeeper.php

48 lines
924 B
PHP
Raw Permalink 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
namespace app\process;
use app\framework\Log\SimpleLog;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Liebig\Cron\Cron;
use function foo\func;
class CronKeeper
{
use Fork;
public function main()
{
// app()->share(function ($app) {
// return new Cron;
// });
app()->singleton('cron',function () {
return new Cron;
});
}
public function run()
{
//改为redis锁防止集群极端并发
if (!Redis::setnx('CronRunning', gethostname() . '[' . getmypid() . ']')) {
// 60秒内运行过了
// 验证CronRunning的过期时间
$ttl = Redis::TTL('CronRunning');
if ($ttl == -1 || $ttl > 59) {
Redis::del('CronRunning');
}
return true;
}
Redis::expire('CronRunning', 59);
$this->cProcess(function (){
\Artisan::call("cron:run");
},'cron');
}
}