增加延时任务

pull/21/merge
ouqiang 2017-04-14 18:05:34 +08:00
parent 64563ac8d8
commit 4ba6e28162
5 changed files with 37 additions and 5 deletions

View File

@ -4,10 +4,19 @@ import (
"time"
)
type TaskType int8
const (
Timing = iota + 1 // 定时任务
Delay // 延时任务
)
// 任务执行日志
type TaskLog struct {
Id int64 `xorm:"bigint pk autoincr"`
taskId int `xorm:"int notnull index default 0"` // 任务id
TaskId int `xorm:"int notnull index default 0"` // 任务id
Type TaskType `xorm:"tinyint notnull default 1"` // 任务类型 1 定时任务 2 延时任务
Delay int `xorm:"int notnull default 0"` // 延时任务-延时时间
Name string `xorm:"varchar(64) notnull"` // 任务名称
Spec string `xorm:"varchar(64) notnull"` // crontab
Protocol TaskProtocol `xorm:"tinyint notnull"` // 协议 1:http 2:ssh-command

View File

@ -1,6 +1,18 @@
package delaytask
import "gopkg.in/macaron.v1"
import (
"gopkg.in/macaron.v1"
"github.com/ouqiang/gocron/models"
)
type DelayForm struct {
Protocol models.TaskType
Type models.TaskType
Host string
Delay int
Command string
Timeout int
}
// 创建延时任务
func Create(ctx *macaron.Context) {

View File

@ -45,7 +45,9 @@ func (task *Task) Add(taskModel models.TaskHost) {
cronName := strconv.Itoa(taskModel.Id)
Cron.RemoveJob(cronName)
err := Cron.AddFunc(taskModel.Spec, taskFunc, cronName)
logger.Error("添加任务到调度器失败#", err)
if err != nil {
logger.Error("添加任务到调度器失败#", err)
}
}
type Handler interface {
@ -103,6 +105,8 @@ func (h *SSHCommandHandler) Run(taskModel models.TaskHost) (string, error) {
func createTaskLog(taskModel models.TaskHost) (int64, error) {
taskLogModel := new(models.TaskLog)
taskLogModel.TaskId = taskModel.Id
taskLogModel.Type = models.Timing
taskLogModel.Name = taskModel.Task.Name
taskLogModel.Spec = taskModel.Spec
taskLogModel.Protocol = taskModel.Protocol

View File

@ -14,7 +14,7 @@
</h3>
</div>
</div>
<table class="ui single line table">
<table class="ui striped table">
<thead>
<tr>
<th>主机名</th>
@ -35,7 +35,10 @@
<td>{{{.Password}}}</td>
<td>{{{.Port}}}</td>
<td>{{{.Remark}}}</td>
<td><button class="ui positive button" onclick="util.removeConfirm('/host/remove/{{{.Id}}}')">删除</button></td>
<td>
<button class="ui positive button" onclick="util.removeConfirm('/host/remove/{{{.Id}}}')">删除</button>
<button class="ui pink button" >查看任务</button>
</td>
</tr>
{{{end}}}
</tbody>

View File

@ -27,9 +27,11 @@
<thead>
<tr>
<th>任务名称</th>
<th>任务类型</th>
<th>cron表达式</th>
<th>协议</th>
<th>超时时间(秒)</th>
<th>延时时间(秒)</th>
<th>主机</th>
<th>开始时间</th>
<th>结束时间</th>
@ -41,9 +43,11 @@
{{{range $i, $v := .Logs}}}
<tr>
<td>{{{.Name}}}</td>
<td>{{{if eq .Type 1}}}定时任务{{{else}}}延时任务{{{end}}}</td>
<td>{{{.Spec}}}</td>
<td>{{{if eq .Protocol 1}}} HTTP {{{else}}} SSH {{{end}}}</td>
<td>{{{.Timeout}}}</td>
<td>{{{.Delay}}}</td>
<td>{{{.Hostname}}}</td>
<td>
{{{.StartTime.Format "2006-01-02 15:03:04" }}}