整合计划任务,新增访问URL方式的计划任务
This commit is contained in:
@@ -12,7 +12,9 @@ use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\facade\Db;
|
||||
use think\facade\Config;
|
||||
use app\service\OptimizeService;
|
||||
use app\service\CertTaskService;
|
||||
use app\service\ExpireNoticeService;
|
||||
|
||||
class Certtask extends Command
|
||||
{
|
||||
@@ -20,7 +22,7 @@ class Certtask extends Command
|
||||
{
|
||||
// 指令配置
|
||||
$this->setName('certtask')
|
||||
->setDescription('证书申请与部署任务');
|
||||
->setDescription('SSL证书续签与部署、域名到期提醒、CF优选IP更新');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
@@ -28,6 +30,11 @@ class Certtask extends Command
|
||||
$res = Db::name('config')->cache('configs', 0)->column('value', 'key');
|
||||
Config::set($res, 'sys');
|
||||
|
||||
(new CertTaskService())->execute();
|
||||
$res = (new OptimizeService())->execute();
|
||||
if (!$res) {
|
||||
(new CertTaskService())->execute();
|
||||
(new ExpireNoticeService())->task();
|
||||
}
|
||||
echo 'done'.PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\command;
|
||||
|
||||
use Exception;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Argument;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\facade\Db;
|
||||
use think\facade\Config;
|
||||
use app\service\OptimizeService;
|
||||
|
||||
class Opiptask extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
// 指令配置
|
||||
$this->setName('opiptask')
|
||||
->setDescription('CF优选IP任务');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$res = Db::name('config')->cache('configs', 0)->column('value', 'key');
|
||||
Config::set($res, 'sys');
|
||||
|
||||
(new OptimizeService())->execute();
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,9 @@ class Optimizeip extends BaseController
|
||||
if (empty($key)) {
|
||||
continue;
|
||||
}
|
||||
if ($key == 'optimize_ip_min' && intval($value) < 10) {
|
||||
return json(['code' => -1, 'msg' => '自动更新时间间隔不能小于10分钟']);
|
||||
}
|
||||
config_set($key, $value);
|
||||
Cache::delete('configs');
|
||||
}
|
||||
|
||||
@@ -7,6 +7,9 @@ use Exception;
|
||||
use think\facade\Db;
|
||||
use think\facade\View;
|
||||
use think\facade\Cache;
|
||||
use app\service\OptimizeService;
|
||||
use app\service\CertTaskService;
|
||||
use app\service\ExpireNoticeService;
|
||||
|
||||
class System extends BaseController
|
||||
{
|
||||
@@ -107,4 +110,38 @@ class System extends BaseController
|
||||
}
|
||||
return json(['code' => 0]);
|
||||
}
|
||||
|
||||
public function cronset()
|
||||
{
|
||||
if (!checkPermission(2)) return $this->alert('error', '无权限');
|
||||
if (config_get('cron_key') === null) {
|
||||
config_set('cron_key', random(10));
|
||||
Cache::delete('configs');
|
||||
}
|
||||
View::assign('is_user_www', isset($_SERVER['USER']) && $_SERVER['USER'] == 'www');
|
||||
View::assign('siteurl', request()->root(true));
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
public function cron()
|
||||
{
|
||||
if (function_exists("set_time_limit")) {
|
||||
@set_time_limit(0);
|
||||
}
|
||||
if (function_exists("ignore_user_abort")) {
|
||||
@ignore_user_abort(true);
|
||||
}
|
||||
if (isset($_SERVER['HTTP_USER_AGENT']) && str_contains($_SERVER['HTTP_USER_AGENT'], 'Baiduspider')) exit;
|
||||
$key = input('get.key', '');
|
||||
$cron_key = config_get('cron_key');
|
||||
if (config_get('cron_type', '0') != '1' || empty($cron_key)) exit('未开启当前方式');
|
||||
if ($key != $cron_key) exit('访问密钥错误');
|
||||
|
||||
$res = (new OptimizeService())->execute();
|
||||
if (!$res) {
|
||||
(new CertTaskService())->execute();
|
||||
(new ExpireNoticeService())->task();
|
||||
}
|
||||
echo 'success!';
|
||||
}
|
||||
}
|
||||
@@ -11,15 +11,17 @@ class CertTaskService
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$this->execute_deploy();
|
||||
$this->execute_order();
|
||||
(new ExpireNoticeService())->task();
|
||||
config_set('certtask_time', date("Y-m-d H:i:s"));
|
||||
echo 'done'.PHP_EOL;
|
||||
if ($this->execute_deploy()) {
|
||||
config_set('certdeploy_time', date("Y-m-d H:i:s"));
|
||||
}
|
||||
if ($this->execute_order()) {
|
||||
config_set('certtask_time', date("Y-m-d H:i:s"));
|
||||
}
|
||||
}
|
||||
|
||||
private function execute_order()
|
||||
{
|
||||
echo '开始执行SSL证书签发任务...'.PHP_EOL;
|
||||
$days = config_get('cert_renewdays', 7);
|
||||
$list = Db::name('cert_order')->field('id,aid,status,issend')->whereRaw('status NOT IN (3,4) AND (retrytime IS NULL OR retrytime<NOW()) OR status=3 AND isauto=1 AND expiretime<:expiretime', ['expiretime' => date('Y-m-d H:i:s', time() + $days * 86400)])->select();
|
||||
//print_r($list);exit;
|
||||
@@ -55,6 +57,7 @@ class CertTaskService
|
||||
if ($failcount >= 3) break;
|
||||
sleep(1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private function execute_deploy()
|
||||
@@ -64,14 +67,15 @@ class CertTaskService
|
||||
$hour = date('H');
|
||||
if($start <= $end){
|
||||
if($hour < $start || $hour > $end){
|
||||
echo '不在部署任务运行时间范围内'.PHP_EOL; return;
|
||||
echo '不在部署任务运行时间范围内'.PHP_EOL; return false;
|
||||
}
|
||||
}else{
|
||||
if($hour < $start && $hour > $end){
|
||||
echo '不在部署任务运行时间范围内'.PHP_EOL; return;
|
||||
echo '不在部署任务运行时间范围内'.PHP_EOL; return false;
|
||||
}
|
||||
}
|
||||
|
||||
echo '开始执行SSL证书部署任务...'.PHP_EOL;
|
||||
$list = Db::name('cert_deploy')->field('id,status,issend')->whereRaw('active=1 AND status IN (0,-1) AND (retrytime IS NULL OR retrytime<NOW())')->select();
|
||||
//print_r($list);exit;
|
||||
$count = 0;
|
||||
@@ -95,5 +99,6 @@ class CertTaskService
|
||||
if ($count >= 3) break;
|
||||
sleep(1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,8 @@ class ExpireNoticeService
|
||||
|
||||
public function task()
|
||||
{
|
||||
echo '开始执行域名到期提醒任务...' . PHP_EOL;
|
||||
config_set('domain_expire_time', date("Y-m-d H:i:s"));
|
||||
$count = $this->refreshDomainList();
|
||||
if ($count > 0) return;
|
||||
|
||||
|
||||
@@ -96,7 +96,15 @@ class OptimizeService
|
||||
//批量执行优选任务
|
||||
public function execute()
|
||||
{
|
||||
$minute = config_get('optimize_ip_min', '30');
|
||||
$last = config_get('optimize_ip_time', null, true);
|
||||
if ($last && strtotime($last) > time() - $minute * 60) {
|
||||
return false;
|
||||
}
|
||||
$list = Db::name('optimizeip')->where('active', 1)->select();
|
||||
if (empty($list)) {
|
||||
return false;
|
||||
}
|
||||
echo '开始执行IP优选任务,共获取到'.count($list).'个待执行任务'."\n";
|
||||
foreach ($list as $row) {
|
||||
try {
|
||||
@@ -108,6 +116,8 @@ class OptimizeService
|
||||
echo '优选任务'.$row['id'].'执行失败:'.$e->getMessage()."\n";
|
||||
}
|
||||
}
|
||||
config_set('optimize_ip_time', date("Y-m-d H:i:s"));
|
||||
return true;
|
||||
}
|
||||
|
||||
//执行单个优选任务
|
||||
|
||||
@@ -126,7 +126,11 @@ class CheckUtils
|
||||
return ['status' => false, 'errmsg' => 'Invalid IP address', 'usetime' => 0];
|
||||
}
|
||||
$timeout = 1;
|
||||
exec('ping -c 1 -w '.$timeout.' '.$target, $output, $return_var);
|
||||
if (str_contains($target, ':')) {
|
||||
exec('ping -6 -c 1 -w '.$timeout.' '.$target, $output, $return_var);
|
||||
} else {
|
||||
exec('ping -c 1 -w '.$timeout.' '.$target, $output, $return_var);
|
||||
}
|
||||
if (!empty($output[1])) {
|
||||
if (strpos($output[1], '毫秒') !== false) {
|
||||
$usetime = getSubstr($output[1], '时间=', ' 毫秒');
|
||||
|
||||
@@ -3,14 +3,6 @@
|
||||
{block name="main"}
|
||||
<div class="row">
|
||||
<div class="col-xs-12 col-sm-8 col-lg-6 center-block" style="float: none;">
|
||||
<div class="panel panel-warning">
|
||||
<div class="panel-heading"><h3 class="panel-title">计划任务说明</h3></div>
|
||||
<div class="panel-body">
|
||||
<p><li>计划任务:将以下命令添加到计划任务,1分钟1次</li></p>
|
||||
<p><code>cd {:app()->getRootPath()} && php think certtask</code></p>
|
||||
<p><li>上次运行时间:<font color="green">{:config_get('certtask_time', '未运行', true)}</font></li></p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel panel-info">
|
||||
<div class="panel-heading"><h3 class="panel-title">自动续签设置</h3></div>
|
||||
@@ -28,7 +20,7 @@
|
||||
</form>
|
||||
</div>
|
||||
<div class="panel-footer">
|
||||
<li>提示:只有已开启自动续签的证书,才会自动续签。</li>
|
||||
<li>提示:只有已开启自动续签的证书,并添加<a href="/system/cronset">计划任务</a>,才会自动续签。</li>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -153,10 +153,10 @@
|
||||
<li class="{:checkIfActive('deployaccount')}"><a href="/cert/deployaccount"><i class="fa fa-circle-o"></i> 自动部署账户</a></li>
|
||||
<li class="{:checkIfActive('deploytask,deploy_form')}"><a href="/cert/deploytask"><i class="fa fa-circle-o"></i> 自动部署任务</a></li>
|
||||
<li class="{:checkIfActive('cname')}"><a href="/cert/cname"><i class="fa fa-circle-o"></i> CNAME代理</a></li>
|
||||
<li class="{:checkIfActive('certset')}"><a href="/cert/certset"><i class="fa fa-circle-o"></i> 计划任务设置</a></li>
|
||||
<li class="{:checkIfActive('certset')}"><a href="/cert/certset"><i class="fa fa-circle-o"></i> 自动续签设置</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="treeview {:checkIfActive('loginset,noticeset,proxyset')}">
|
||||
<li class="treeview {:checkIfActive('cronset,loginset,noticeset,proxyset')}">
|
||||
<a href="javascript:;">
|
||||
<i class="fa fa-cogs fa-fw"></i>
|
||||
<span>系统设置</span>
|
||||
@@ -165,6 +165,7 @@
|
||||
</span>
|
||||
</a>
|
||||
<ul class="treeview-menu">
|
||||
<li class="{:checkIfActive('cronset')}"><a href="/system/cronset"><i class="fa fa-circle-o"></i> 计划任务</a></li>
|
||||
<li class="{:checkIfActive('loginset')}"><a href="/system/loginset"><i class="fa fa-circle-o"></i> 登录设置</a></li>
|
||||
<li class="{:checkIfActive('noticeset')}"><a href="/system/noticeset"><i class="fa fa-circle-o"></i> 通知设置</a></li>
|
||||
<li class="{:checkIfActive('proxyset')}"><a href="/system/proxyset"><i class="fa fa-circle-o"></i> 代理设置</a></li>
|
||||
|
||||
@@ -160,7 +160,7 @@
|
||||
<p>1、php需要安装swoole组件</p>
|
||||
<p>2、在命令行执行以下命令启动进程:</p>
|
||||
<p><code>cd {:app()->getRootPath()} && php think dmtask</code></p>
|
||||
<p>3、也可以使用进程守护管理器,添加守护进程,运行目录:{:app()->getRootPath()},启动命令:php think dmtask</p>
|
||||
<p>3、也可以使用进程守护管理器,添加守护进程。<br/>运行目录:<code>{:app()->getRootPath()}</code><br/>启动命令:<code>php think dmtask</code></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -35,12 +35,8 @@
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel panel-warning">
|
||||
<div class="panel-heading"><h3 class="panel-title">计划任务说明</h3></div>
|
||||
<div class="panel-body">
|
||||
<p>支持域名到期提醒+域名列表到期时间自动刷新。与SSL证书共用计划任务,不需要单独添加计划任务。</p><p><a href="/cert/certset">查看计划任务说明</a></p>
|
||||
<div class="panel-footer">
|
||||
<p>需添加<a href="/system/cronset">计划任务</a>,支持域名到期提醒+域名列表到期时间自动刷新。</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -16,8 +16,7 @@
|
||||
<p><li>不支持对CloudFlare里的域名添加优选,必须使用其他DNS服务商。需开通Cloudflare for SaaS,且域名使用CNAME的方式解析到CloudFlare。</li></p>
|
||||
<p><li>数据接口:<a href="https://www.wetest.vip/" target="_blank" rel="noreferrer">wetest.vip</a> 数据接口支持CloudFlare、CloudFront、EdgeOne;<a href="https://stock.hostmonit.com/" target="_blank" rel="noreferrer">HostMonit</a> 只支持CloudFlare。</li></p>
|
||||
<p><li>接口密钥:默认o1zrmHAF为免费KEY可永久免费使用。</li></p>
|
||||
<p><li>计划任务:将以下命令添加到计划任务,周期设置为15分钟以上</li></p>
|
||||
<p><code>cd {:app()->getRootPath()} && php think opiptask</code></p>
|
||||
<p><li>自动更新:可查看<a href="/system/cronset">计划任务设置</a></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -43,6 +42,22 @@
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div class="panel panel-info">
|
||||
<div class="panel-heading"><h3 class="panel-title">自动更新设置</h3></div>
|
||||
<div class="panel-body">
|
||||
<form onsubmit="return saveSetting(this)" method="post" class="form-horizontal" role="form">
|
||||
<div class="form-group">
|
||||
<label class="col-sm-3 control-label">自动更新时间间隔(分钟)</label>
|
||||
<div class="col-sm-9"><input type="text" name="optimize_ip_min" value="{:config_get('optimize_ip_min', '30')}" class="form-control" placeholder="单位:分钟"/></div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div class="col-sm-offset-3 col-sm-9">
|
||||
<input type="submit" name="submit" value="保存" class="btn btn-primary btn-block"/>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
126
app/view/system/cronset.html
Normal file
126
app/view/system/cronset.html
Normal file
@@ -0,0 +1,126 @@
|
||||
{extend name="common/layout" /}
|
||||
{block name="title"}计划任务{/block}
|
||||
{block name="main"}
|
||||
<div class="row">
|
||||
<div class="col-xs-12 col-sm-8 col-lg-6 center-block" style="float: none;">
|
||||
<div class="panel panel-info">
|
||||
<div class="panel-heading"><h3 class="panel-title">计划任务说明</h3></div>
|
||||
<div class="panel-body">
|
||||
{if config_get('cron_type', '0') == '1'}
|
||||
<p><li>需定时访问以下URL,频率;1分钟1次</li></p>
|
||||
<p><code>{$siteurl}/cron?key={:config_get('cron_key')}</code></p>
|
||||
{else}
|
||||
<p><li>将以下Shell命令添加到计划任务,频率;1分钟1次</li></p>
|
||||
<p><code>cd {:app()->getRootPath()} && php think certtask</code></p>
|
||||
{if $is_user_www}<p><li><b>注:计划任务执行用户必须选择www用户</b></li></p>{/if}
|
||||
<p><li>采用Docker镜像部署的会自动添加计划任务,无需手动添加。</li></p>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel panel-intro">
|
||||
<div class="panel-heading"><h3 class="panel-title">计划任务设置</h3></div>
|
||||
<div class="panel-body">
|
||||
<form onsubmit="return saveSetting(this)" method="post" class="form-horizontal" role="form">
|
||||
<div class="form-group">
|
||||
<label class="col-sm-3 control-label">计划任务执行方式</label>
|
||||
<div class="col-sm-9"><select class="form-control" name="cron_type" default="{:config_get('cron_type', '0')}"><option value="0">Shell命令(推荐)</option><option value="1">访问URL</option></select></div>
|
||||
</div>
|
||||
<div class="form-group" id="cron_url" {:config_get('cron_type', '0') == 0 ? 'style="display: none"' : ''}>
|
||||
<label class="col-sm-3 control-label">访问密钥</label>
|
||||
<div class="col-sm-9"><input type="text" name="cron_key" value="{:config_get('cron_key')}" class="form-control" requ/></div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div class="col-sm-offset-3 col-sm-9">
|
||||
<input type="submit" name="submit" value="保存" class="btn btn-primary btn-block"/>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="panel-footer">
|
||||
<p>优先推荐使用Shell命令方式执行计划任务,访问URL方式可能会请求超时导致执行失败。</p><p>如果是虚拟主机环境无法执行命令,则可以使用访问URL方式。</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel panel-success mt-3">
|
||||
<div class="panel-heading"><h3 class="panel-title">计划任务运行状态</h3></div>
|
||||
<table class="table table-bordered table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>任务名称</th>
|
||||
<th>上次运行时间</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>SSL证书续签</td>
|
||||
<td><font color="green">{:config_get('certtask_time', '未运行', true)}</font></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>SSL证书部署</td>
|
||||
<td><font color="green">{:config_get('certdeploy_time', '未运行', true)}</font></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>域名到期提醒</td>
|
||||
<td><font color="green">{:config_get('domain_expire_time', '未运行', true)}</font></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>CF优选IP更新</td>
|
||||
<td><font color="green">{:config_get('optimize_ip_time', '未运行', true)}</font></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
{block name="script"}
|
||||
<script src="{$cdnpublic}layer/3.1.1/layer.js"></script>
|
||||
<script>
|
||||
var items = $("select[default]");
|
||||
for (i = 0; i < items.length; i++) {
|
||||
$(items[i]).val($(items[i]).attr("default")||0);
|
||||
}
|
||||
function saveSetting(obj){
|
||||
var cron_type = $("select[name='cron_type']").val();
|
||||
var cron_key = $("input[name='cron_key']").val();
|
||||
if(cron_type == 1 && cron_key == ''){
|
||||
layer.alert('访问密钥不能为空!', {icon: 2});
|
||||
return false;
|
||||
}
|
||||
var ii = layer.load(2, {shade:[0.1,'#fff']});
|
||||
$.ajax({
|
||||
type : 'POST',
|
||||
url : '/system/set',
|
||||
data : {cron_type:cron_type, cron_key:cron_key},
|
||||
dataType : 'json',
|
||||
success : function(data) {
|
||||
layer.close(ii);
|
||||
if(data.code == 0){
|
||||
layer.alert('设置保存成功!', {
|
||||
icon: 1,
|
||||
closeBtn: false
|
||||
}, function(){
|
||||
window.location.reload()
|
||||
});
|
||||
}else{
|
||||
layer.alert(data.msg, {icon: 2})
|
||||
}
|
||||
},
|
||||
error:function(data){
|
||||
layer.close(ii);
|
||||
layer.msg('服务器错误');
|
||||
}
|
||||
});
|
||||
return false;
|
||||
}
|
||||
$("select[name='cron_type']").change(function(){
|
||||
if($(this).val() == 0){
|
||||
$("#cron_url").hide();
|
||||
}else{
|
||||
$("#cron_url").show();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
{/block}
|
||||
@@ -31,7 +31,7 @@ return [
|
||||
'show_error_msg' => true,
|
||||
'exception_tmpl' => \think\facade\App::getAppPath() . 'view/exception.tpl',
|
||||
|
||||
'version' => '1038',
|
||||
'version' => '1039',
|
||||
|
||||
'dbversion' => '1033'
|
||||
];
|
||||
|
||||
@@ -6,7 +6,6 @@ return [
|
||||
// 指令定义
|
||||
'commands' => [
|
||||
'dmtask' => 'app\command\Dmtask',
|
||||
'opiptask' => 'app\command\Opiptask',
|
||||
'certtask' => 'app\command\Certtask',
|
||||
'reset' => 'app\command\Reset',
|
||||
],
|
||||
|
||||
@@ -29,6 +29,7 @@ Route::get('/logout', 'auth/logout');
|
||||
Route::any('/quicklogin', 'auth/quicklogin');
|
||||
Route::any('/dmtask/status', 'dmonitor/status');
|
||||
Route::any('/optimizeip/status', 'optimizeip/status');
|
||||
Route::get('/cron', 'system/cron');
|
||||
|
||||
Route::group(function () {
|
||||
Route::any('/', 'index/index');
|
||||
@@ -120,6 +121,7 @@ Route::group(function () {
|
||||
Route::get('/system/tgbottest', 'system/tgbottest');
|
||||
Route::get('/system/webhooktest', 'system/webhooktest');
|
||||
Route::post('/system/proxytest', 'system/proxytest');
|
||||
Route::get('/system/cronset', 'system/cronset');
|
||||
|
||||
})->middleware(CheckLogin::class)
|
||||
->middleware(ViewOutput::class);
|
||||
|
||||
Reference in New Issue
Block a user