diff --git a/.gitignore b/.gitignore index 1c6eb78..63c7ee6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ !composer.json !LICENSE.txt !README.md +!.phpstorm.meta.php !.gitignore \ No newline at end of file diff --git a/LICENSE.txt b/LICENSE.txt index 4b233df..901c3e0 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -6,7 +6,7 @@ BusyPHP 最终用户授权协议 BusyPHP官方网站: www.harter.cn 为了使您正确并合法的使用本软件,请您在使用前务必阅读清楚下面的协议条款: -一、本授权协议适用且仅适用于 BusyPHP 6.x.x 版本及官方基于 BusyPHP 开发的扩展composer安装包,BusyPHP官方对本授权协议拥有最终解释权。 +一、本授权协议适用且仅适用于 BusyPHP 6.x.x 7.x.x 版本及官方基于 BusyPHP 开发的扩展composer安装包,BusyPHP官方对本授权协议拥有最终解释权。 二、协议许可的权利 1.您可以在完全遵守本最终用户授权协议的基础上,将本软件应用于非商业用途,而不必支付软件版权授权费用。 @@ -28,4 +28,4 @@ BusyPHP官方网站: www.harter.cn 3.电子文本形式的授权协议如同双方书面签署的协议一样,具有完全的和等同的法律效力。您一旦开始确认本协议并安装BusyPHP,即被视为完全理解并接受本协议的各项条款,在享有上述条款授予的权力的同时,受到相关的约束和限制。协议许可范围以外的行为,将直接违反本授权协议并构成侵权,我们有权随时终止授权,责令停止损害,并保留追究相关责任的权力。 4.如果本软件带有其它软件的整合API示范例子包,这些文件版权不属于本软件官方,并且这些文件是没经过授权发布的,请参考相关软件的使用许可合法的使用。 -协议发布日期: 2021年10月21日 By 山西函拓科技有限公司 \ No newline at end of file +协议发布日期: 2023年5月18日 By 山西函拓科技有限公司 \ No newline at end of file diff --git a/README.md b/README.md index 3a294d5..a585167 100644 --- a/README.md +++ b/README.md @@ -21,16 +21,16 @@ composer require busyphp/queue php think queue:work ``` -| 参数 | 默认值 | 说明 | -| :----- | :-----: | :----- | -| connection | sync | 设置队列连接器名称,默认依据 `config/queue.php` 中的 `default` 确定 | -| --queue | default | 设置执行的队列名称 | -| --once | - | 仅处理队列上的下一个任务后就退出 | -| --delay | 0 | 如果本次任务执行抛出异常且任务未被删除时,设置其下次执行前延迟多少秒 | -| --memory | 128 | 该进程允许使用的内存上限,以 M 为单位 | -| --timeout | 60 | 该进程的允许执行的最长时间,以秒为单位 | -| --sleep | 3 | 如果队列中无任务,则多长时间后重新检查 | -| --tries | 0 | 如果任务已经超过尝试次数上限,0为不限,则触发当前任务类型下的failed()方法 | +| 参数 | 默认值 | 说明 | +|:-----------|:-------:|:--------------------------------------------------| +| connection | sync | 设置队列连接器名称,默认依据 `config/queue.php` 中的 `default` 确定 | +| --queue | default | 设置执行的队列名称 | +| --once | - | 仅处理队列上的下一个任务后就退出 | +| --delay | 0 | 如果本次任务执行抛出异常且任务未被删除时,设置其下次执行前延迟多少秒 | +| --memory | 128 | 该进程允许使用的内存上限,以 M 为单位 | +| --timeout | 60 | 该进程的允许执行的最长时间,以秒为单位 | +| --sleep | 3 | 如果队列中无任务,则多长时间后重新检查 | +| --tries | 0 | 如果任务已经超过尝试次数上限,0为不限,则触发当前任务类型下的failed()方法 | ### `queue:listen` 命令 @@ -40,15 +40,15 @@ php think queue:work php think queue:listen ``` -| 参数 | 默认值 | 说明 | -| :----- | :-----: | :----- | -| connection | sync | 设置队列连接器名称,默认依据 `config/queue.php` 中的 `default` 确定 | -| --queue | default | 设置执行的队列名称 | -| --delay | 0 | 如果本次任务执行抛出异常且任务未被删除时,设置其下次执行前延迟多少秒 | -| --memory | 128 | 子进程允许使用的内存上限,以 M 为单位 | -| --timeout | 60 | 子进程的允许执行的最长时间,以秒为单位 | -| --sleep | 3 | 如果队列中无任务,则多长时间后重新检查 | -| --tries | 0 | 如果任务已经超过尝试次数上限,0为不限,则触发当前任务类型下的failed()方法 | +| 参数 | 默认值 | 说明 | +|:-----------|:-------:|:--------------------------------------------------| +| connection | sync | 设置队列连接器名称,默认依据 `config/queue.php` 中的 `default` 确定 | +| --queue | default | 设置执行的队列名称 | +| --delay | 0 | 如果本次任务执行抛出异常且任务未被删除时,设置其下次执行前延迟多少秒 | +| --memory | 128 | 子进程允许使用的内存上限,以 M 为单位 | +| --timeout | 60 | 子进程的允许执行的最长时间,以秒为单位 | +| --sleep | 3 | 如果队列中无任务,则多长时间后重新检查 | +| --tries | 0 | 如果任务已经超过尝试次数上限,0为不限,则触发当前任务类型下的failed()方法 | ### `queue:failed` 列出所有失败的任务 @@ -96,8 +96,6 @@ return [ 'database' => [ 'type' => 'database', 'queue' => 'default', - 'table' => 'system_jobs', - 'connection' => null, ], // redis驱动 @@ -117,8 +115,7 @@ return [ // 任务失败 'failed' => [ - 'type' => 'none', - 'table' => 'system_jobs_failed', + 'type' => 'database', ], ]; ``` diff --git a/composer.json b/composer.json index 73ae771..5de39be 100644 --- a/composer.json +++ b/composer.json @@ -13,13 +13,13 @@ "email" : "busy.life@qq.com" } ], - "license" : "CC-BY-NC-4.0", + "license" : "Apache-2.0", "require" : { - "php" : ">=7.1.0", + "php" : ">=8.0.0", "ext-json" : "*", - "busyphp/busyphp" : "^6.0", - "symfony/process" : "^4.2", - "nesbot/carbon" : "^2.16" + "busyphp/busyphp" : "^7.0", + "nesbot/carbon" : "^2.16", + "symfony/process" : "^6.0" }, "require-dev" : { "symfony/var-dumper" : "^4.2" @@ -33,12 +33,9 @@ ] }, "extra" : { - "branch-alias" : { - "dev-master" : "2.0.x-dev" - }, "think" : { "config" : { - "busy-queue" : "src/config/config.php" + "queue" : "src/config/config.php" } }, "busyphp" : { @@ -46,11 +43,8 @@ "name" : "消费队列模块", "class" : "BusyPHP\\queue\\app\\controller\\ManagerController", "install" : { - "install_operate" : { - "request_confirm" : "确认要安装 __package__ 吗?
注意:安装会覆盖数据库中的 *_system_jobs *_system_jobs_failed
" - }, - "uninstall_operate" : { - "request_confirm" : "确认要卸载 __package__ 吗?
注意:卸载会删除数据库中的 *_system_jobs *_system_jobs_failed
" + "install_operate" : { + "request_confirm" : "确认要安装 __package__ 吗?
注意:安装会覆盖数据库中的 *_plugin_queue_jobs *_plugin_queue_jobs_failed
" } } }, diff --git a/src/Service.php b/src/Service.php index b8be568..e6b36f9 100644 --- a/src/Service.php +++ b/src/Service.php @@ -30,7 +30,7 @@ public function register() $type = Arr::pull($config, 'type', 'none'); - $class = false !== strpos($type, '\\') ? $type : '\\BusyPHP\\queue\\failed\\' . Str::studly($type); + $class = str_contains($type, '\\') ? $type : '\\BusyPHP\\queue\\failed\\' . Str::studly($type); return $this->app->invokeClass($class, [$config]); }); diff --git a/src/WithQueueConfig.php b/src/WithQueueConfig.php index 7471306..1ae5be0 100644 --- a/src/WithQueueConfig.php +++ b/src/WithQueueConfig.php @@ -19,7 +19,7 @@ trait WithQueueConfig * @param mixed $default * @return mixed */ - public function getQueueConfig(string $name, $default = null) + public function getQueueConfig(string $name, mixed $default = null) : mixed { if (isset($this->app)) { $app = $this->app; @@ -27,6 +27,6 @@ public function getQueueConfig(string $name, $default = null) $app = App::getInstance(); } - return $app->config->get("busy-queue.{$name}", $default); + return $app->config->get("queue.$name", $default); } } \ No newline at end of file diff --git a/src/app/controller/ManagerController.php b/src/app/controller/ManagerController.php index dd9550a..2a5afe3 100644 --- a/src/app/controller/ManagerController.php +++ b/src/app/controller/ManagerController.php @@ -1,11 +1,14 @@ "CREATE TABLE `#__table_prefix__#system_jobs` ( + protected string $jobsSql = << "CREATE TABLE `#__table_prefix__#system_jobs_failed` ( +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='任务列队表' +SQL; + + protected string $jobsFailedSql = <<startTrans(); try { - foreach ($this->deleteTableSql as $item) { - $this->executeSQL($item); + if (!$this->hasTable('plugin_queue_jobs')) { + $this->executeSQL($this->jobsSql); } - - foreach ($this->createTableSql as $item) { - $this->executeSQL($item); + if (!$this->hasTable('queue_jobs_failed')) { + $this->executeSQL($this->jobsFailedSql); } $model->setInstall($this->info->package); $model->commit(); - } catch (Exception $e) { + } catch (Throwable $e) { $model->rollback(); throw $e; @@ -102,30 +93,10 @@ public function install() : Response /** * 卸载插件 * @return Response - * @throws Exception */ public function uninstall() : Response { - $model = SystemPlugin::init(); - $model->startTrans(); - try { - foreach ($this->deleteTableSql as $item) { - $this->executeSQL($item); - } - - $model->setUninstall($this->info->package); - - $model->commit(); - } catch (Exception $e) { - $model->rollback(); - - throw $e; - } - - $this->updateCache(); - $this->logUninstall(); - - return $this->success('卸载成功'); + throw new RuntimeException('不支持卸载'); } diff --git a/src/config/config.php b/src/config/config.php index 07a65e2..fcdc915 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -8,7 +8,7 @@ return [ // 默认队列连接器 - 'default' => 'sync', + 'default' => 'database', // 队列连接器配置 'connections' => [ @@ -19,10 +19,8 @@ // 数据库 'database' => [ - 'type' => 'database', - 'queue' => 'default', - 'table' => 'system_jobs', - 'connection' => null, + 'type' => 'database', + 'queue' => 'default', ], // redis @@ -43,7 +41,6 @@ // 任务超过尝试次数上限的处理方式 'failed' => [ - 'type' => 'database', - 'table' => 'system_jobs_failed', + 'type' => 'database', ], ]; diff --git a/src/connector/Database.php b/src/connector/Database.php index 1bb98b9..18a853a 100644 --- a/src/connector/Database.php +++ b/src/connector/Database.php @@ -2,6 +2,7 @@ namespace BusyPHP\queue\connector; +use BusyPHP\helper\ArrayHelper; use Carbon\Carbon; use stdClass; use think\Db; @@ -57,9 +58,14 @@ public function __construct(ConnectionInterface $db, $table, $default = 'default public static function __make(Db $db, $config) { - $connection = $db->connect($config['connection'] ?? null); + $connection = $db->connect(ArrayHelper::get($config, 'connection')); - return new self($connection, $config['table'], $config['queue'], $config['retry_after'] ?? 60); + return new self( + $connection, + ArrayHelper::get($config, 'table') ?: 'plugin_queue_jobs', + ArrayHelper::get($config, 'queue') ?: 'default', + ArrayHelper::get($config, 'retry_after', 60) ?: 60 + ); } diff --git a/src/connector/Redis.php b/src/connector/Redis.php index bb7111c..950dd4a 100644 --- a/src/connector/Redis.php +++ b/src/connector/Redis.php @@ -11,6 +11,7 @@ namespace BusyPHP\queue\connector; +use BusyPHP\helper\ArrayHelper; use Closure; use Exception; use RedisException; @@ -117,7 +118,13 @@ public function __call($name, $arguments) } }; - return new self($redis, $config['queue'], $config['retry_after'] ?? 60, $config['block_for'] ?? null, $config['prefix'] ?? ''); + return new self( + $redis, + ArrayHelper::get($config, 'queue') ?: 'default', + ArrayHelper::get($config, 'retry_after', 60) ?: 60, + $config['block_for'] ?? null, + $config['prefix'] ?? '' + ); } diff --git a/src/failed/Database.php b/src/failed/Database.php index bf862c1..52bec98 100644 --- a/src/failed/Database.php +++ b/src/failed/Database.php @@ -2,6 +2,7 @@ namespace BusyPHP\queue\failed; +use BusyPHP\helper\ArrayHelper; use Carbon\Carbon; use think\Db; use BusyPHP\queue\FailedJob; @@ -37,7 +38,7 @@ public function __construct(Db $db, $table) public static function __make(Db $db, $config) { - return new self($db, $config['table']); + return new self($db, ArrayHelper::get($config, 'table') ?: 'plugin_queue_jobs_failed'); }