Files
ppgo_job/jobs/init.go
郝大全 4d8e647523 新增任务分类,杀死进程等
常驻任务和定时任务
常驻任务暂停时杀死进程
2017-06-30 15:49:33 +08:00

73 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* @Author: haodaquan
* @Date: 2017-06-21 12:55:19
* @Last Modified by: haodaquan
* @Last Modified time: 2017-06-21 13:03:06
*/
package jobs
import (
"fmt"
"github.com/astaxie/beego"
"github.com/george518/PPGo_Job/models"
"os/exec"
"time"
)
func InitJobs() {
list, _ := models.TaskGetList(1, 1000000, "status", 1)
for _, task := range list {
job, err := NewJobFromTask(task)
if err != nil {
beego.Error("InitJobs:", err.Error())
continue
}
AddJob(task.CronSpec, job)
}
}
func runCmdWithTimeout(cmd *exec.Cmd, timeout time.Duration) (error, bool) {
done := make(chan error)
go func() {
done <- cmd.Wait()
}()
var err error
select {
case <-time.After(timeout):
beego.Warn(fmt.Sprintf("任务执行时间超过%d秒进程将被强制杀掉: %d", int(timeout/time.Second), cmd.Process.Pid))
go func() {
<-done // 读出上面的goroutine数据避免阻塞导致无法退出
}()
if err = cmd.Process.Kill(); err != nil {
beego.Error(fmt.Sprintf("进程无法杀掉: %d, 错误信息: %s", cmd.Process.Pid, err))
}
return err, true
case err = <-done:
return err, false
}
}
//func stopCmd(cmd *exec.Cmd) (error,bool) {
// done := make(chan error)
// go func() {
// done <- cmd.Wait()
// }()
//
// var err error
// select {
// case <-time.After(timeout):
// beego.Warn(fmt.Sprintf("任务执行时间超过%d秒进程将被强制杀掉: %d", int(timeout/time.Second), cmd.Process.Pid))
// go func() {
// <-done // 读出上面的goroutine数据避免阻塞导致无法退出
// }()
// if err = cmd.Process.Kill(); err != nil {
// beego.Error(fmt.Sprintf("进程无法杀掉: %d, 错误信息: %s", cmd.Process.Pid, err))
// }
// return err, true
// case err = <-done:
// return err, false
// }
//}