From 31c5e57f7cd3fd5b9f0aaab85cba48d0731a8048 Mon Sep 17 00:00:00 2001 From: georgehao Date: Wed, 20 Mar 2019 00:39:34 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=92=8C=E6=89=A7=E8=A1=8C=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/common.go | 9 ++- controllers/task.go | 144 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 147 insertions(+), 6 deletions(-) diff --git a/controllers/common.go b/controllers/common.go index c36d94a..451b4e7 100644 --- a/controllers/common.go +++ b/controllers/common.go @@ -78,7 +78,7 @@ func (self *BaseController) Auth() { } isHasAuth := strings.Contains(self.allowUrl, self.controllerName+"/"+self.actionName) - noAuth := "ajaxsave/table/loginin/loginout/getnodes/start" + noAuth := "ajaxsave/table/loginin/loginout/getnodes/start/apitask/apistart/apipause" isNoAuth := strings.Contains(noAuth, self.actionName) if isHasAuth == false && isNoAuth == false { @@ -95,7 +95,12 @@ func (self *BaseController) Auth() { } } - if self.userId == 0 && (self.controllerName != "login" && self.actionName != "loginin") { + if self.userId == 0 && + (self.controllerName != "login" && + self.actionName != "loginin" && + self.actionName != "apistart" && + self.actionName != "apitask" && + self.actionName != "apipause") { self.redirect(beego.URLFor("LoginController.Login")) } } diff --git a/controllers/task.go b/controllers/task.go index 18569fe..eca4798 100644 --- a/controllers/task.go +++ b/controllers/task.go @@ -12,8 +12,6 @@ import ( "strings" "time" - "fmt" - "github.com/astaxie/beego" "github.com/george518/PPGo_Job/crons" "github.com/george518/PPGo_Job/jobs" @@ -46,8 +44,6 @@ func (self *TaskController) Add() { self.Data["serverGroup"] = serverLists(self.serverGroups, self.userId) self.Data["isAdmin"] = self.userId self.Data["adminInfo"] = AllAdminInfo("") - - fmt.Println(self.Data["adminInfo"]) self.display() } @@ -295,6 +291,8 @@ func (self *TaskController) AjaxSave() { self.ajaxMsg("", MSG_OK) } + + //检查是否含有禁用命令 func checkCommand(command string) (string, bool) { @@ -334,6 +332,8 @@ func (self *TaskController) AjaxNopass() { self.ajaxMsg("", MSG_OK) } + + func (self *TaskController) AjaxStart() { taskId, _ := self.GetInt("id") if taskId == 0 { @@ -656,3 +656,139 @@ func (self *TaskController) Table() { self.ajaxList("成功", MSG_OK, count, list) } + +func (self *TaskController) ApiTask() { + task_id, _ := self.GetInt("id") + if task_id == 0 { + task := new(models.Task) + task.CreateId,_ = self.GetInt("create_id") + task.GroupId, _ = self.GetInt("group_id") + task.TaskName = strings.TrimSpace(self.GetString("task_name")) + task.Description = strings.TrimSpace(self.GetString("description")) + task.Concurrent, _ = self.GetInt("concurrent") + task.ServerId, _ = self.GetInt("server_id") + task.CronSpec = strings.TrimSpace(self.GetString("cron_spec")) + task.Command = strings.TrimSpace(self.GetString("command")) + task.Timeout, _ = self.GetInt("timeout") + task.IsNotify, _ = self.GetInt("is_notify") + task.NotifyType, _ = self.GetInt("notify_type") + task.NotifyTplId, _ = self.GetInt("notify_tpl_id") + task.NotifyUserIds = strings.TrimSpace(self.GetString("notify_user_ids")) + + if task.IsNotify == 1 && task.NotifyTplId <= 0 { + self.ajaxMsg("请选择通知模板", MSG_ERR) + } + + msg, isBan := checkCommand(task.Command) + if !isBan { + self.ajaxMsg("含有禁止命令:"+msg, MSG_ERR) + } + + task.CreateTime = time.Now().Unix() + task.UpdateTime = time.Now().Unix() + task.Status = 0 //接口不需要审核 + + if task.TaskName == "" || task.CronSpec == "" || task.Command == "" { + self.ajaxMsg("请填写完整信息", MSG_ERR) + } + var id int64 + var err error + if _, err = cron.Parse(task.CronSpec); err != nil { + self.ajaxMsg("cron表达式无效", MSG_ERR) + } + + if id, err = models.TaskAdd(task); err != nil { + self.ajaxMsg(err.Error(), MSG_ERR) + } + task_id = int(id) + self.ajaxMsg(task_id, MSG_OK) + } + + task, _ := models.TaskGetById(task_id) + + if task.Status == 1 { + self.ajaxMsg("运行状态无法编辑任务,请先暂停任务", MSG_ERR) + } + //修改 + task.Id = task_id + task.UpdateTime = time.Now().Unix() + task.TaskName = strings.TrimSpace(self.GetString("task_name")) + task.Description = strings.TrimSpace(self.GetString("description")) + task.GroupId, _ = self.GetInt("group_id") + task.Concurrent, _ = self.GetInt("concurrent") + task.ServerId, _ = self.GetInt("server_id") + task.CronSpec = strings.TrimSpace(self.GetString("cron_spec")) + task.Command = strings.TrimSpace(self.GetString("command")) + task.Timeout, _ = self.GetInt("timeout") + task.IsNotify, _ = self.GetInt("is_notify") + task.NotifyType, _ = self.GetInt("notify_type") + task.NotifyTplId, _ = self.GetInt("notify_tpl_id") + task.NotifyUserIds = strings.TrimSpace(self.GetString("notify_user_ids")) + task.UpdateId , _ = self.GetInt("update_id") + task.Status = 0 //接口不需要 + + if task.IsNotify == 1 && task.NotifyTplId <= 0 { + self.ajaxMsg("请选择通知模板", MSG_ERR) + } + + msg, isBan := checkCommand(task.Command) + if !isBan { + self.ajaxMsg("含有禁止命令:"+msg, MSG_ERR) + } + + if _, err := cron.Parse(task.CronSpec); err != nil { + self.ajaxMsg("cron表达式无效", MSG_ERR) + } + + if err := task.Update(); err != nil { + self.ajaxMsg(err.Error(), MSG_ERR) + } + self.ajaxMsg(task_id, MSG_OK) +} + +func (self *TaskController) ApiStart() { + taskId, _ := self.GetInt("id") + if taskId == 0 { + self.ajaxMsg("任务不存在", MSG_ERR) + } + + task, err := models.TaskGetById(taskId) + if err != nil { + self.ajaxMsg("查不到该任务", MSG_ERR) + } + + if task.Status != 0 { + self.ajaxMsg("任务状态有误", MSG_ERR) + } + + job, err := jobs.NewJobFromTask(task) + if err != nil { + self.ajaxMsg("创建任务失败", MSG_ERR) + } + + if jobs.AddJob(task.CronSpec, job) { + task.Status = 1 + task.Update() + } + self.ajaxMsg("", MSG_OK) +} + +func (self *TaskController) ApiPause() { + taskId, _ := self.GetInt("id") + if taskId == 0 { + self.ajaxMsg("任务不存在", MSG_ERR) + } + + task, err := models.TaskGetById(taskId) + if err != nil { + self.ajaxMsg("查不到该任务", MSG_ERR) + } + + jobs.RemoveJob(taskId) + task.Status = 0 + task.Update() + self.ajaxMsg("", MSG_OK) + +} + + From 8e4535d685e5d57561feb9a90b06bae6cd9ee874 Mon Sep 17 00:00:00 2001 From: georgehao Date: Thu, 21 Mar 2019 22:04:40 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ffc7280..e60b3fb 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ PPGo_Job定时任务管理系统 V2.0 PPGo_Job是一款定时任务可视化的、多人多权限的管理系统,采用golang开发,安装方便,资源消耗少,支持大并发,可同时管理多台服务器上的定时任务。 -前言:PPGo_Job V1.x版本开源一年多,好几个朋友的公司都在用,反响还不错,当然,也有好多朋友提了不少合理的意见和建议,所以这次干脆重构了一下,连UI也重新编码。目前V2.0版本 +前言:PPGo_Job V1.x版本开源两年多了,好几个朋友的公司都在用,反响还不错,当然,也有好多朋友提了不少合理的意见和建议,所以这次干脆重构了一下,连UI也重新编码。目前V2.0版本 已经用于生产环境。 码云地址:https://gitee.com/georgehao/PPGo_Job @@ -22,8 +22,14 @@ V1.x版本是一个简单的定时任务管理系统,进入V1.0 :https://git - 3、新增服务器复制功能,让服务器资源添加更加方便。 - 4、新增定时任务详情页面,将任务相关操作更加集中起来操作。 - 5、新增任务审核功能,提高任务的管控能力。 -- 6、新增禁止命令管理功能,配合任务审核功能,提高任务运行安全性。 -- 7、优化日志详情页面,查看日志更方便。 +- 6、新增禁止命令管理功能,配合任务审核功能,提高任务运行安全性,总有人会犯错。 +- 7、优化日志详情页面,查看日志更方便,你想看到的现场内容都在,亲。 +- 8、支持docker化部署,这年头,系统不支持docker化部署好像不能出门似的。 +- 9、支持windows系统运行定时系统,不歧视windows,也要支持。 +- 10、提醒信息新增钉钉和微信通知功能,并支持编辑通知模版功能。让提醒内容个性化,不再死板。 +- 11、新增创建、启动、关闭任务的API,通过接口的方式控制定时任务,猜你喜欢。 + +总之,管理定时任务,使用PPGo_Job吧,节省出来的时间,或皮或浪,随你,哈哈。 感觉不错的话,给个星星吧 :) @@ -173,10 +179,79 @@ docker-compose logs -f web 控制面板->管理工具->计算机管理->系统工具->本地用户和组->组->TelnetClients->添加用户 +任务接口说明 +---- +三个简陋的接口,满足日常所需。 + +1、新增和修改任务接口 + +- url:/task/apitask +- method:post +- params: +``` +id:0 +create_id:4 +group_id:3 +task_name:测试API创建任务 +description:测试 +concurrent:0 +server_id:2 +cron_spec:*/2 * * * * +command:free -G +timeout:0 +is_notify:0 +notify_type:0 +notify_tpl_id:0 +notify_user_ids:0 +``` + +参数含义详见数据库字段。 +需要注意的是id为0为新增,大于0为修改。 + +2、任务启动接口 + +- url:/task/apistart +- method:post +- params: + +``` +id:11 +``` + +3、任务暂停接口 + +- url:/task/apipause +- method:post +- params: + + ``` + id:11 + ``` + + +注意使用 form-data的方式传参 + +4、返回json,status=0表示成功,其他为失败,msg是错误理由或id + +``` +{ + "message": 11, + "status": 0 +} +``` + +具体可以使用postman测试 + 联系我 ---- qq群号:547564773 欢迎交流,欢迎提交代码。 +感谢 +---- +@bannerchi +@linxiaozhi +@gongwalker + From d4463c215d0e133e7e2d08f73f2e5cc9dc792185 Mon Sep 17 00:00:00 2001 From: georgehao Date: Thu, 21 Mar 2019 22:08:21 +0800 Subject: [PATCH 3/3] Update README.md --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e60b3fb..d2668ad 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,11 @@ -PPGo_Job定时任务管理系统 V2.0 +PPGo_Job定时任务管理系统 V2.x ==== + ![](http://www.haodaquan.com/Uploads/article/2018-07-26/153262059813931.png) PPGo_Job是一款定时任务可视化的、多人多权限的管理系统,采用golang开发,安装方便,资源消耗少,支持大并发,可同时管理多台服务器上的定时任务。 -前言:PPGo_Job V1.x版本开源两年多了,好几个朋友的公司都在用,反响还不错,当然,也有好多朋友提了不少合理的意见和建议,所以这次干脆重构了一下,连UI也重新编码。目前V2.0版本 +前言:PPGo_Job V1.x版本开源两年多了,不少朋友的公司都在用,反响还不错,当然,也有好多朋友提了不少合理的意见和建议,所以这次干脆重构了一下,连UI也重新编码。目前V2.x版本 已经用于生产环境。 码云地址:https://gitee.com/georgehao/PPGo_Job @@ -64,21 +65,21 @@ V1.x版本是一个简单的定时任务管理系统,进入V1.0 :https://git linux - 进入 https://github.com/george518/PPGo_Job/releases -- 下载 ppgo_job-linux-2.1.0.zip 并解压 +- 下载 ppgo_job-linux-2.x.0.zip 并解压 - 进入文件夹,设置好数据库(创建数据库,导入ppgo_job2.sql)和配置文件(conf/app.conf) - 运行 ./run.sh start|stop mac - 进入https://github.com/george518/PPGo_Job/releases -- 下载 ppgo_job-mac-2.1.0.zip 并解压 +- 下载 ppgo_job-mac-2.x.0.zip 并解压 - 进入文件夹,设置好数据库(创建数据库,导入ppgo_job2.sql)和配置文件(conf/app.conf) - 运行 ./run.sh start|stop windows - 进入 https://github.com/george518/PPGo_Job/releases -- 下载 ppgo_job-linux-2.1.0.zip 并解压 +- 下载 ppgo_job-linux-2.x.0.zip 并解压 - 进入文件夹,设置好数据库(创建数据库,导入ppgo_job2.sql)和配置文件(conf/app.conf) - 运行 go build - 运行 run.bat