diff --git a/app/admin/controller/Queue.php b/app/admin/controller/Queue.php index dae6e5596..dc552e7cf 100644 --- a/app/admin/controller/Queue.php +++ b/app/admin/controller/Queue.php @@ -60,7 +60,6 @@ class Queue extends Controller if ($item['status'] === 3) $this->total['oks'] = $item['count']; if ($item['status'] === 4) $this->total['ers'] = $item['count']; }); - $this->title = '系统任务管理'; $this->iswin = ProcessService::instance()->iswin(); // 任务列表查询分页处理 @@ -116,12 +115,12 @@ class Queue extends Controller */ public function clear() { - $map = [['exec_time', '<', strtotime('-3days')]]; + $map = [['exec_time', '<', strtotime('-7days')]]; $result = $this->app->db->name($this->table)->where($map)->delete(); if ($result !== false) { - $this->success('成功清理3天前的任务记录!'); + $this->success('成功清理7天前的任务记录!'); } else { - $this->error('清理3天前的任务记录失败!'); + $this->error('清理7天前的任务记录失败!'); } } diff --git a/app/admin/view/queue/index.html b/app/admin/view/queue/index.html index 42a4e300b..c76fc4e8d 100644 --- a/app/admin/view/queue/index.html +++ b/app/admin/view/queue/index.html @@ -15,7 +15,7 @@ {/if} {if auth("clear")} -<button data-load='{:url("clear")}' data-confirm="确定要清理3天前的任务记录吗?" class='layui-btn layui-btn-sm layui-btn-primary'>清理记录</button> +<button data-load='{:url("clear")}' data-confirm="确定要清理7天前的任务记录吗?" class='layui-btn layui-btn-sm layui-btn-primary'>清理记录</button> {/if} {/block} @@ -79,23 +79,30 @@ <label><input class="list-check-box" value='{$vo.id}' type='checkbox'></label> </td> <td class='text-left nowrap'> + 任务编号:{$vo.code|default=''}<br> 任务名称:{$vo.title|default=''}<br> - <span class="color-desc">任务指令:{$vo.command|default=''}</span> + 任务指令:{$vo.command|default=''} </td> <td class='text-left nowrap'> 计划时间:{$vo.exec_time|format_datetime} {if isset($vo.exec_pid) and $vo.exec_pid>0}( 进程 <b class="color-blue">{$vo.exec_pid|default='-'}</b> ){/if}<br> {if $vo.status eq 3 or $vo.status eq 4} 执行时间:{$vo.enter_time|format_datetime}( 耗时 <b class="color-blue">{:sprintf("%.4f",$vo.outer_time-$vo.enter_time)}</b> 秒 ) {elseif $vo.status eq 2} 开始时间:{$vo.enter_time|format_datetime} - {else} 创建时间:{$vo.create_at|format_datetime} {/if} + {else}执行时间:<span class="color-desc">任务还没有执行,等待执行...</span> {/if}<br> + 创建时间:{$vo.create_at|format_datetime} {if isset($vo.loops_time) and $vo.loops_time > 0}( 每 <b class="color-blue">{$vo.loops_time|default='0'}</b> 秒执行,共 <b class="color-blue">{$vo.attempts}</b> 次){else}( 共执行 <b class="color-blue">{$vo.attempts}</b> 次 ) + {/if} </td> <td class='text-left nowrap'> - <div> - {eq name='vo.rscript' value='1'} + <div class="margin-bottom-10"> + {if isset($vo.loops_time) and $vo.loops_time > 0} + <span class="layui-badge layui-bg-orange margin-right-5">循</span> + {/if} + + {if $vo.rscript eq 1} <span class="layui-badge layui-bg-green margin-right-5">复</span> {else} <span class="layui-badge layui-bg-blue margin-right-5">单</span> - {/eq} + {/if} {eq name='vo.status' value='1'} <span class="layui-badge layui-bg-black">待处理</span> diff --git a/app/admin/view/queue/index_search.html b/app/admin/view/queue/index_search.html index 680e5bc29..cb60fc1b9 100644 --- a/app/admin/view/queue/index_search.html +++ b/app/admin/view/queue/index_search.html @@ -55,6 +55,12 @@ <input data-date-range name="enter_time" value="{:input('get.enter_time')}" placeholder="请选择开始时间" class="layui-input"> </div> </div> + <div class="layui-form-item layui-inline"> + <label class="layui-form-label">创建时间</label> + <div class="layui-input-inline"> + <input data-date-range name="create_at" value="{:input('get.create_at')}" placeholder="请选择创建时间" class="layui-input"> + </div> + </div> <div class="layui-form-item layui-inline"> <button class="layui-btn layui-btn-primary"><i class="layui-icon"></i> 搜 索</button> </div> diff --git a/composer.lock b/composer.lock index f5a840d40..1281f7093 100644 --- a/composer.lock +++ b/composer.lock @@ -909,12 +909,12 @@ "source": { "type": "git", "url": "https://github.com/zoujingli/ThinkLibrary.git", - "reference": "cd754db0e9921b7aa6cf617887cdc8b122fe8c46" + "reference": "d121a5b29ef178def60e1b72c3b1ad9e0fece7cb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/cd754db0e9921b7aa6cf617887cdc8b122fe8c46", - "reference": "cd754db0e9921b7aa6cf617887cdc8b122fe8c46", + "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/d121a5b29ef178def60e1b72c3b1ad9e0fece7cb", + "reference": "d121a5b29ef178def60e1b72c3b1ad9e0fece7cb", "shasum": "", "mirrors": [ { @@ -958,7 +958,7 @@ ], "description": "ThinkPHP v6.0 Development Library", "homepage": "http://framework.thinkadmin.top", - "time": "2020-01-08T07:48:20+00:00" + "time": "2020-01-09T07:42:54+00:00" }, { "name": "zoujingli/wechat-developer", diff --git a/config/log.php b/config/log.php index 3c43fa37c..7b4d7add3 100644 --- a/config/log.php +++ b/config/log.php @@ -32,9 +32,9 @@ return [ // 日志保存目录 'path' => '', // 单文件日志写入 - 'single' => true, + 'single' => false, // 独立日志级别 - 'apart_level' => [], + 'apart_level' => ['error'], // 最大日志文件数量 'max_files' => 0, // 使用JSON格式记录php diff --git a/vendor/autoload.php b/vendor/autoload.php index 484fcd3f6..35583b22c 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -4,4 +4,4 @@ require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInit2b1316f37dd8fe5c4c25969e0b842e8e::getLoader(); +return ComposerAutoloaderInit9eebf462f38fc6a0db482795c8caf813::getLoader(); diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 9950c50cb..bbd3b8df1 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -259,6 +259,7 @@ return array( 'think\\admin\\helper\\SaveHelper' => $vendorDir . '/zoujingli/think-library/src/helper/SaveHelper.php', 'think\\admin\\helper\\TokenHelper' => $vendorDir . '/zoujingli/think-library/src/helper/TokenHelper.php', 'think\\admin\\helper\\ValidateHelper' => $vendorDir . '/zoujingli/think-library/src/helper/ValidateHelper.php', + 'think\\admin\\queue\\CleanQueue' => $vendorDir . '/zoujingli/think-library/src/queue/CleanQueue.php', 'think\\admin\\queue\\ListenQueue' => $vendorDir . '/zoujingli/think-library/src/queue/ListenQueue.php', 'think\\admin\\queue\\QueryQueue' => $vendorDir . '/zoujingli/think-library/src/queue/QueryQueue.php', 'think\\admin\\queue\\StartQueue' => $vendorDir . '/zoujingli/think-library/src/queue/StartQueue.php', diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 54043742f..210ade57d 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInit2b1316f37dd8fe5c4c25969e0b842e8e +class ComposerAutoloaderInit9eebf462f38fc6a0db482795c8caf813 { private static $loader; @@ -19,15 +19,15 @@ class ComposerAutoloaderInit2b1316f37dd8fe5c4c25969e0b842e8e return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInit2b1316f37dd8fe5c4c25969e0b842e8e', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInit9eebf462f38fc6a0db482795c8caf813', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(); - spl_autoload_unregister(array('ComposerAutoloaderInit2b1316f37dd8fe5c4c25969e0b842e8e', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInit9eebf462f38fc6a0db482795c8caf813', 'loadClassLoader')); $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded()); if ($useStaticLoader) { require_once __DIR__ . '/autoload_static.php'; - call_user_func(\Composer\Autoload\ComposerStaticInit2b1316f37dd8fe5c4c25969e0b842e8e::getInitializer($loader)); + call_user_func(\Composer\Autoload\ComposerStaticInit9eebf462f38fc6a0db482795c8caf813::getInitializer($loader)); } else { $map = require __DIR__ . '/autoload_namespaces.php'; foreach ($map as $namespace => $path) { @@ -48,19 +48,19 @@ class ComposerAutoloaderInit2b1316f37dd8fe5c4c25969e0b842e8e $loader->register(true); if ($useStaticLoader) { - $includeFiles = Composer\Autoload\ComposerStaticInit2b1316f37dd8fe5c4c25969e0b842e8e::$files; + $includeFiles = Composer\Autoload\ComposerStaticInit9eebf462f38fc6a0db482795c8caf813::$files; } else { $includeFiles = require __DIR__ . '/autoload_files.php'; } foreach ($includeFiles as $fileIdentifier => $file) { - composerRequire2b1316f37dd8fe5c4c25969e0b842e8e($fileIdentifier, $file); + composerRequire9eebf462f38fc6a0db482795c8caf813($fileIdentifier, $file); } return $loader; } } -function composerRequire2b1316f37dd8fe5c4c25969e0b842e8e($fileIdentifier, $file) +function composerRequire9eebf462f38fc6a0db482795c8caf813($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { require $file; diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 2f10b1566..9c30bcd2b 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInit2b1316f37dd8fe5c4c25969e0b842e8e +class ComposerStaticInit9eebf462f38fc6a0db482795c8caf813 { public static $files = array ( '9b552a3cc426e3287cc811caefa3cf53' => __DIR__ . '/..' . '/topthink/think-helper/src/helper.php', @@ -392,6 +392,7 @@ class ComposerStaticInit2b1316f37dd8fe5c4c25969e0b842e8e 'think\\admin\\helper\\SaveHelper' => __DIR__ . '/..' . '/zoujingli/think-library/src/helper/SaveHelper.php', 'think\\admin\\helper\\TokenHelper' => __DIR__ . '/..' . '/zoujingli/think-library/src/helper/TokenHelper.php', 'think\\admin\\helper\\ValidateHelper' => __DIR__ . '/..' . '/zoujingli/think-library/src/helper/ValidateHelper.php', + 'think\\admin\\queue\\CleanQueue' => __DIR__ . '/..' . '/zoujingli/think-library/src/queue/CleanQueue.php', 'think\\admin\\queue\\ListenQueue' => __DIR__ . '/..' . '/zoujingli/think-library/src/queue/ListenQueue.php', 'think\\admin\\queue\\QueryQueue' => __DIR__ . '/..' . '/zoujingli/think-library/src/queue/QueryQueue.php', 'think\\admin\\queue\\StartQueue' => __DIR__ . '/..' . '/zoujingli/think-library/src/queue/StartQueue.php', @@ -623,9 +624,9 @@ class ComposerStaticInit2b1316f37dd8fe5c4c25969e0b842e8e public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInit2b1316f37dd8fe5c4c25969e0b842e8e::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInit2b1316f37dd8fe5c4c25969e0b842e8e::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInit2b1316f37dd8fe5c4c25969e0b842e8e::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInit9eebf462f38fc6a0db482795c8caf813::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInit9eebf462f38fc6a0db482795c8caf813::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInit9eebf462f38fc6a0db482795c8caf813::$classMap; }, null, ClassLoader::class); } diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index 69d9600c6..0384b0407 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -935,12 +935,12 @@ "source": { "type": "git", "url": "https://github.com/zoujingli/ThinkLibrary.git", - "reference": "cd754db0e9921b7aa6cf617887cdc8b122fe8c46" + "reference": "d121a5b29ef178def60e1b72c3b1ad9e0fece7cb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/cd754db0e9921b7aa6cf617887cdc8b122fe8c46", - "reference": "cd754db0e9921b7aa6cf617887cdc8b122fe8c46", + "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/d121a5b29ef178def60e1b72c3b1ad9e0fece7cb", + "reference": "d121a5b29ef178def60e1b72c3b1ad9e0fece7cb", "shasum": "", "mirrors": [ { @@ -956,7 +956,7 @@ "ext-json": "*", "topthink/framework": "^6.0" }, - "time": "2020-01-08T07:48:20+00:00", + "time": "2020-01-09T07:42:54+00:00", "type": "library", "extra": { "think": { diff --git a/vendor/services.php b/vendor/services.php index 7ca1ca4a2..16ef48798 100644 --- a/vendor/services.php +++ b/vendor/services.php @@ -1,5 +1,5 @@ <?php -// This file is automatically generated at:2020-01-08 15:50:23 +// This file is automatically generated at:2020-01-09 15:48:18 declare (strict_types = 1); return array ( 0 => 'think\\app\\Service', diff --git a/vendor/zoujingli/think-library/src/Library.php b/vendor/zoujingli/think-library/src/Library.php index be97dac3f..fa6a8a2da 100644 --- a/vendor/zoujingli/think-library/src/Library.php +++ b/vendor/zoujingli/think-library/src/Library.php @@ -79,6 +79,7 @@ class Library extends Service { // 注册系统任务指令 $this->commands([ + 'think\admin\queue\CleanQueue', 'think\admin\queue\WorkQueue', 'think\admin\queue\StopQueue', 'think\admin\queue\StateQueue', diff --git a/vendor/zoujingli/think-library/src/queue/CleanQueue.php b/vendor/zoujingli/think-library/src/queue/CleanQueue.php new file mode 100644 index 000000000..9ca98263d --- /dev/null +++ b/vendor/zoujingli/think-library/src/queue/CleanQueue.php @@ -0,0 +1,78 @@ +<?php + +// +---------------------------------------------------------------------- +// | ThinkAdmin +// +---------------------------------------------------------------------- +// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ] +// +---------------------------------------------------------------------- +// | 官方网站: https://gitee.com/zoujingli/ThinkLibrary +// +---------------------------------------------------------------------- +// | 开源协议 ( https://mit-license.org ) +// +---------------------------------------------------------------------- +// | gitee 代码仓库:https://gitee.com/zoujingli/ThinkLibrary +// | github 代码仓库:https://github.com/zoujingli/ThinkLibrary +// +---------------------------------------------------------------------- + +namespace think\admin\queue; + +use think\admin\service\QueueService; +use think\console\Command; +use think\console\Input; +use think\console\input\Argument; +use think\console\Output; + +/** + * 清理历史任务记录 + * Class CleanQueue + * @package think\admin\queue + */ +class CleanQueue extends Command +{ + /** + * 截止时间 + * @var integer + */ + protected $time; + + /** + * 绑定数据表 + * @var string + */ + protected $table = 'SystemQueue'; + + /** + * 配置指定信息 + */ + protected function configure() + { + $this->setName('xtask:clean')->setDescription('Clean up historical task records'); + $this->addArgument('time', Argument::OPTIONAL, 'BeforeTime', 7 * 24 * 3600); + } + + /** + * 清理历史任务 + * @param Input $input + * @param Output $output + * @throws \think\db\exception\DbException + * @throws \think\Exception + */ + protected function execute(Input $input, Output $output) + { + $this->time = $input->getArgument('time'); + if (empty($this->time) || !is_numeric($this->time) || $this->time <= 0) { + $this->output->error('Wrong parameter, the deadline needs to be an integer'); + } else { + $map = [['exec_time', '<', time() - $this->time]]; + $count1 = $this->app->db->name($this->table)->where($map)->delete(); + $this->output->info("Successfully cleaned up {$count1} history task records"); + // 重置超1小时无响应的记录 + $map = [['exec_time', '<', time() - 3600], ['status', '=', '2']]; + $count2 = $this->app->db->name($this->table)->where($map)->update(['status' => '4', 'exec_desc' => '执行等待超过1小时无响应']); + $this->output->info("Failed {$count2} records without response after waiting for more than 1 hour"); + // 返回消息到任务状态描述 + if (defined('WorkQueueCall')) { + throw new \think\Exception("清理{$count1}条任务记录,标志{$count2}条超60分钟无响应的任务", 3); + } + } + } +} \ No newline at end of file diff --git a/vendor/zoujingli/think-library/src/queue/WorkQueue.php b/vendor/zoujingli/think-library/src/queue/WorkQueue.php index 61f13c470..1f251dd29 100644 --- a/vendor/zoujingli/think-library/src/queue/WorkQueue.php +++ b/vendor/zoujingli/think-library/src/queue/WorkQueue.php @@ -21,7 +21,6 @@ use think\console\Command; use think\console\Input; use think\console\input\Argument; use think\console\Output; -use think\Exception; /** * 启动独立执行进程 @@ -37,6 +36,12 @@ class WorkQueue extends Command */ protected $code; + /** + * 当前任务数据 + * @var array + */ + protected $queue; + /** * 绑定数据表 * @var string @@ -66,40 +71,40 @@ class WorkQueue extends Command if (empty($this->code)) { $this->output->error('Task number needs to be specified for task execution'); } else try { - $queue = $this->app->db->name($this->table)->where(['code' => $this->code, 'status' => '1'])->find(); - if (empty($queue)) { + $this->queue = $this->app->db->name($this->table)->where(['code' => $this->code, 'status' => '1'])->find(); + if (empty($this->queue)) { // 这里不做任何处理(该任务可能在其它地方已经在执行) $this->output->warning($message = "The or status of task {$this->code} is abnormal"); } else { // 锁定任务状态 $this->app->db->name($this->table)->strict(false)->where(['code' => $this->code])->update([ - 'status' => '2', 'enter_time' => microtime(true), 'outer_time' => '0', 'exec_pid' => getmypid(), 'exec_desc' => '', 'attempts' => $this->app->db->raw('attempts+1'), + 'enter_time' => microtime(true), 'attempts' => $this->app->db->raw('attempts+1'), + 'outer_time' => '0', 'exec_pid' => getmypid(), 'exec_desc' => '', 'status' => '2', ]); // 设置进程标题 if (($process = ProcessService::instance())->iswin()) { - $this->setProcessTitle("ThinkAdmin {$process->version()} Queue - {$queue['title']}"); + $this->setProcessTitle("ThinkAdmin {$process->version()} Queue - {$this->queue['title']}"); } // 执行任务内容 - if (class_exists($command = $queue['command'])) { + if (class_exists($command = $this->queue['command'])) { // 自定义服务,支持返回消息(支持异常结束,异常码可选择 3|4 设置任务状态) if ($command instanceof QueueService) { - $data = json_decode($queue['data'], true) ?: []; + $data = json_decode($this->queue['data'], true) ?: []; $this->update('3', $command::instance()->initialize($this->code)->execute($data)); } else { - throw new Exception("Task processing class {$command} does not inherit class think\\admin\\service\\QueueService"); + throw new \think\Exception("自定义 {$command} 未继承 QueueService"); } } else { // 自定义指令,不支持返回消息(支持异常结束,异常码可选择 3|4 设置任务状态) - $attr = explode(' ', trim(preg_replace('|\s+|', ' ', $queue['command']))); - $this->update('3', $this->app->console->call(array_shift($attr), $attr, 'console')); + defined('WorkQueueCall') or define('WorkQueueCall', true); + $attr = explode(' ', trim(preg_replace('|\s+|', ' ', $this->queue['command']))); + $this->update('3', $this->app->console->call(array_shift($attr), $attr)->fetch(), false); } } } catch (\Exception $exception) { - if (in_array($exception->getCode(), ['3', '4'])) { - $this->update($exception->getCode(), $exception->getMessage()); - } else { - $this->update('4', $exception->getMessage()); - } + $code = $exception->getCode(); + if (intval($code) !== 3) $code = 4; + $this->update($code, $exception->getMessage()); } } @@ -107,17 +112,28 @@ class WorkQueue extends Command * 修改当前任务状态 * @param integer $status 任务状态 * @param string $message 消息内容 - * @return boolean + * @param boolean $issplit 是否分隔 * @throws \think\db\exception\DbException */ - protected function update($status, $message) + protected function update($status, $message, $issplit = true) { - $desc = explode("\n", trim(is_string($message) ? $message : '')); - $result = $this->app->db->name($this->table)->strict(false)->where(['code' => $this->code])->update([ + // 更新当前任务 + $info = trim(is_string($message) ? $message : ''); + $desc = $issplit ? explode("\n", $info) : [$message]; + $this->app->db->name($this->table)->strict(false)->where(['code' => $this->code])->update([ 'status' => $status, 'outer_time' => microtime(true), 'exec_pid' => getmypid(), 'exec_desc' => $desc[0], ]); $this->output->writeln(is_string($message) ? $message : ''); - return $result == false; + // 注册循环任务 + if (isset($this->queue['loops_time']) && $this->queue['loops_time'] > 0) try { + QueueService::instance()->register( + $this->queue['title'], $this->queue['command'], $this->queue['loops_time'], + json_decode($this->queue['exec_data'], true), $this->queue['rscript'], $this->queue['loops_time'], + $this->app->db->name($this->table)->where(['code' => $this->code])->value('attempts') + 1 + ); + } catch (\Exception $exception) { + $this->app->log->error("Queue {$this->queue['code']} Loops Failed. {$exception->getMessage()}"); + } } } diff --git a/vendor/zoujingli/think-library/src/service/QueueService.php b/vendor/zoujingli/think-library/src/service/QueueService.php index faeb72cf8..ee9ed585e 100644 --- a/vendor/zoujingli/think-library/src/service/QueueService.php +++ b/vendor/zoujingli/think-library/src/service/QueueService.php @@ -98,35 +98,50 @@ class QueueService extends Service return $this->initialize($this->code); } + /** + * 添加清理7天前的记录及超时任务 + * @throws \think\Exception + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function addCleanQueue() + { + $this->register('清理7天前的记录及执行超时任务', "xtask:clean", 0, [], 0, 3600); + } + /** * 注册异步处理任务 * @param string $title 任务名称 * @param string $command 执行内容 - * @param integer $later 延时执行时间 + * @param integer $later 延时时间 * @param array $data 任务附加数据 - * @param integer $rscript 任务多开 + * @param integer $rscript 任务类型(0单例,1多例) + * @param integer $loops 循环等待时间 + * @param integer $attempts 执行次数 * @return $this * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function register($title, $command, $later = 0, $data = [], $rscript = 1) + public function register($title, $command, $later = 0, $data = [], $rscript = 1, $loops = 0, $attempts = 0) { $map = [['title', '=', $title], ['status', 'in', ['1', '2']]]; if (empty($rscript) && $this->app->db->name('SystemQueue')->where($map)->count() > 0) { throw new \think\Exception('该任务已经创建,请耐心等待处理完成!'); } - $this->app->db->name('SystemQueue')->failException(true)->insert([ + $this->app->db->name('SystemQueue')->strict(false)->failException(true)->insert([ 'code' => $this->code = CodeExtend::uniqidDate(16), 'title' => $title, 'command' => $command, - 'attempts' => '0', + 'attempts' => $attempts, 'rscript' => intval(boolval($rscript)), 'exec_data' => json_encode($data, JSON_UNESCAPED_UNICODE), 'exec_time' => $later > 0 ? time() + $later : time(), 'enter_time' => '0', 'outer_time' => '0', + 'loops_time' => $loops, ]); return $this->initialize($this->code); } diff --git a/vendor/zoujingli/think-library/src/service/SystemService.php b/vendor/zoujingli/think-library/src/service/SystemService.php index c5eea2eda..a717f48fc 100644 --- a/vendor/zoujingli/think-library/src/service/SystemService.php +++ b/vendor/zoujingli/think-library/src/service/SystemService.php @@ -176,7 +176,7 @@ class SystemService extends Service */ public function putDebug($data, $new = false, $file = null) { - if (is_null($file)) $file = $this->app->getRuntimePath() . date('Ymd') . '.txt'; + if (is_null($file)) $file = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . date('Ymd') . '.txt'; $str = (is_string($data) ? $data : ((is_array($data) || is_object($data)) ? print_r($data, true) : var_export($data, true))) . PHP_EOL; $new ? file_put_contents($file, $str) : file_put_contents($file, $str, FILE_APPEND); }