feat: 合并代码
This commit is contained in:
@@ -25,7 +25,6 @@ type SystemInfoController struct {
|
||||
// GET /
|
||||
func (s *SystemInfoController) Info(c *gin.Context) {
|
||||
c.JSON(200, result.OkData(map[string]any{
|
||||
"project": s.systemInfogService.ProjectInfo(),
|
||||
"cpu": s.systemInfogService.CPUInfo(),
|
||||
"memory": s.systemInfogService.MemoryInfo(),
|
||||
"network": s.systemInfogService.NetworkInfo(),
|
||||
|
||||
@@ -20,6 +20,8 @@ type SysJob struct {
|
||||
Concurrent string `json:"concurrent"`
|
||||
// 任务状态(0暂停 1正常)
|
||||
Status string `json:"status"`
|
||||
// 是否记录任务日志
|
||||
SaveLog string `json:"saveLog"`
|
||||
// 创建者
|
||||
CreateBy string `json:"createBy"`
|
||||
// 创建时间
|
||||
|
||||
@@ -20,7 +20,7 @@ type BarProcessor struct {
|
||||
count int
|
||||
}
|
||||
|
||||
func (s *BarProcessor) Execute(data any) any {
|
||||
func (s *BarProcessor) Execute(data any) (any, error) {
|
||||
logger.Infof("执行 %d 次,上次进度: %d ", s.count, s.progress)
|
||||
s.count++
|
||||
|
||||
@@ -51,10 +51,11 @@ func (s *BarProcessor) Execute(data any) any {
|
||||
}
|
||||
|
||||
// 返回结果,用于记录执行结果
|
||||
return map[string]any{
|
||||
result := map[string]any{
|
||||
"repeat": options.Repeat,
|
||||
"jobName": sysJob.JobName,
|
||||
"invokeTarget": sysJob.InvokeTarget,
|
||||
"targetParams": sysJob.TargetParams,
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ type FooProcessor struct {
|
||||
count int
|
||||
}
|
||||
|
||||
func (s *FooProcessor) Execute(data any) any {
|
||||
func (s *FooProcessor) Execute(data any) (any, error) {
|
||||
logger.Infof("执行 %d 次,上次进度: %d ", s.count, s.progress)
|
||||
s.count++
|
||||
|
||||
@@ -43,10 +43,11 @@ func (s *FooProcessor) Execute(data any) any {
|
||||
}
|
||||
|
||||
// 返回结果,用于记录执行结果
|
||||
return map[string]any{
|
||||
result := map[string]any{
|
||||
"repeat": options.Repeat,
|
||||
"jobName": sysJob.JobName,
|
||||
"invokeTarget": sysJob.InvokeTarget,
|
||||
"targetParams": sysJob.TargetParams,
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -10,17 +10,18 @@ var NewProcessor = &simpleProcessor{}
|
||||
// simple 队列任务处理
|
||||
type simpleProcessor struct{}
|
||||
|
||||
func (s *simpleProcessor) Execute(data any) any {
|
||||
func (s *simpleProcessor) Execute(data any) (any, error) {
|
||||
options := data.(cron.JobData)
|
||||
|
||||
sysJob := options.SysJob
|
||||
logger.Infof("重复 %v 任务ID %s", options.Repeat, sysJob.JobID)
|
||||
|
||||
// 返回结果,用于记录执行结果
|
||||
return map[string]any{
|
||||
result := map[string]any{
|
||||
"repeat": options.Repeat,
|
||||
"jobName": sysJob.JobName,
|
||||
"invokeTarget": sysJob.InvokeTarget,
|
||||
"targetParams": sysJob.TargetParams,
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
// 实例化数据层 SysJobImpl 结构体
|
||||
var NewSysJobImpl = &SysJobImpl{
|
||||
selectSql: `select job_id, job_name, job_group, invoke_target, target_params, cron_expression,
|
||||
misfire_policy, concurrent, status, create_by, create_time, remark from sys_job`,
|
||||
misfire_policy, concurrent, status, save_log, create_by, create_time, remark from sys_job`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"job_id": "JobID",
|
||||
@@ -27,6 +27,7 @@ var NewSysJobImpl = &SysJobImpl{
|
||||
"misfire_policy": "MisfirePolicy",
|
||||
"concurrent": "Concurrent",
|
||||
"status": "Status",
|
||||
"save_log": "SaveLog",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
@@ -245,6 +246,9 @@ func (r *SysJobImpl) InsertJob(sysJob model.SysJob) string {
|
||||
if sysJob.Status != "" {
|
||||
params["status"] = sysJob.Status
|
||||
}
|
||||
if sysJob.SaveLog != "" {
|
||||
params["save_log"] = sysJob.SaveLog
|
||||
}
|
||||
if sysJob.Remark != "" {
|
||||
params["remark"] = sysJob.Remark
|
||||
}
|
||||
@@ -308,6 +312,9 @@ func (r *SysJobImpl) UpdateJob(sysJob model.SysJob) int64 {
|
||||
if sysJob.Status != "" {
|
||||
params["status"] = sysJob.Status
|
||||
}
|
||||
if sysJob.SaveLog != "" {
|
||||
params["save_log"] = sysJob.SaveLog
|
||||
}
|
||||
if sysJob.Remark != "" {
|
||||
params["remark"] = sysJob.Remark
|
||||
}
|
||||
|
||||
@@ -2,9 +2,6 @@ package service
|
||||
|
||||
// ISystemInfo 服务器系统相关信息 服务层接口
|
||||
type ISystemInfo interface {
|
||||
// ProjectInfo 程序项目信息
|
||||
ProjectInfo() map[string]any
|
||||
|
||||
// SystemInfo 系统信息
|
||||
SystemInfo() map[string]any
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
@@ -24,105 +23,43 @@ var NewSystemInfoImpl = &SystemInfoImpl{}
|
||||
// SystemInfoImpl 服务器系统相关信息 服务层处理
|
||||
type SystemInfoImpl struct{}
|
||||
|
||||
// ProjectInfo 程序项目信息
|
||||
func (s *SystemInfoImpl) ProjectInfo() map[string]any {
|
||||
// 获取工作目录
|
||||
appDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
appDir = ""
|
||||
}
|
||||
// 项目依赖
|
||||
dependencies := s.dependencies()
|
||||
return map[string]any{
|
||||
"appDir": appDir,
|
||||
"env": config.Env(),
|
||||
"name": config.Get("framework.name"),
|
||||
"version": config.Get("framework.version"),
|
||||
"dependencies": dependencies,
|
||||
}
|
||||
}
|
||||
|
||||
// dependencies 读取mod内项目包依赖
|
||||
func (s *SystemInfoImpl) dependencies() map[string]string {
|
||||
var pkgs = make(map[string]string)
|
||||
|
||||
// 打开 go.mod 文件
|
||||
file, err := os.Open("go.mod")
|
||||
if err != nil {
|
||||
return pkgs
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 使用 bufio.Scanner 逐行读取文件内容
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
line = strings.TrimSpace(line)
|
||||
|
||||
// 行不为空,不以module\require开头,不带有 // indirect 注释,则解析包名和版本
|
||||
prefixLine := strings.HasPrefix(line, "module") || strings.HasPrefix(line, "require") || strings.HasPrefix(line, "go ")
|
||||
suffixLine := strings.HasSuffix(line, ")") || strings.HasSuffix(line, "// indirect")
|
||||
if line == "" || prefixLine || suffixLine {
|
||||
continue
|
||||
}
|
||||
|
||||
modInfo := strings.Split(line, " ")
|
||||
if len(modInfo) >= 2 {
|
||||
moduleName := strings.TrimSpace(modInfo[0])
|
||||
version := strings.TrimSpace(modInfo[1])
|
||||
pkgs[moduleName] = version
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
pkgs["scanner-err"] = err.Error()
|
||||
}
|
||||
return pkgs
|
||||
}
|
||||
|
||||
// SystemInfo 系统信息
|
||||
func (s *SystemInfoImpl) SystemInfo() map[string]any {
|
||||
info, err := host.Info()
|
||||
if err != nil {
|
||||
info.Platform = err.Error()
|
||||
}
|
||||
// 用户目录
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
homeDir = ""
|
||||
}
|
||||
cmd, err := os.Executable()
|
||||
if err != nil {
|
||||
cmd = ""
|
||||
}
|
||||
// 获取主机运行时间
|
||||
bootTime := time.Since(time.Unix(int64(info.BootTime), 0)).Seconds()
|
||||
// 获取程序运行时间
|
||||
runTime := time.Since(config.RunTime()).Abs().Seconds()
|
||||
return map[string]any{
|
||||
"platform": info.Platform,
|
||||
"go": runtime.Version(),
|
||||
"processId": os.Getpid(),
|
||||
"arch": info.KernelArch,
|
||||
"uname": runtime.GOARCH,
|
||||
"release": info.OS,
|
||||
"hostname": info.Hostname,
|
||||
"homeDir": homeDir,
|
||||
"cmd": cmd,
|
||||
"execCommand": strings.Join(os.Args, " "),
|
||||
"platform": info.Platform,
|
||||
"platformVersion": info.PlatformVersion,
|
||||
"arch": info.KernelArch,
|
||||
"archVersion": info.KernelVersion,
|
||||
"os": info.OS,
|
||||
"hostname": info.Hostname,
|
||||
"bootTime": int64(bootTime),
|
||||
"processId": os.Getpid(),
|
||||
"runArch": runtime.GOARCH,
|
||||
"runVersion": runtime.Version(),
|
||||
"runTime": int64(runTime),
|
||||
}
|
||||
}
|
||||
|
||||
// TimeInfo 系统时间信息
|
||||
func (s *SystemInfoImpl) TimeInfo() map[string]string {
|
||||
now := time.Now()
|
||||
// 获取当前时间
|
||||
current := time.Now().Format("2006-01-02 15:04:05")
|
||||
// 获取程序运行时间
|
||||
uptime := time.Since(config.RunTime()).String()
|
||||
current := now.Format("2006-01-02 15:04:05")
|
||||
// 获取时区
|
||||
timezone := time.Now().Format("-0700 MST")
|
||||
timezone := now.Format("-0700 MST")
|
||||
// 获取时区名称
|
||||
timezoneName := time.Now().Format("MST")
|
||||
timezoneName := now.Format("MST")
|
||||
|
||||
return map[string]string{
|
||||
"current": current,
|
||||
"uptime": uptime,
|
||||
"timezone": timezone,
|
||||
"timezoneName": timezoneName,
|
||||
}
|
||||
@@ -153,12 +90,12 @@ func (s *SystemInfoImpl) MemoryInfo() map[string]any {
|
||||
|
||||
// CPUInfo CPU信息
|
||||
func (s *SystemInfoImpl) CPUInfo() map[string]any {
|
||||
var core int32 = 0
|
||||
var core int = 0
|
||||
var speed string = "未知"
|
||||
var model string = "未知"
|
||||
cpuInfo, err := cpu.Info()
|
||||
if err == nil {
|
||||
core = cpuInfo[0].Cores
|
||||
core = runtime.NumCPU()
|
||||
speed = fmt.Sprintf("%.0fMHz", cpuInfo[0].Mhz)
|
||||
model = strings.TrimSpace(cpuInfo[0].ModelName)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user