mirror of https://github.com/ouqiang/gocron
增加延时任务
parent
64563ac8d8
commit
4ba6e28162
|
@ -4,10 +4,19 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type TaskType int8
|
||||||
|
|
||||||
|
const (
|
||||||
|
Timing = iota + 1 // 定时任务
|
||||||
|
Delay // 延时任务
|
||||||
|
)
|
||||||
|
|
||||||
// 任务执行日志
|
// 任务执行日志
|
||||||
type TaskLog struct {
|
type TaskLog struct {
|
||||||
Id int64 `xorm:"bigint pk autoincr"`
|
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"` // 任务名称
|
Name string `xorm:"varchar(64) notnull"` // 任务名称
|
||||||
Spec string `xorm:"varchar(64) notnull"` // crontab
|
Spec string `xorm:"varchar(64) notnull"` // crontab
|
||||||
Protocol TaskProtocol `xorm:"tinyint notnull"` // 协议 1:http 2:ssh-command
|
Protocol TaskProtocol `xorm:"tinyint notnull"` // 协议 1:http 2:ssh-command
|
||||||
|
|
|
@ -1,6 +1,18 @@
|
||||||
package delaytask
|
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) {
|
func Create(ctx *macaron.Context) {
|
||||||
|
|
|
@ -45,8 +45,10 @@ func (task *Task) Add(taskModel models.TaskHost) {
|
||||||
cronName := strconv.Itoa(taskModel.Id)
|
cronName := strconv.Itoa(taskModel.Id)
|
||||||
Cron.RemoveJob(cronName)
|
Cron.RemoveJob(cronName)
|
||||||
err := Cron.AddFunc(taskModel.Spec, taskFunc, cronName)
|
err := Cron.AddFunc(taskModel.Spec, taskFunc, cronName)
|
||||||
|
if err != nil {
|
||||||
logger.Error("添加任务到调度器失败#", err)
|
logger.Error("添加任务到调度器失败#", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type Handler interface {
|
type Handler interface {
|
||||||
Run(taskModel models.TaskHost) (string, error)
|
Run(taskModel models.TaskHost) (string, error)
|
||||||
|
@ -103,6 +105,8 @@ func (h *SSHCommandHandler) Run(taskModel models.TaskHost) (string, error) {
|
||||||
|
|
||||||
func createTaskLog(taskModel models.TaskHost) (int64, error) {
|
func createTaskLog(taskModel models.TaskHost) (int64, error) {
|
||||||
taskLogModel := new(models.TaskLog)
|
taskLogModel := new(models.TaskLog)
|
||||||
|
taskLogModel.TaskId = taskModel.Id
|
||||||
|
taskLogModel.Type = models.Timing
|
||||||
taskLogModel.Name = taskModel.Task.Name
|
taskLogModel.Name = taskModel.Task.Name
|
||||||
taskLogModel.Spec = taskModel.Spec
|
taskLogModel.Spec = taskModel.Spec
|
||||||
taskLogModel.Protocol = taskModel.Protocol
|
taskLogModel.Protocol = taskModel.Protocol
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
</h3>
|
</h3>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<table class="ui single line table">
|
<table class="ui striped table">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>主机名</th>
|
<th>主机名</th>
|
||||||
|
@ -35,7 +35,10 @@
|
||||||
<td>{{{.Password}}}</td>
|
<td>{{{.Password}}}</td>
|
||||||
<td>{{{.Port}}}</td>
|
<td>{{{.Port}}}</td>
|
||||||
<td>{{{.Remark}}}</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>
|
</tr>
|
||||||
{{{end}}}
|
{{{end}}}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|
|
@ -27,9 +27,11 @@
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>任务名称</th>
|
<th>任务名称</th>
|
||||||
|
<th>任务类型</th>
|
||||||
<th>cron表达式</th>
|
<th>cron表达式</th>
|
||||||
<th>协议</th>
|
<th>协议</th>
|
||||||
<th>超时时间(秒)</th>
|
<th>超时时间(秒)</th>
|
||||||
|
<th>延时时间(秒)</th>
|
||||||
<th>主机</th>
|
<th>主机</th>
|
||||||
<th>开始时间</th>
|
<th>开始时间</th>
|
||||||
<th>结束时间</th>
|
<th>结束时间</th>
|
||||||
|
@ -41,9 +43,11 @@
|
||||||
{{{range $i, $v := .Logs}}}
|
{{{range $i, $v := .Logs}}}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{{.Name}}}</td>
|
<td>{{{.Name}}}</td>
|
||||||
|
<td>{{{if eq .Type 1}}}定时任务{{{else}}}延时任务{{{end}}}</td>
|
||||||
<td>{{{.Spec}}}</td>
|
<td>{{{.Spec}}}</td>
|
||||||
<td>{{{if eq .Protocol 1}}} HTTP {{{else}}} SSH {{{end}}}</td>
|
<td>{{{if eq .Protocol 1}}} HTTP {{{else}}} SSH {{{end}}}</td>
|
||||||
<td>{{{.Timeout}}}</td>
|
<td>{{{.Timeout}}}</td>
|
||||||
|
<td>{{{.Delay}}}</td>
|
||||||
<td>{{{.Hostname}}}</td>
|
<td>{{{.Hostname}}}</td>
|
||||||
<td>
|
<td>
|
||||||
{{{.StartTime.Format "2006-01-02 15:03:04" }}}
|
{{{.StartTime.Format "2006-01-02 15:03:04" }}}
|
||||||
|
|
Loading…
Reference in New Issue