driver

package
v2.0.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2018 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const ZERO_TICK float64 = 0.0000000

Variables

View Source
var (
	ErrScheduleInvalid = errors.New("schedule invalid.") //任务计划无效
	ErrScheduleExpired = errors.New("schedule expired.") //任务计划过期
)
View Source
var (
	//执行超时(超过job.ExecMaxSec阀值)
	ErrExecuteDeadline = errors.New("the job has been executed for too long and has exceeded the timeout threshold.")
	//执行异常
	ErrExecuteException = errors.New("job execute exception")
	//执行终止
	ErrExecuteTerminal = errors.New("job execute terminal error")
)

执行错误定义

View Source
var (
	ErrAllScheduleIsEmpty = errors.New("job all schedules isempty.")
	ErrAllScheduleDisable = errors.New("job all schedules disable.")
	ErrAllScheduleInvalid = errors.New("job all schedules invalid.")
)

Functions

func CalcDaily

func CalcDaily(schedule *models.Schedule, seed time.Time) (time.Time, error)

func CalcInterval

func CalcInterval(schedule *models.Schedule, seed time.Time) (time.Time, error)

func CalcMonthly

func CalcMonthly(schedule *models.Schedule, seed time.Time) (time.Time, error)

func CalcSchedule

func CalcSchedule(schedule *models.Schedule, seed time.Time) (time.Time, error)

func CalcWeekly

func CalcWeekly(schedule *models.Schedule, seed time.Time) (time.Time, error)

Types

type CoreHandler

type CoreHandler struct {
	ICoreHandler
}

type CoreHandlerFunc

type CoreHandlerFunc func(core *ExecCore, state int, err error)

func (CoreHandlerFunc) OnCoreHandlerFunc

func (fn CoreHandlerFunc) OnCoreHandlerFunc(core *ExecCore, state int, err error)

type Driver

type Driver struct {
	sync.RWMutex
	CoreHandler
	Root string
	// contains filtered or unexported fields
}

Driver is exported

func NewDirver

func NewDirver(root string, handler IDriverHandler) *Driver

NewDirver is exported

func (*Driver) Action

func (driver *Driver) Action(jobid string, action string)

Action is exported

func (*Driver) Clear

func (driver *Driver) Clear()

Clear is exported

func (*Driver) Dispatch

func (driver *Driver) Dispatch()

Dispatch is exported

func (*Driver) ExecuteHandleFunc

func (driver *Driver) ExecuteHandleFunc(state int, context *DriverContext)

func (*Driver) NewExecuteContext

func (driver *Driver) NewExecuteContext(job *Job, core *ExecCore, nextat time.Time, err error) *DriverContext

NewExecuteContext构造

func (*Driver) NewSelectContext

func (driver *Driver) NewSelectContext(job *Job, nextat time.Time) *DriverContext

NewSelectContext构造

func (*Driver) NewStopedContext

func (driver *Driver) NewStopedContext(job *Job, execat time.Time, nextat time.Time, err error) *DriverContext

NewStopedContext构造

func (*Driver) OnCoreHandlerFunc

func (driver *Driver) OnCoreHandlerFunc(core *ExecCore, state int, err error)

func (*Driver) Remove

func (driver *Driver) Remove(jobid string)

Remove is exported

func (*Driver) SelectHandleFunc

func (driver *Driver) SelectHandleFunc(context *DriverContext)

func (*Driver) Set

func (driver *Driver) Set(jobbase *models.JobBase)

Set is exported

func (*Driver) StopedHandleFunc

func (driver *Driver) StopedHandleFunc(state int, context *DriverContext)

type DriverContext

type DriverContext struct {
	Job       *Job
	StdOut    string
	ErrOut    string
	ExecErr   string
	ExecAt    time.Time
	NextAt    time.Time
	ExecTimes float64
}

DriverContext上下文定义

type DriverExecuteHandlerFunc

type DriverExecuteHandlerFunc func(state int, context *DriverContext)

func (DriverExecuteHandlerFunc) OnDriverExecuteHandlerFunc

func (fn DriverExecuteHandlerFunc) OnDriverExecuteHandlerFunc(state int, context *DriverContext)

type DriverSelectHandlerFunc

type DriverSelectHandlerFunc func(context *DriverContext)

func (DriverSelectHandlerFunc) OnDriverSelectHandlerFunc

func (fn DriverSelectHandlerFunc) OnDriverSelectHandlerFunc(context *DriverContext)

type DriverStopedHandlerFunc

type DriverStopedHandlerFunc func(state int, context *DriverContext)

func (DriverStopedHandlerFunc) OnDriverStopedHandlerFunc

func (fn DriverStopedHandlerFunc) OnDriverStopedHandlerFunc(state int, context *DriverContext)

type ExecCore

type ExecCore struct {
	JobId      string           //任务编号
	WorkDir    string           //工作目录
	Exit       ExitState        //退出状态
	ExecAt     time.Time        //本次执行时间
	NextAt     time.Time        //下次执行时间
	Schedule   *models.Schedule //执行计划
	ExecDriver *ExecDriver      //执行驱动
	// contains filtered or unexported fields
}

func NewExecCore

func NewExecCore(jobid string, schedule *models.Schedule, handler ICoreHandler) *ExecCore

func (*ExecCore) Close

func (core *ExecCore) Close(state ExitState) error

func (*ExecCore) Execute

func (core *ExecCore) Execute(seed time.Time, workdir string, cmd string, env []string)

func (*ExecCore) GetExecDriverPipeBuffer

func (core *ExecCore) GetExecDriverPipeBuffer() ([]byte, []byte)

func (*ExecCore) GetExecTimes

func (core *ExecCore) GetExecTimes() float64

type ExecDriver

type ExecDriver struct {
	Running   bool      //执行状态
	Command   *exec.Cmd //执行对象
	ExecTimes float64   //总执行时长
	StdOut    StdOutput //标准输出
	ErrOut    StdOutput //错误输出
}

ExecDriver 任务执行体 负责任务执行的生命期和状态.

func NewExecDriver

func NewExecDriver(name string, cmd string, env []string) (*ExecDriver, error)

func (*ExecDriver) ReadCommandPipeBuffer

func (driver *ExecDriver) ReadCommandPipeBuffer(stdoutCh chan<- []byte, erroutCh chan<- []byte)

ReadCommandPipeBuffer 读取任务输出管道数据 StdOut.Buffer:标准输出 ErrOut.Buffer:错误输出 分别开启携程读取管道数据,避免读取阻塞. 若不开启携程,有种情况会出现读取stderr数据导致stdout读取阻塞.

func (*ExecDriver) SetCommandPipe

func (driver *ExecDriver) SetCommandPipe() error

SetCommandPipe 设置command对象管道 指定程序标准输出到StdOut.reader & ErrOut.reader 设置失败返回error.

func (*ExecDriver) Start

func (driver *ExecDriver) Start(start chan<- bool) error

func (*ExecDriver) Stop

func (driver *ExecDriver) Stop() error

type ExitState

type ExitState int
const (
	EXIT_NORMAL   ExitState = iota + 1 //无强制退出状态
	EXIT_STOP                          //强制停止退出(Action Stop 命令)
	EXIT_DEADLINE                      //超时强制退出(Execute Timeout)
)

func (ExitState) String

func (state ExitState) String() string

type ICoreHandler

type ICoreHandler interface {
	OnCoreHandlerFunc(core *ExecCore, state int, err error)
}

type IDriverHandler

type IDriverHandler interface {
	//DriverContext Code = ERR_SCHEDULE_EXECUTE
	OnDriverExecuteHandlerFunc(state int, context *DriverContext)
	//DriverContext Code = ERR_SCHEDULE_INVALID
	OnDriverSelectHandlerFunc(context *DriverContext)
	//DriverContext Code = ERR_SCHEDULE_STOPED
	OnDriverStopedHandlerFunc(state int, context *DriverContext)
}

Driver回调handler定义

type IExecDriver

type IExecDriver interface {
	//启动任务
	Start(start chan<- bool) error
	//停止任务
	Stop() error
	//设置exec.cmd输入输出管道
	SetCommandPipe() error
	//读取exec.cmd管道数据
	ReadCommandPipeBuffer(stdoutCh chan<- []byte, erroutCh chan<- []byte)
}

ExecDriver 接口定义

type Job

type Job struct {
	JobId      string    //任务编号
	Name       string    //任务名称
	Root       string    //工作根目录
	FileCode   string    //文件编码
	WorkDir    string    //工作目录
	Cmd        string    //执行命令
	Env        []string  //环境变量
	Timeout    int       //执行超时(秒)
	ExecMaxSec int64     //执行最长时长(时间戳:UNIX时间戳)
	State      JobState  //执行状态
	LastExecAt time.Time //最后一次执行时间
	LastError  error     //最后一次错误信息
	// contains filtered or unexported fields
}

func NewJob

func NewJob(root string, jobbase *models.JobBase, handler ICoreHandler) *Job

func (*Job) CheckWithTimeout

func (job *Job) CheckWithTimeout(seed time.Time)

func (*Job) Close

func (job *Job) Close(state ExitState)

func (*Job) Execute

func (job *Job) Execute(seed time.Time, force bool)

func (*Job) Select

func (job *Job) Select() error

func (*Job) SetJob

func (job *Job) SetJob(jobbase *models.JobBase, handler ICoreHandler)

type JobState

type JobState int
const (
	JOB_RUNNING JobState = iota + 1 //任务被调度状态
	JOB_WAITING                     //任务等待调度状态
)

func (JobState) String

func (state JobState) String() string

type StdOutput

type StdOutput struct {
	Reader io.ReadCloser //输出读取对象(stdout或stderr)
	Buffer []byte        //输出数据
}

StdOutput 输出数据结构定义

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL