7 Commits
0.1.0 ... 0.2.1

Author SHA1 Message Date
daodoa97
7727dd95cf fix: metric default disable 2021-02-04 11:22:47 +08:00
刀刀
d6216f1830 Merge pull request #41 from wxfjamdc/patch-2
Update User.php
2021-01-23 18:46:30 +08:00
刀刀
78b06154bb Merge pull request #46 from wxfjamdc/patch-4
Update SystemController.php
2021-01-23 18:44:42 +08:00
WxAmd
20b2cb0a70 Update SystemController.php
返回有权限的 system_module
2021-01-23 16:19:08 +08:00
WxAmd
fceeda8203 Update User.php
保存用户信息时 realname 为空自动填充
2020-12-31 15:28:31 +08:00
daodao97
c4ba9db9a5 perf: support hyperf 2.1 2020-12-30 15:44:00 +08:00
daodao97
e614556a31 perf: support hyperf 2.1 2020-12-30 15:39:58 +08:00
12 changed files with 115 additions and 63 deletions

View File

@@ -8,33 +8,33 @@
}
],
"require": {
"php": ">=7.2",
"php": ">=7.3",
"ext-json": "*",
"ext-pdo": "*",
"ext-swoole": ">=4.4",
"aliyuncs/oss-sdk-php": "^2.3",
"box/spout": "^3.1",
"hyperf/amqp": "~2.0.0",
"hyperf/async-queue": "~2.0.0",
"hyperf/cache": "~2.0.0",
"hyperf/command": "~2.0.0",
"hyperf/config": "~2.0.0",
"hyperf/constants": "~2.0.0",
"hyperf/crontab": "~2.0.0",
"hyperf/database": "~2.0.0",
"hyperf/db-connection": "~2.0.0",
"hyperf/amqp": "~2.1.0",
"hyperf/async-queue": "~2.1.0",
"hyperf/cache": "~2.1.0",
"hyperf/command": "~2.1.0",
"hyperf/config": "~2.1.0",
"hyperf/constants": "~2.1.0",
"hyperf/crontab": "~2.1.0",
"hyperf/database": "~2.1.0",
"hyperf/db-connection": "~2.1.0",
"hyperf/filesystem": "^2.0",
"hyperf/framework": "~2.0.0",
"hyperf/guzzle": "~2.0.0",
"hyperf/http-server": "~2.0.0",
"hyperf/logger": "~2.0.0",
"hyperf/memory": "~2.0.0",
"hyperf/metric": "~2.0.0",
"hyperf/nsq": "~2.0.0",
"hyperf/process": "~2.0.0",
"hyperf/redis": "~2.0.0",
"hyperf/snowflake": "~2.0.0",
"hyperf/validation": "~2.0.0",
"hyperf/framework": "~2.1.0",
"hyperf/guzzle": "~2.1.0",
"hyperf/http-server": "~2.1.0",
"hyperf/logger": "~2.1.0",
"hyperf/memory": "~2.1.0",
"hyperf/metric": "~2.1.0",
"hyperf/nsq": "~2.1.0",
"hyperf/process": "~2.1.0",
"hyperf/redis": "~2.1.0",
"hyperf/snowflake": "~2.1.0",
"hyperf/validation": "~2.1.0",
"nette/php-generator": "^3.4",
"xxtime/flysystem-aliyun-oss": "^1.5",
"yadakhov/insert-on-duplicate-key": "^1.2",

View File

@@ -1,14 +1,57 @@
### 为什么要做 CronCenter
#### 当前定时任务管理的解放方案及问题
1. 系统原生 crontab
- 需要有服务器权限, 有改动都要登录服务器手动操作
2. hyperf/crontab 提供的 基于 swoole/timer 的模拟 crontab
- 定时任务的编排以硬编码的方式放置在配置文件中
- 如果有变动需要 `编码->改配置->CodeReview->部署` 走一遍完整的上限流程
3. AirFlow 等工具
- 诚然也是一种很优秀的解决方案, 但对轻量级的定时任务需求来说有显得过重
### 如何规避上面的问题?
`CronCenter` 的思路是, 在 `hyperf/crontab` 的基础上, 将 `config/autoload/crontab.php` 中的定时任务配置, 迁移到数据库中, 提供后台, 以方便的管理所有定时任务的 `启动`, `停止`, `运行参数`, `单例/多例` 等等, 这样将极大的方便我们对定时任务的管理, 也相对轻量.
#### 具体实现
`hyperf-admin/cron-center` 重载了 `hyperf/crontab` 的关键类 `Hyperf/Crontab/Strategy/Executor`, 具体源码 [cron-center](https://github.com/hyperf-admin/hyperf-admin/blob/master/src/cron-center/src/ConfigProvider.php#L31)
#### 后台概览
![list](https://gitee.com/daodao97/asset/raw/master/imgs/8ynybw.png)
![lQJf50](https://gitee.com/daodao97/asset/raw/master/imgs/lQJf50.png)
#### 使用细节
脚本作业的管理中心, 可以在代码中实现`class`类型, `command`类型的脚本作业, 在后台添加相关任务, 即可在相应脚本机上执行
可以对任务的入口, 执行规划, 执行节点, 执行参数 等进行配置, 也可在列表主动触发任务
作业必须基于`App\Util\CronCenter\ClassJobAbstract`, 或`App/Util/CronCenter/CommandJobAbstract.php`抽象类进行实现, 才可进行执行状态的跟踪
作业必须基于`HyperfAdmin/CronCenter/ClassJobAbstract`, 或`HyperfAdmin/CronCenter/CommandJobAbstract.php`抽象类进行实现, 才可进行执行状态的跟踪
`CronCenter`的实现基于`hyperf-crontab`进行实现, 具体代码在`src/CronCenter`, 更多细节可查看[文档](https://hyperf.wiki/#/zh-cn/crontab)
### 相关配置
开启`cron-center` `config/autoload/crontab.php`
1. 首先要启用 `hyperf/crotnab`
`config/autoload/crontab.php`
```php
[
"enable" => true
// config/autoload/crontab.php
return [
"enable" => true,
...
]
```
2. 启用 `CronCenter`
```php
// config/config.php
return [
"cron_center" => [
"enable" => true,
],
...
]
```
### 此方案的问题
此种模拟式的 `crontab` 有一个很明显的弊端, 也就是任务的 `派遣`, `运行` 等都依赖于服务进程, 也就是服务一旦重启, 运行中的任务都会停止, 如果对任务的运行中断极度敏感, 则不建议使用此种方式.

View File

@@ -79,6 +79,7 @@ php bin/hyperf.php vendor:publish hyperf/validation
'password' => [
'salt' => env('HYPERF_ADMIN_PWD_SALT', 'c093d70f088499c3a837cae00c042f14'), // 用 md5(time()) 获取 salt
]
```
#### 8. 启动
@@ -87,7 +88,7 @@ php bin/hyperf.php vendor:publish hyperf/validation
composer watch
```
## nginx配置
## 生产环境nginx配置
```nginx
upstream backend {

View File

@@ -30,7 +30,7 @@ class SystemController extends AdminAbstractController
$user_id = $this->auth_service->get('id');
$modules = $this->permission_service->getModules($user_id);
$config['system_module'] = array_filter($config['system_module'], function ($item) use ($modules) {
return !in_array($item['name'], $modules);
return in_array($item['name'], $modules);
});
}

View File

@@ -82,4 +82,9 @@ class User extends BaseModel
{
return $value ?: $this->username;
}
public function setRealnameAttribute($value)
{
$this->attributes['realname'] = $value ?: $this->username;
}
}

View File

@@ -9,8 +9,8 @@
}
],
"require": {
"hyperf/async-queue": "~2.0.0",
"hyperf/process": "~2.0.0",
"hyperf/async-queue": "~2.1.0",
"hyperf/process": "~2.1.0",
"hyperf-admin/rule-engine": "dev-master"
},
"autoload": {

View File

@@ -9,38 +9,38 @@
}
],
"require": {
"php": ">=7.2",
"php": ">=7.3",
"ext-json": "*",
"ext-pdo": "*",
"ext-swoole": ">=4.4",
"ext-yaml": "*",
"aliyuncs/oss-sdk-php": "^2.3",
"box/spout": "^3.1",
"hyperf/amqp": "~2.0.0",
"hyperf/cache": "~2.0.0",
"hyperf/command": "~2.0.0",
"hyperf/config": "~2.0.0",
"hyperf/constants": "~2.0.0",
"hyperf/database": "~2.0.0",
"hyperf/db-connection": "~2.0.0",
"hyperf/filesystem": "~2.0.0",
"hyperf/framework": "~2.0.0",
"hyperf/guzzle": "~2.0.0",
"hyperf/http-server": "~2.0.0",
"hyperf/logger": "~2.0.0",
"hyperf/memory": "~2.0.0",
"hyperf/metric": "~2.0.0",
"hyperf/nsq": "~2.0.0",
"hyperf/process": "~2.0.0",
"hyperf/redis": "~2.0.0",
"hyperf/snowflake": "~2.0.0",
"hyperf/amqp": "~2.1.0",
"hyperf/cache": "~2.1.0",
"hyperf/command": "~2.1.0",
"hyperf/config": "~2.1.0",
"hyperf/constants": "~2.1.0",
"hyperf/database": "~2.1.0",
"hyperf/db-connection": "~2.1.0",
"hyperf/filesystem": "~2.1.0",
"hyperf/framework": "~2.1.0",
"hyperf/guzzle": "~2.1.0",
"hyperf/http-server": "~2.1.0",
"hyperf/logger": "~2.1.0",
"hyperf/memory": "~2.1.0",
"hyperf/metric": "~2.1.0",
"hyperf/nsq": "~2.1.0",
"hyperf/process": "~2.1.0",
"hyperf/redis": "~2.1.0",
"hyperf/snowflake": "~2.1.0",
"yadakhov/insert-on-duplicate-key": "^1.2"
},
"require-dev": {
"swoft/swoole-ide-helper": "^4.2",
"phpstan/phpstan": "^0.11.2",
"hyperf/devtool": "~2.0.0",
"hyperf/testing": "~2.0.0",
"hyperf/devtool": "~2.1.0",
"hyperf/testing": "~2.1.0",
"daodao97/hyperf-watch": "dev-master",
"symfony/var-dumper": "^5.0"
},

View File

@@ -88,6 +88,9 @@ class ConfigProvider
HttpLogMiddleware::class,
],
],
'metric' => [
'use_standalone_process' => false
],
'redis' => [
'metric' => [
'host' => env('REDIS_ALERT_MANAGER_HOST', 'localhost'),

View File

@@ -10,10 +10,10 @@
}
],
"require": {
"hyperf/crontab": "~2.0.0",
"hyperf/process": "~2.0.0",
"hyperf/command": "~2.0.0",
"hyperf/event": "~2.0.0",
"hyperf/crontab": "~2.1.0",
"hyperf/process": "~2.1.0",
"hyperf/command": "~2.1.0",
"hyperf/event": "~2.1.0",
"hyperf-admin/base-utils": "dev-master"
},
"autoload": {

View File

@@ -9,8 +9,8 @@
}
],
"require": {
"hyperf/process": "~2.0.0",
"hyperf/amqp": "~2.0.0"
"hyperf/process": "~2.1.0",
"hyperf/amqp": "~2.1.0"
},
"autoload": {
"psr-4": {

View File

@@ -9,11 +9,11 @@
}
],
"require": {
"php": ">=7.2",
"hyperf/amqp": "~2.0.0",
"hyperf/crontab": "~2.0.0",
"hyperf/nsq": "~2.0.0",
"hyperf/process": "~2.0.0"
"php": ">=7.3",
"hyperf/amqp": "~2.1.0",
"hyperf/crontab": "~2.1.0",
"hyperf/nsq": "~2.1.0",
"hyperf/process": "~2.1.0"
},
"autoload": {
"psr-4": {

View File

@@ -9,7 +9,7 @@
}
],
"require": {
"hyperf/validation": "~2.0.0",
"hyperf/validation": "~2.1.0",
"hyperf-admin/base-utils": "dev-master"
},
"autoload": {