新增任务分类,杀死进程等

常驻任务和定时任务
常驻任务暂停时杀死进程
This commit is contained in:
郝大全
2017-06-30 15:49:33 +08:00
parent fbd1f9e1a7
commit 4d8e647523
10 changed files with 160 additions and 17 deletions

View File

@@ -16,6 +16,9 @@ import (
"strconv"
"strings"
"time"
"os/exec"
"log"
"os"
)
type TaskController struct {
@@ -44,6 +47,8 @@ func (this *TaskController) List() {
row["status"] = v.Status
row["description"] = v.Description
row["task_type"] = v.TaskType
e := jobs.GetEntryById(v.Id)
if e != nil {
row["next_time"] = beego.Date(e.Next, "Y-m-d H:i:s")
@@ -85,6 +90,8 @@ func (this *TaskController) Add() {
task.UserId = this.userId
task.GroupId, _ = this.GetInt("group_id")
task.TaskName = strings.TrimSpace(this.GetString("task_name"))
task.TaskTag = strings.TrimSpace(this.GetString("task_tag"))
task.TaskType, _ = this.GetInt("task_type")
task.Description = strings.TrimSpace(this.GetString("description"))
task.Concurrent, _ = this.GetInt("concurrent")
task.CronSpec = strings.TrimSpace(this.GetString("cron_spec"))
@@ -92,6 +99,7 @@ func (this *TaskController) Add() {
task.Notify, _ = this.GetInt("notify")
task.Timeout, _ = this.GetInt("timeout")
notifyEmail := strings.TrimSpace(this.GetString("notify_email"))
if notifyEmail != "" {
emailList := make([]string, 0)
@@ -138,6 +146,8 @@ func (this *TaskController) Edit() {
if this.isPost() {
task.TaskName = strings.TrimSpace(this.GetString("task_name"))
task.TaskTag = strings.TrimSpace(this.GetString("task_tag"))
task.TaskType, _ = this.GetInt("task_type")
task.Description = strings.TrimSpace(this.GetString("description"))
task.GroupId, _ = this.GetInt("group_id")
task.Concurrent, _ = this.GetInt("concurrent")
@@ -290,10 +300,15 @@ func (this *TaskController) Batch() {
}
case "pause":
jobs.RemoveJob(id)
if task, err := models.TaskGetById(id); err == nil {
task.Status = 0
task.Update()
if task.TaskType==1 {
stopProcess(task.TaskTag)//杀死进程
}
}
case "delete":
models.TaskDel(id)
models.TaskLogDelByTaskId(id)
@@ -343,6 +358,10 @@ func (this *TaskController) Pause() {
task.Status = 0
task.Update()
//如果是常驻进程kill
if task.TaskType==1 {
stopProcess(task.TaskTag)
}
refer := this.Ctx.Request.Referer()
if refer == "" {
refer = beego.URLFor("TaskController.List")
@@ -363,8 +382,14 @@ func (this *TaskController) Run() {
if err != nil {
this.showMsg(err.Error())
}
job.Run()
this.redirect(beego.URLFor("TaskController.ViewLog", "id", job.GetLogId()))
}
//杀死进程
func stopProcess(taskTag string) {
ppPath,_ := os.Getwd() //项目根目录
shellFile := ppPath+"/kill_process.sh "
cmd:= exec.Command("sh","-c", shellFile + taskTag)
out,err:=cmd.Output()
log.Printf("==========================out:%s err:%s",out,err)
}