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