Files
ppgo_job/jobs/init.go
georgehao 830d49ee1c 支持多台服务器同时执行任务+优化页面
一个定时任务可以允许多台服务器同时执行任务,优化页面关闭并刷新列表页
2019-04-16 23:40:07 +08:00

55 lines
1.1 KiB
Go
Raw Permalink 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 {
jobs, err := NewJobFromTask(task)
if err != nil {
beego.Error("InitJobs:", err.Error())
continue
}
for _, job := range jobs {
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
}
}