1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
job.go 2.79 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-04-12 14:20 . scheduler
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) GetId() 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())
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.6.6

搜索帮助

344bd9b3 5694891 D2dac590 5694891