v2版本正式上线测试版
This commit is contained in:
128
V1/controllers/common.go
Normal file
128
V1/controllers/common.go
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* @Author: haodaquan
|
||||
* @Date: 2017-06-19 22:27:09
|
||||
* @Last Modified by: haodaquan
|
||||
* @Last Modified time: 2017-06-22 11:15:33
|
||||
*/
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/george518/PPGo_Job/libs"
|
||||
"github.com/george518/PPGo_Job/models"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
MSG_OK = 0
|
||||
MSG_ERR = -1
|
||||
)
|
||||
|
||||
type BaseController struct {
|
||||
beego.Controller
|
||||
controllerName string
|
||||
actionName string
|
||||
user *models.User
|
||||
userId int
|
||||
userName string
|
||||
pageSize int
|
||||
}
|
||||
|
||||
func (this *BaseController) Prepare() {
|
||||
this.pageSize = 20
|
||||
controllerName, actionName := this.GetControllerAndAction()
|
||||
this.controllerName = strings.ToLower(controllerName[0 : len(controllerName)-10])
|
||||
this.actionName = strings.ToLower(actionName)
|
||||
this.auth()
|
||||
|
||||
this.Data["version"] = beego.AppConfig.String("version")
|
||||
this.Data["siteName"] = beego.AppConfig.String("site.name")
|
||||
this.Data["curRoute"] = this.controllerName + "." + this.actionName
|
||||
this.Data["curController"] = this.controllerName
|
||||
this.Data["curAction"] = this.actionName
|
||||
this.Data["loginUserId"] = this.userId
|
||||
this.Data["loginUserName"] = this.userName
|
||||
this.Data["menuTag"] = this.controllerName
|
||||
}
|
||||
|
||||
//登录状态验证
|
||||
func (this *BaseController) auth() {
|
||||
arr := strings.Split(this.Ctx.GetCookie("auth"), "|")
|
||||
if len(arr) == 2 {
|
||||
idstr, password := arr[0], arr[1]
|
||||
userId, _ := strconv.Atoi(idstr)
|
||||
if userId > 0 {
|
||||
user, err := models.UserGetById(userId)
|
||||
if err == nil && password == libs.Md5([]byte(this.getClientIp()+"|"+user.Password+user.Salt)) {
|
||||
this.userId = user.Id
|
||||
this.userName = user.UserName
|
||||
this.user = user
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if this.userId == 0 && (this.controllerName != "main" ||
|
||||
(this.controllerName == "main" && this.actionName != "logout" && this.actionName != "login")) {
|
||||
this.redirect(beego.URLFor("MainController.Login"))
|
||||
}
|
||||
}
|
||||
|
||||
//渲染模版
|
||||
func (this *BaseController) display(tpl ...string) {
|
||||
var tplname string
|
||||
if len(tpl) > 0 {
|
||||
tplname = tpl[0] + ".html"
|
||||
} else {
|
||||
tplname = this.controllerName + "/" + this.actionName + ".html"
|
||||
}
|
||||
this.Layout = "public/layout.html"
|
||||
this.TplName = tplname
|
||||
}
|
||||
|
||||
// 重定向
|
||||
func (this *BaseController) redirect(url string) {
|
||||
this.Redirect(url, 302)
|
||||
this.StopRun()
|
||||
}
|
||||
|
||||
// 是否POST提交
|
||||
func (this *BaseController) isPost() bool {
|
||||
return this.Ctx.Request.Method == "POST"
|
||||
}
|
||||
|
||||
// 显示错误信息
|
||||
func (this *BaseController) showMsg(args ...string) {
|
||||
this.Data["message"] = args[0]
|
||||
redirect := this.Ctx.Request.Referer()
|
||||
if len(args) > 1 {
|
||||
redirect = args[1]
|
||||
}
|
||||
|
||||
this.Data["redirect"] = redirect
|
||||
this.Data["pageTitle"] = "系统提示"
|
||||
this.display("error/message")
|
||||
this.Render()
|
||||
this.StopRun()
|
||||
}
|
||||
|
||||
// 输出json
|
||||
func (this *BaseController) jsonResult(out interface{}) {
|
||||
this.Data["json"] = out
|
||||
this.ServeJSON()
|
||||
this.StopRun()
|
||||
}
|
||||
|
||||
func (this *BaseController) ajaxMsg(msg interface{}, msgno int) {
|
||||
out := make(map[string]interface{})
|
||||
out["status"] = msgno
|
||||
out["msg"] = msg
|
||||
|
||||
this.jsonResult(out)
|
||||
}
|
||||
|
||||
//获取用户IP地址
|
||||
func (this *BaseController) getClientIp() string {
|
||||
s := strings.Split(this.Ctx.Request.RemoteAddr, ":")
|
||||
return s[0]
|
||||
}
|
||||
99
V1/controllers/group.go
Normal file
99
V1/controllers/group.go
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* @Author: haodaquan
|
||||
* @Date: 2017-06-21 10:27:40
|
||||
* @Last Modified by: haodaquan
|
||||
* @Last Modified time: 2017-06-22 09:17:22
|
||||
*/
|
||||
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/george518/PPGo_Job/libs"
|
||||
"github.com/george518/PPGo_Job/models"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type GroupController struct {
|
||||
BaseController
|
||||
}
|
||||
|
||||
func (this *GroupController) List() {
|
||||
page, _ := this.GetInt("page")
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
list, count := models.TaskGroupGetList(page, this.pageSize)
|
||||
|
||||
this.Data["pageTitle"] = "分组列表"
|
||||
this.Data["list"] = list
|
||||
this.Data["pageBar"] = libs.NewPager(page, int(count), this.pageSize, beego.URLFor("GroupController.List"), true).ToString()
|
||||
this.display()
|
||||
}
|
||||
|
||||
func (this *GroupController) Add() {
|
||||
if this.isPost() {
|
||||
group := new(models.TaskGroup)
|
||||
group.GroupName = strings.TrimSpace(this.GetString("group_name"))
|
||||
group.UserId = this.userId
|
||||
group.Description = strings.TrimSpace(this.GetString("description"))
|
||||
group.CreateTime = time.Now().Unix()
|
||||
|
||||
_, err := models.TaskGroupAdd(group)
|
||||
if err != nil {
|
||||
this.ajaxMsg(err.Error(), MSG_ERR)
|
||||
}
|
||||
this.ajaxMsg("", MSG_OK)
|
||||
}
|
||||
|
||||
this.Data["pageTitle"] = "添加分组"
|
||||
this.display()
|
||||
}
|
||||
|
||||
func (this *GroupController) Edit() {
|
||||
id, _ := this.GetInt("id")
|
||||
|
||||
group, err := models.TaskGroupGetById(id)
|
||||
if err != nil {
|
||||
this.showMsg(err.Error())
|
||||
}
|
||||
|
||||
if this.isPost() {
|
||||
group.GroupName = strings.TrimSpace(this.GetString("group_name"))
|
||||
group.Description = strings.TrimSpace(this.GetString("description"))
|
||||
err := group.Update()
|
||||
if err != nil {
|
||||
this.ajaxMsg(err.Error(), MSG_ERR)
|
||||
}
|
||||
this.ajaxMsg("", MSG_OK)
|
||||
}
|
||||
|
||||
this.Data["pageTitle"] = "编辑分组"
|
||||
this.Data["group"] = group
|
||||
this.display()
|
||||
}
|
||||
|
||||
func (this *GroupController) Batch() {
|
||||
action := this.GetString("action")
|
||||
ids := this.GetStrings("ids")
|
||||
if len(ids) < 1 {
|
||||
this.ajaxMsg("请选择要操作的项目", MSG_ERR)
|
||||
}
|
||||
|
||||
for _, v := range ids {
|
||||
id, _ := strconv.Atoi(v)
|
||||
if id < 1 {
|
||||
continue
|
||||
}
|
||||
switch action {
|
||||
case "delete":
|
||||
models.TaskGroupDelById(id)
|
||||
models.TaskResetGroupId(id)
|
||||
}
|
||||
}
|
||||
|
||||
this.ajaxMsg("", MSG_OK)
|
||||
}
|
||||
18
V1/controllers/help.go
Normal file
18
V1/controllers/help.go
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* @Author: haodaquan
|
||||
* @Date: 2017-06-21 10:29:55
|
||||
* @Last Modified by: haodaquan
|
||||
* @Last Modified time: 2017-06-21 10:30:07
|
||||
*/
|
||||
|
||||
package controllers
|
||||
|
||||
type HelpController struct {
|
||||
BaseController
|
||||
}
|
||||
|
||||
func (this *HelpController) Index() {
|
||||
|
||||
this.Data["pageTitle"] = "使用帮助"
|
||||
this.display()
|
||||
}
|
||||
195
V1/controllers/main.go
Normal file
195
V1/controllers/main.go
Normal file
@@ -0,0 +1,195 @@
|
||||
/*
|
||||
* @Author: haodaquan
|
||||
* @Date: 2017-06-19 18:03:25
|
||||
* @Last Modified by: haodaquan
|
||||
* @Last Modified time: 2017-06-22 17:39:31
|
||||
*/
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/utils"
|
||||
"github.com/george518/PPGo_Job/jobs"
|
||||
"github.com/george518/PPGo_Job/libs"
|
||||
"github.com/george518/PPGo_Job/models"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MainController struct {
|
||||
BaseController
|
||||
}
|
||||
|
||||
// 首页
|
||||
func (this *MainController) Index() {
|
||||
this.Data["pageTitle"] = "系统概况"
|
||||
// 分组列表
|
||||
groups, _ := models.TaskGroupGetList(1, 100)
|
||||
groups_map := make(map[int]string)
|
||||
for _,gname := range groups {
|
||||
groups_map[gname.Id] = gname.GroupName
|
||||
}
|
||||
//计算总任务数量
|
||||
_, count := models.TaskGetList(1, 200)
|
||||
// 即将执行的任务
|
||||
entries := jobs.GetEntries(30)
|
||||
jobList := make([]map[string]interface{}, len(entries))
|
||||
startJob := 0 //即将执行的任务
|
||||
for k, v := range entries {
|
||||
row := make(map[string]interface{})
|
||||
job := v.Job.(*jobs.Job)
|
||||
task, _ := models.TaskGetById(job.GetId())
|
||||
row["task_id"] = job.GetId()
|
||||
row["task_name"] = job.GetName()
|
||||
row["task_group"] = groups_map[task.GroupId]
|
||||
row["next_time"] = beego.Date(v.Next, "Y-m-d H:i:s")
|
||||
jobList[k] = row
|
||||
startJob++
|
||||
}
|
||||
|
||||
// 最近执行的日志
|
||||
logs, _ := models.TaskLogGetList(1, 20)
|
||||
recentLogs := make([]map[string]interface{}, len(logs))
|
||||
failJob := 0 //最近失败的数量
|
||||
okJob:=0 //最近成功的数量
|
||||
for k, v := range logs {
|
||||
task, err := models.TaskGetById(v.TaskId)
|
||||
taskName := ""
|
||||
if err == nil {
|
||||
taskName = task.TaskName
|
||||
}
|
||||
row := make(map[string]interface{})
|
||||
row["task_name"] = taskName
|
||||
row["id"] = v.Id
|
||||
row["start_time"] = beego.Date(time.Unix(v.CreateTime, 0), "Y-m-d H:i:s")
|
||||
row["process_time"] = float64(v.ProcessTime) / 1000
|
||||
row["ouput_size"] = libs.SizeFormat(float64(len(v.Output)))
|
||||
row["output"] = beego.Substr(v.Output, 0, 100)
|
||||
row["status"] = v.Status
|
||||
recentLogs[k] = row
|
||||
if v.Status!=0 {
|
||||
failJob++
|
||||
}else {
|
||||
okJob++
|
||||
}
|
||||
}
|
||||
|
||||
// 最近执行失败的日志
|
||||
logs, _ = models.TaskLogGetList(1, 20, "status__lt", 0)
|
||||
errLogs := make([]map[string]interface{}, len(logs))
|
||||
|
||||
for k, v := range logs {
|
||||
task, err := models.TaskGetById(v.TaskId)
|
||||
taskName := ""
|
||||
if err == nil {
|
||||
taskName = task.TaskName
|
||||
}
|
||||
|
||||
row := make(map[string]interface{})
|
||||
row["task_name"] = taskName
|
||||
row["id"] = v.Id
|
||||
row["start_time"] = beego.Date(time.Unix(v.CreateTime, 0), "Y-m-d H:i:s")
|
||||
row["process_time"] = float64(v.ProcessTime) / 1000
|
||||
row["ouput_size"] = libs.SizeFormat(float64(len(v.Output)))
|
||||
row["error"] = beego.Substr(v.Error, 0, 100)
|
||||
row["status"] = v.Status
|
||||
errLogs[k] = row
|
||||
|
||||
}
|
||||
|
||||
this.Data["startJob"] = startJob
|
||||
this.Data["okJob"] = okJob
|
||||
this.Data["failJob"] = failJob
|
||||
this.Data["totalJob"] = count
|
||||
|
||||
this.Data["recentLogs"] = recentLogs
|
||||
// this.Data["errLogs"] = errLogs
|
||||
this.Data["jobs"] = jobList
|
||||
this.Data["cpuNum"] = runtime.NumCPU()
|
||||
this.display()
|
||||
}
|
||||
|
||||
//个人信息
|
||||
func (this *MainController) Profile() {
|
||||
beego.ReadFromRequest(&this.Controller)
|
||||
user, _ := models.UserGetById(this.userId)
|
||||
|
||||
if this.isPost() {
|
||||
user.Email = this.GetString("email")
|
||||
user.Update()
|
||||
password1 := this.GetString("password1")
|
||||
password2 := this.GetString("password2")
|
||||
if password1 != "" {
|
||||
if len(password1) < 6 {
|
||||
this.ajaxMsg("密码长度必须大于6位", MSG_ERR)
|
||||
} else if password2 != password1 {
|
||||
this.ajaxMsg("两次输入的密码不一致", MSG_ERR)
|
||||
} else {
|
||||
user.Salt = string(utils.RandomCreateBytes(10))
|
||||
user.Password = libs.Md5([]byte(password1 + user.Salt))
|
||||
user.Update()
|
||||
}
|
||||
}
|
||||
this.ajaxMsg("", MSG_OK)
|
||||
}
|
||||
|
||||
this.Data["pageTitle"] = "资料修改"
|
||||
this.Data["user"] = user
|
||||
this.display()
|
||||
}
|
||||
|
||||
// 登录
|
||||
func (this *MainController) Login() {
|
||||
if this.userId > 0 {
|
||||
this.redirect("/")
|
||||
}
|
||||
beego.ReadFromRequest(&this.Controller)
|
||||
if this.isPost() {
|
||||
|
||||
username := strings.TrimSpace(this.GetString("username"))
|
||||
password := strings.TrimSpace(this.GetString("password"))
|
||||
remember := this.GetString("remember")
|
||||
|
||||
if username != "" && password != "" {
|
||||
user, err := models.UserGetByName(username)
|
||||
flash := beego.NewFlash()
|
||||
errorMsg := ""
|
||||
if err != nil || user.Password != libs.Md5([]byte(password+user.Salt)) {
|
||||
errorMsg = "帐号或密码错误"
|
||||
} else if user.Status == -1 {
|
||||
errorMsg = "该帐号已禁用"
|
||||
} else {
|
||||
user.LastIp = this.getClientIp()
|
||||
user.LastLogin = time.Now().Unix()
|
||||
models.UserUpdate(user)
|
||||
|
||||
authkey := libs.Md5([]byte(this.getClientIp() + "|" + user.Password + user.Salt))
|
||||
if remember == "yes" {
|
||||
this.Ctx.SetCookie("auth", strconv.Itoa(user.Id)+"|"+authkey, 7*86400)
|
||||
} else {
|
||||
this.Ctx.SetCookie("auth", strconv.Itoa(user.Id)+"|"+authkey,86400)
|
||||
}
|
||||
this.redirect(beego.URLFor("TaskController.List"))
|
||||
}
|
||||
flash.Error(errorMsg)
|
||||
flash.Store(&this.Controller)
|
||||
this.redirect(beego.URLFor("MainController.Login"))
|
||||
}
|
||||
}
|
||||
this.TplName = "public/login.html"
|
||||
}
|
||||
|
||||
// 退出登录
|
||||
func (this *MainController) Logout() {
|
||||
this.Ctx.SetCookie("auth", "")
|
||||
this.redirect(beego.URLFor("MainController.Login"))
|
||||
}
|
||||
|
||||
// 获取系统时间
|
||||
func (this *MainController) GetTime() {
|
||||
out := make(map[string]interface{})
|
||||
out["time"] = time.Now().UnixNano() / int64(time.Millisecond)
|
||||
this.jsonResult(out)
|
||||
}
|
||||
137
V1/controllers/server.go
Normal file
137
V1/controllers/server.go
Normal file
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* @Author: haodaquan
|
||||
* @Date: 2017-08-16 10:27:40
|
||||
* @Last Modified by: haodaquan
|
||||
* @Last Modified time: 2017-08-16 09:17:22
|
||||
*/
|
||||
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/george518/PPGo_Job/libs"
|
||||
"github.com/george518/PPGo_Job/models"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ServerController struct {
|
||||
BaseController
|
||||
}
|
||||
|
||||
func (this *ServerController) List() {
|
||||
page, _ := this.GetInt("page")
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
result, count := models.TaskServerGetList(page, this.pageSize)
|
||||
list := make([]map[string]interface{}, len(result))
|
||||
for k, v := range result {
|
||||
row := make(map[string]interface{})
|
||||
row["id"] = v.Id
|
||||
if(v.Type==0){
|
||||
row["type"] = "密码"
|
||||
}else {
|
||||
row["type"] = "密钥"
|
||||
}
|
||||
row["server_name"] = v.ServerName
|
||||
row["server_ip"] = v.ServerIp
|
||||
row["detail"] = v.Detail
|
||||
row["port"] = v.Port
|
||||
row["create_time"] = beego.Date(time.Unix(v.CreateTime, 0), "Y-m-d H:i:s")
|
||||
list[k] = row
|
||||
}
|
||||
this.Data["pageTitle"] = "服务器列表"
|
||||
this.Data["list"] = list
|
||||
this.Data["pageBar"] = libs.NewPager(page, int(count), this.pageSize, beego.URLFor("ServerController.List"), true).ToString()
|
||||
this.display()
|
||||
}
|
||||
|
||||
func (this *ServerController) Add() {
|
||||
if this.isPost() {
|
||||
server := new(models.TaskServer)
|
||||
server.ServerName = strings.TrimSpace(this.GetString("server_name"))
|
||||
server.ServerAccount = strings.TrimSpace(this.GetString("server_account"))
|
||||
server.ServerIp = strings.TrimSpace(this.GetString("server_ip"))
|
||||
server.Port,_= strconv.Atoi(this.GetString("port"))
|
||||
server.Type,_ = strconv.Atoi(this.GetString("type"))
|
||||
server.PrivateKeySrc = strings.TrimSpace(this.GetString("private_key_src"))
|
||||
server.PublicKeySrc = strings.TrimSpace(this.GetString("public_key_src"))
|
||||
server.Password = strings.TrimSpace(this.GetString("password"))
|
||||
server.Detail = strings.TrimSpace(this.GetString("detail"))
|
||||
server.CreateTime = time.Now().Unix()
|
||||
server.UpdateTime = time.Now().Unix()
|
||||
server.Status = 0
|
||||
_, err := models.TaskServerAdd(server)
|
||||
if err != nil {
|
||||
this.ajaxMsg(err.Error(), MSG_ERR)
|
||||
}
|
||||
this.ajaxMsg("", MSG_OK)
|
||||
}
|
||||
this.Data["pageTitle"] = "添加服务器"
|
||||
this.display()
|
||||
}
|
||||
|
||||
func (this *ServerController) Edit() {
|
||||
id, _ := this.GetInt("id")
|
||||
server, err := models.TaskServerGetById(id)
|
||||
if err != nil {
|
||||
this.showMsg(err.Error())
|
||||
}
|
||||
|
||||
if this.isPost() {
|
||||
server.ServerName = strings.TrimSpace(this.GetString("server_name"))
|
||||
server.ServerAccount = strings.TrimSpace(this.GetString("server_account"))
|
||||
server.ServerIp = strings.TrimSpace(this.GetString("server_ip"))
|
||||
server.Port,_ = strconv.Atoi(this.GetString("port"))
|
||||
server.Type,_ = strconv.Atoi(this.GetString("type"))
|
||||
server.Id,_ = strconv.Atoi(this.GetString("id"))
|
||||
server.PrivateKeySrc = strings.TrimSpace(this.GetString("private_key_src"))
|
||||
server.PublicKeySrc = strings.TrimSpace(this.GetString("public_key_src"))
|
||||
server.Password = strings.TrimSpace(this.GetString("password"))
|
||||
server.Detail = strings.TrimSpace(this.GetString("detail"))
|
||||
server.UpdateTime = time.Now().Unix()
|
||||
server.Status = 0
|
||||
err := server.Update()
|
||||
if err != nil {
|
||||
this.ajaxMsg(err.Error(), MSG_ERR)
|
||||
}
|
||||
this.ajaxMsg("", MSG_OK)
|
||||
}
|
||||
|
||||
this.Data["pageTitle"] = "编辑服务器"
|
||||
this.Data["server"] = server
|
||||
this.display()
|
||||
}
|
||||
|
||||
//TODO删除更新
|
||||
func (this *ServerController) Batch() {
|
||||
action := this.GetString("action")
|
||||
ids := this.GetStrings("ids")
|
||||
if len(ids) < 1 {
|
||||
this.ajaxMsg("请选择要操作的项目", MSG_ERR)
|
||||
}
|
||||
|
||||
for _, v := range ids {
|
||||
id, _ := strconv.Atoi(v)
|
||||
if id < 1 {
|
||||
continue
|
||||
}
|
||||
switch action {
|
||||
case "delete":
|
||||
//查询服务器是否被占用
|
||||
filters := make([]interface{}, 0)
|
||||
filters = append(filters, "server_id", id)
|
||||
_, count := models.TaskGetList(1, 1000, filters...)
|
||||
if count > 0 {
|
||||
this.ajaxMsg("请先解除该服务器的任务占用", MSG_ERR)
|
||||
}else{
|
||||
models.TaskServerDelById(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.ajaxMsg("", MSG_OK)
|
||||
}
|
||||
395
V1/controllers/task.go
Normal file
395
V1/controllers/task.go
Normal file
@@ -0,0 +1,395 @@
|
||||
/*
|
||||
* @Author: haodaquan
|
||||
* @Date: 2017-06-21 10:22:29
|
||||
* @Last Modified by: haodaquan
|
||||
* @Last Modified time: 2017-06-23 11:04:54
|
||||
*/
|
||||
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
crons "github.com/george518/PPGo_Job/crons"
|
||||
"github.com/george518/PPGo_Job/jobs"
|
||||
"github.com/george518/PPGo_Job/libs"
|
||||
"github.com/george518/PPGo_Job/models"
|
||||
)
|
||||
|
||||
type TaskController struct {
|
||||
BaseController
|
||||
}
|
||||
|
||||
// 任务列表
|
||||
func (this *TaskController) List() {
|
||||
page, _ := this.GetInt("page")
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
groupId, _ := this.GetInt("groupid")
|
||||
if groupId > 0 {
|
||||
this.Ctx.SetCookie("groupid", strconv.Itoa(groupId)+"|job")
|
||||
} else {
|
||||
arr := strings.Split(this.Ctx.GetCookie("groupid"), "|")
|
||||
groupId, _ = strconv.Atoi(arr[0])
|
||||
}
|
||||
|
||||
filters := make([]interface{}, 0)
|
||||
if groupId > 0 && groupId != 99 {
|
||||
filters = append(filters, "group_id", groupId)
|
||||
}
|
||||
result, count := models.TaskGetList(page, this.pageSize, filters...)
|
||||
|
||||
list := make([]map[string]interface{}, len(result))
|
||||
|
||||
// 分组列表
|
||||
groups, _ := models.TaskGroupGetList(1, 100)
|
||||
groups_map := make(map[int]string)
|
||||
for _, gname := range groups {
|
||||
groups_map[gname.Id] = gname.GroupName
|
||||
}
|
||||
|
||||
//服务器列表
|
||||
servers, _ := models.TaskServerGetList(1, 100)
|
||||
|
||||
server_map := make(map[int]string)
|
||||
for _, sname := range servers {
|
||||
server_map[sname.Id] = sname.ServerName
|
||||
}
|
||||
server_map[0] = "本地"
|
||||
for k, v := range result {
|
||||
row := make(map[string]interface{})
|
||||
row["id"] = v.Id
|
||||
row["name"] = v.TaskName
|
||||
row["cron_spec"] = v.CronSpec
|
||||
row["status"] = v.Status
|
||||
row["description"] = v.Description
|
||||
row["group_id"] = v.GroupId
|
||||
row["group_name"] = groups_map[v.GroupId]
|
||||
row["server_name"] = server_map[v.ServerId]
|
||||
row["is_odd"] = k % 2
|
||||
|
||||
e := jobs.GetEntryById(v.Id)
|
||||
if e != nil {
|
||||
row["next_time"] = beego.Date(e.Next, "Y-m-d H:i:s")
|
||||
row["prev_time"] = "-"
|
||||
if e.Prev.Unix() > 0 {
|
||||
row["prev_time"] = beego.Date(e.Prev, "Y-m-d H:i:s")
|
||||
} else if v.PrevTime > 0 {
|
||||
row["prev_time"] = beego.Date(time.Unix(v.PrevTime, 0), "Y-m-d H:i:s")
|
||||
}
|
||||
row["running"] = 1
|
||||
} else {
|
||||
row["next_time"] = "-"
|
||||
if v.PrevTime > 0 {
|
||||
row["prev_time"] = beego.Date(time.Unix(v.PrevTime, 0), "Y-m-d H:i:s")
|
||||
} else {
|
||||
row["prev_time"] = "-"
|
||||
}
|
||||
row["running"] = 0
|
||||
}
|
||||
list[k] = row
|
||||
}
|
||||
|
||||
this.Data["pageTitle"] = "任务列表"
|
||||
this.Data["list"] = list
|
||||
this.Data["groups"] = groups
|
||||
this.Data["groupid"] = groupId
|
||||
this.Data["pageBar"] = libs.NewPager(page, int(count), this.pageSize, beego.URLFor("TaskController.List", "groupid", groupId), true).ToString()
|
||||
this.display()
|
||||
}
|
||||
|
||||
// 添加任务
|
||||
func (this *TaskController) Add() {
|
||||
|
||||
if this.isPost() {
|
||||
task := new(models.Task)
|
||||
task.UserId = this.userId
|
||||
task.GroupId, _ = this.GetInt("group_id")
|
||||
task.TaskName = strings.TrimSpace(this.GetString("task_name"))
|
||||
task.Description = strings.TrimSpace(this.GetString("description"))
|
||||
task.Concurrent, _ = this.GetInt("concurrent")
|
||||
task.ServerId, _ = this.GetInt("server_id")
|
||||
task.CronSpec = strings.TrimSpace(this.GetString("cron_spec"))
|
||||
task.Command = strings.TrimSpace(this.GetString("command"))
|
||||
task.Timeout, _ = this.GetInt("timeout")
|
||||
|
||||
if task.TaskName == "" || task.CronSpec == "" || task.Command == "" {
|
||||
this.ajaxMsg("请填写完整信息", MSG_ERR)
|
||||
}
|
||||
if _, err := crons.Parse(task.CronSpec); err != nil {
|
||||
this.ajaxMsg("cron表达式无效", MSG_ERR)
|
||||
}
|
||||
if _, err := models.TaskAdd(task); err != nil {
|
||||
this.ajaxMsg(err.Error(), MSG_ERR)
|
||||
}
|
||||
|
||||
this.ajaxMsg("", MSG_OK)
|
||||
}
|
||||
|
||||
// 分组列表
|
||||
groups, _ := models.TaskGroupGetList(1, 100)
|
||||
this.Data["groups"] = groups
|
||||
//服务器分组
|
||||
servers, _ := models.TaskServerGetList(1, 100)
|
||||
this.Data["servers"] = servers
|
||||
this.Data["pageTitle"] = "添加任务"
|
||||
this.display()
|
||||
}
|
||||
|
||||
// 编辑任务
|
||||
func (this *TaskController) Edit() {
|
||||
id, _ := this.GetInt("id")
|
||||
|
||||
task, err := models.TaskGetById(id)
|
||||
if err != nil {
|
||||
this.showMsg(err.Error())
|
||||
}
|
||||
|
||||
if task.Status != 0 {
|
||||
this.ajaxMsg("激活状态无法编辑任务,请先暂停任务", MSG_ERR)
|
||||
}
|
||||
|
||||
if this.isPost() {
|
||||
task.TaskName = strings.TrimSpace(this.GetString("task_name"))
|
||||
task.Description = strings.TrimSpace(this.GetString("description"))
|
||||
task.GroupId, _ = this.GetInt("group_id")
|
||||
task.Concurrent, _ = this.GetInt("concurrent")
|
||||
task.ServerId, _ = this.GetInt("server_id")
|
||||
task.CronSpec = strings.TrimSpace(this.GetString("cron_spec"))
|
||||
task.Command = strings.TrimSpace(this.GetString("command"))
|
||||
task.Timeout, _ = this.GetInt("timeout")
|
||||
if task.TaskName == "" || task.CronSpec == "" || task.Command == "" {
|
||||
this.ajaxMsg("请填写完整信息", MSG_ERR)
|
||||
}
|
||||
if _, err := crons.Parse(task.CronSpec); err != nil {
|
||||
this.ajaxMsg("cron表达式无效", MSG_ERR)
|
||||
}
|
||||
if err := task.Update(); err != nil {
|
||||
this.ajaxMsg(err.Error(), MSG_ERR)
|
||||
}
|
||||
|
||||
this.ajaxMsg("", MSG_OK)
|
||||
}
|
||||
|
||||
// 分组列表
|
||||
groups, _ := models.TaskGroupGetList(1, 100)
|
||||
this.Data["groups"] = groups
|
||||
//服务器分组
|
||||
servers, _ := models.TaskServerGetList(1, 100)
|
||||
this.Data["servers"] = servers
|
||||
|
||||
this.Data["task"] = task
|
||||
this.Data["pageTitle"] = "编辑任务"
|
||||
this.display()
|
||||
}
|
||||
|
||||
//复制任务
|
||||
func (this *TaskController) Copy() {
|
||||
|
||||
id, _ := this.GetInt("id")
|
||||
task, err := models.TaskGetById(id)
|
||||
if err != nil {
|
||||
this.showMsg(err.Error())
|
||||
}
|
||||
// 分组列表
|
||||
groups, _ := models.TaskGroupGetList(1, 100)
|
||||
this.Data["groups"] = groups
|
||||
//服务器分组
|
||||
servers, _ := models.TaskServerGetList(1, 100)
|
||||
this.Data["servers"] = servers
|
||||
|
||||
this.Data["task"] = task
|
||||
this.Data["pageTitle"] = "复制任务"
|
||||
this.display()
|
||||
}
|
||||
|
||||
// 任务执行日志列表
|
||||
func (this *TaskController) Logs() {
|
||||
taskId, _ := this.GetInt("id")
|
||||
page, _ := this.GetInt("page")
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
task, err := models.TaskGetById(taskId)
|
||||
if err != nil {
|
||||
this.showMsg(err.Error())
|
||||
}
|
||||
|
||||
result, count := models.TaskLogGetList(page, this.pageSize, "task_id", task.Id)
|
||||
|
||||
list := make([]map[string]interface{}, len(result))
|
||||
for k, v := range result {
|
||||
row := make(map[string]interface{})
|
||||
row["id"] = v.Id
|
||||
row["start_time"] = beego.Date(time.Unix(v.CreateTime, 0), "Y-m-d H:i:s")
|
||||
row["process_time"] = float64(v.ProcessTime) / 1000
|
||||
row["ouput_size"] = libs.SizeFormat(float64(len(v.Output)))
|
||||
row["status"] = v.Status
|
||||
list[k] = row
|
||||
}
|
||||
|
||||
this.Data["pageTitle"] = "任务执行日志"
|
||||
this.Data["list"] = list
|
||||
this.Data["task"] = task
|
||||
this.Data["pageBar"] = libs.NewPager(page, int(count), this.pageSize, beego.URLFor("TaskController.Logs", "id", taskId), true).ToString()
|
||||
this.display()
|
||||
}
|
||||
|
||||
// 查看日志详情
|
||||
func (this *TaskController) ViewLog() {
|
||||
id, _ := this.GetInt("id")
|
||||
|
||||
taskLog, err := models.TaskLogGetById(id)
|
||||
if err != nil {
|
||||
this.showMsg(err.Error())
|
||||
}
|
||||
|
||||
task, err := models.TaskGetById(taskLog.TaskId)
|
||||
if err != nil {
|
||||
this.showMsg(err.Error())
|
||||
}
|
||||
|
||||
data := make(map[string]interface{})
|
||||
data["id"] = taskLog.Id
|
||||
data["output"] = taskLog.Output
|
||||
data["error"] = taskLog.Error
|
||||
data["start_time"] = beego.Date(time.Unix(taskLog.CreateTime, 0), "Y-m-d H:i:s")
|
||||
data["process_time"] = float64(taskLog.ProcessTime) / 1000
|
||||
data["ouput_size"] = libs.SizeFormat(float64(len(taskLog.Output)))
|
||||
data["status"] = taskLog.Status
|
||||
|
||||
this.Data["task"] = task
|
||||
this.Data["data"] = data
|
||||
this.Data["pageTitle"] = "查看日志"
|
||||
this.display()
|
||||
}
|
||||
|
||||
// 批量操作日志
|
||||
func (this *TaskController) LogBatch() {
|
||||
action := this.GetString("action")
|
||||
ids := this.GetStrings("ids")
|
||||
if len(ids) < 1 {
|
||||
this.ajaxMsg("请选择要操作的项目", MSG_ERR)
|
||||
}
|
||||
for _, v := range ids {
|
||||
id, _ := strconv.Atoi(v)
|
||||
if id < 1 {
|
||||
continue
|
||||
}
|
||||
switch action {
|
||||
case "delete":
|
||||
models.TaskLogDelById(id)
|
||||
}
|
||||
}
|
||||
|
||||
this.ajaxMsg("", MSG_OK)
|
||||
}
|
||||
|
||||
// 批量操作
|
||||
func (this *TaskController) Batch() {
|
||||
action := this.GetString("action")
|
||||
ids := this.GetStrings("ids")
|
||||
if len(ids) < 1 {
|
||||
this.ajaxMsg("请选择要操作的项目", MSG_ERR)
|
||||
}
|
||||
|
||||
for _, v := range ids {
|
||||
id, _ := strconv.Atoi(v)
|
||||
if id < 1 {
|
||||
continue
|
||||
}
|
||||
switch action {
|
||||
case "active":
|
||||
if task, err := models.TaskGetById(id); err == nil {
|
||||
job, err := jobs.NewJobFromTask(task)
|
||||
if err == nil {
|
||||
jobs.AddJob(task.CronSpec, job)
|
||||
task.Status = 1
|
||||
task.Update()
|
||||
}
|
||||
}
|
||||
case "pause":
|
||||
jobs.RemoveJob(id)
|
||||
|
||||
if task, err := models.TaskGetById(id); err == nil {
|
||||
task.Status = 0
|
||||
task.Update()
|
||||
}
|
||||
|
||||
case "delete":
|
||||
models.TaskDel(id)
|
||||
models.TaskLogDelByTaskId(id)
|
||||
jobs.RemoveJob(id)
|
||||
}
|
||||
}
|
||||
|
||||
this.ajaxMsg("", MSG_OK)
|
||||
}
|
||||
|
||||
// 启动任务
|
||||
func (this *TaskController) Start() {
|
||||
id, _ := this.GetInt("id")
|
||||
|
||||
task, err := models.TaskGetById(id)
|
||||
if err != nil {
|
||||
this.showMsg(err.Error())
|
||||
}
|
||||
|
||||
job, err := jobs.NewJobFromTask(task)
|
||||
if err != nil {
|
||||
this.showMsg(err.Error())
|
||||
}
|
||||
|
||||
if jobs.AddJob(task.CronSpec, job) {
|
||||
task.Status = 1
|
||||
task.Update()
|
||||
}
|
||||
|
||||
refer := this.Ctx.Request.Referer()
|
||||
if refer == "" {
|
||||
refer = beego.URLFor("TaskController.List")
|
||||
}
|
||||
this.redirect(refer)
|
||||
}
|
||||
|
||||
// 暂停任务
|
||||
func (this *TaskController) Pause() {
|
||||
id, _ := this.GetInt("id")
|
||||
|
||||
task, err := models.TaskGetById(id)
|
||||
if err != nil {
|
||||
this.showMsg(err.Error())
|
||||
}
|
||||
|
||||
jobs.RemoveJob(id)
|
||||
task.Status = 0
|
||||
task.Update()
|
||||
|
||||
refer := this.Ctx.Request.Referer()
|
||||
if refer == "" {
|
||||
refer = beego.URLFor("TaskController.List")
|
||||
}
|
||||
this.redirect(refer)
|
||||
}
|
||||
|
||||
// 立即执行
|
||||
func (this *TaskController) Run() {
|
||||
id, _ := this.GetInt("id")
|
||||
|
||||
task, err := models.TaskGetById(id)
|
||||
if err != nil {
|
||||
this.showMsg(err.Error())
|
||||
}
|
||||
|
||||
job, err := jobs.NewJobFromTask(task)
|
||||
if err != nil {
|
||||
this.showMsg(err.Error())
|
||||
}
|
||||
job.Run()
|
||||
this.redirect(beego.URLFor("TaskController.ViewLog", "id", job.GetLogId()))
|
||||
}
|
||||
Reference in New Issue
Block a user