mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-01 15:32:48 +08:00
新增定时任务功能;
This commit is contained in:
@@ -42,6 +42,13 @@
|
||||
<tip>填写版权信息。</tip>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">站点域名</label>
|
||||
<div class="layui-input-block">
|
||||
<input type="text" name="site_domain" class="layui-input" lay-verify="required" placeholder="请输入站点名称" value="{:sysconfig('site','site_domain')}">
|
||||
<tip>填写站点域名。以http://或https://开头,内置的定时任务用到了这个配置项,修改之后需要重启定时任务</tip>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="hr-line"></div>
|
||||
<div class="layui-form-item text-center">
|
||||
|
||||
115
app/common/command/Timer.php
Normal file
115
app/common/command/Timer.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\command;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Promise\Utils;
|
||||
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\Cache;
|
||||
use think\facade\Log;
|
||||
|
||||
class Timer extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
// 指令配置
|
||||
$this->setName('timer')
|
||||
->setDescription('内置秒级定时器');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
// 指令输出
|
||||
$output->writeln('start timer');
|
||||
|
||||
$client = new Client([
|
||||
'base_uri' => sysconfig('site', 'site_domain'),
|
||||
'verify' => false,
|
||||
]);
|
||||
|
||||
while (true) {
|
||||
|
||||
try {
|
||||
|
||||
|
||||
$config_list = include __DIR__ . '/timer/config.php';
|
||||
|
||||
$list_promises = [];
|
||||
foreach ($config_list as $config_item) {
|
||||
|
||||
$config_item = static::initConfigItem($config_item);
|
||||
|
||||
|
||||
$name = $config_item['name'];
|
||||
|
||||
$cache_key = 'timer_' . $name;
|
||||
$cache_tag = 'system_timer';
|
||||
|
||||
$last_exec_time = Cache::get($cache_key, 0);
|
||||
|
||||
if ($last_exec_time >= time() - $config_item['frequency']) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Cache::tag($cache_tag)->set($cache_key, time());
|
||||
|
||||
$type = $config_item['type'];
|
||||
|
||||
switch ($type) {
|
||||
case 'site':
|
||||
$output->writeln(date('Y-m-d H:i:s') . ': build site request async:' . $config_item['target']);
|
||||
$list_promises[$config_item['name']] = $client->getAsync($config_item['target']);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
$output->writeln(date('Y-m-d H:i:s') . 'unsupport type:' . $type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($list_promises)){
|
||||
|
||||
$output->writeln(date('Y-m-d H:i:s') . ' no request');
|
||||
}else{
|
||||
$results = Utils::unwrap($list_promises);
|
||||
$output->writeln(date('Y-m-d H:i:s') . ': request all finished');
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
// throw $th;
|
||||
$output->writeln('error:' . $th->getMessage());
|
||||
Log::error($th->getMessage());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
private static function initConfigItem($config)
|
||||
{
|
||||
$default = [
|
||||
'name' => 'http_demo',
|
||||
'type' => 'site',
|
||||
'target' => '',
|
||||
'frequency' => 600
|
||||
];
|
||||
|
||||
$data = array_merge($default, $config);
|
||||
|
||||
if ($data['frequency'] < 1) {
|
||||
$data['frequency'] = 1;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ class ResetPassword extends Command
|
||||
{
|
||||
// 指令配置
|
||||
$this->setName('admin:resetPassword')
|
||||
->addOption('password','p', Option::VALUE_OPTIONAL)
|
||||
->setDescription('重置超管密码');
|
||||
}
|
||||
|
||||
@@ -33,7 +34,12 @@ class ResetPassword extends Command
|
||||
return false;
|
||||
}
|
||||
|
||||
$password = uniqid();
|
||||
$password = $input->getOption('password');
|
||||
|
||||
if(is_null($password)){
|
||||
$password = uniqid();
|
||||
}
|
||||
|
||||
|
||||
$model_admin->save([
|
||||
'password' => password($password)
|
||||
|
||||
10
app/common/command/timer/config.php
Normal file
10
app/common/command/timer/config.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
[
|
||||
'name'=>'http_demo', // 定时任务的名称,不能重複
|
||||
'type'=>'site', // 定时任务的类型,默认只支持site,你也可以重写定时器命令行以支持其他命令
|
||||
'target'=>'/tools/timer.ResetPassword/do', // 要访问的地址,如果不是以https开头,那么以后台的系统配置中读取相关配置,如果没有配置则不执行
|
||||
'frequency'=>600 // 执行频率,单位:秒,填写10,则每10秒过后执行一次
|
||||
]
|
||||
];
|
||||
11
app/common/controller/ToolsController.php
Normal file
11
app/common/controller/ToolsController.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\controller;
|
||||
|
||||
use app\BaseController;
|
||||
use app\common\traits\JumpTrait;
|
||||
|
||||
class ToolsController extends BaseController
|
||||
{
|
||||
use JumpTrait;
|
||||
}
|
||||
20
app/tools/controller/timer/ResetPassword.php
Normal file
20
app/tools/controller/timer/ResetPassword.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\tools\controller\timer;
|
||||
|
||||
use app\common\controller\ToolsController;
|
||||
use think\facade\Console;
|
||||
|
||||
class ResetPassword extends ToolsController
|
||||
{
|
||||
public function do()
|
||||
{
|
||||
$output = Console::call('admin:resetPassword', [
|
||||
'--password=123456'
|
||||
]);
|
||||
|
||||
return $output->fetch();
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,8 @@
|
||||
"jianyan74/php-excel": "^1.0",
|
||||
"zhongshaofa/easy-admin": "^1.0.2",
|
||||
"ext-json": "*",
|
||||
"zhongshaofa/thinkphp-log-trace": "^1.0"
|
||||
"zhongshaofa/thinkphp-log-trace": "^1.0",
|
||||
"guzzlehttp/guzzle": "^7.4"
|
||||
},
|
||||
"require-dev": {
|
||||
"symfony/var-dumper": "^4.2",
|
||||
|
||||
5
composer.lock
generated
5
composer.lock
generated
@@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "6f1ec526803f2ecbe4f1486314294fdd",
|
||||
"content-hash": "fdb5d3e361e1b400984f4ff47a15c98e",
|
||||
"packages": [
|
||||
{
|
||||
"name": "adbario/php-dot-notation",
|
||||
@@ -3380,7 +3380,6 @@
|
||||
"require": {
|
||||
"php": ">=7.0"
|
||||
},
|
||||
"default-branch": true,
|
||||
"type": "library",
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
@@ -3601,5 +3600,5 @@
|
||||
"ext-json": "*"
|
||||
},
|
||||
"platform-dev": [],
|
||||
"plugin-api-version": "2.2.0"
|
||||
"plugin-api-version": "1.1.0"
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
use app\common\command\admin\ResetPassword;
|
||||
use app\common\command\Install;
|
||||
use app\common\command\Timer;
|
||||
|
||||
return [
|
||||
// 指令定义
|
||||
@@ -13,6 +14,7 @@ return [
|
||||
'node' => 'app\common\command\Node',
|
||||
'OssStatic' => 'app\common\command\OssStatic',
|
||||
ResetPassword::class,
|
||||
Install::class
|
||||
Install::class,
|
||||
Timer::class,
|
||||
],
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user