代码拉取完成,页面将自动刷新
package scheduler
import (
"fmt"
"gitee.com/h79/goutils/common/result"
"gitee.com/h79/goutils/common/system"
"gitee.com/h79/goutils/common/timer"
"time"
)
type JobFunc func(job *Job) (interface{}, error)
var ErrJobFuncNil = result.Error(result.ErrParam, "job func is nil")
var ErrJobQuited = result.Error(result.ErrParam, "job quited")
var _ Task = (*Job)(nil)
type Job struct {
JType string
JId string
JState State
JStartAt int64
JTimeout time.Duration
JFunc JobFunc
JData interface{} //自定认数据
}
type JobResult struct {
JType string `json:"type"`
JId string `json:"id"`
Err error `json:"err,omitempty"`
Data interface{} `json:"data,omitempty"`
}
type ShellJob struct {
Job
Cmd string
Result system.CmdResult
}
func NewShellJob(cmd string) *ShellJob {
job := &ShellJob{
Job: BuildJob("shell", fmt.Sprintf("ShellJob: Cmd:%s", cmd), 0),
Cmd: cmd,
}
return job
}
func (job *ShellJob) Execute() (interface{}, error) {
if job.JState.IsQuit() {
return nil, ErrJobQuited
}
if job.JState == InitState {
job.JState = RunningState
}
job.Result = system.SyncExec(job.Cmd, job.JTimeout)
if job.Result.Err != nil {
job.JState = ErrorState
return job.Result, job.Result.Err
}
job.JState = CompletedState
return job.Result, nil
}
func BuildJob(jType, jobId string, timeout time.Duration) Job {
return Job{
JType: jType,
JId: jobId,
JState: InitState,
JStartAt: timer.CurrentS(),
JTimeout: timeout,
}
}
func NewJob(jType, jobId string, timeout time.Duration) *Job {
job := BuildJob(jType, jobId, timeout)
return &job
}
func NewEmptyJob(fn JobFunc) *Job {
job := BuildJob("", "", 0)
return job.WithFunc(fn)
}
func (job *Job) WithFunc(fn JobFunc) *Job {
job.JFunc = fn
return job
}
func (job *Job) WithTimeOut(timeout time.Duration) *Job {
job.JTimeout = timeout
return job
}
func (job *Job) WithData(data interface{}) *Job {
job.JData = data
return job
}
func (job *Job) GetType() string {
return job.JType
}
func (job *Job) GetKey() string {
return job.JId
}
func (job *Job) GetState() State {
return job.JState
}
func (job *Job) Execute() (interface{}, error) {
if job.JState.IsQuit() {
return nil, ErrJobQuited
}
if job.JState == InitState {
job.JState = RunningState
}
if job.JFunc == nil {
job.JState = CompletedState
return nil, ErrJobFuncNil
}
return job.JFunc(job)
}
func (job *Job) Cancel() {
job.JState = CancelState
}
func (job *Job) Pause() {
job.JState = PauseState
}
func (job *Job) Start() {
job.JState = RunningState
}
func (job *Job) IsQuit() bool {
return job.JState.IsQuit()
}
func (job *Job) CheckTimeout() bool {
if job.JTimeout <= 0 {
return false
}
now := timer.CurrentS()
if job.JStartAt <= 0 {
job.JStartAt = now
}
return now-job.JStartAt > int64(job.JTimeout.Seconds())
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。