Documentation
¶
Index ¶
- Constants
- Variables
- func NewFileLogger(filePath string) *fileLogger
- func NewFmtLogger() *fmtLogger
- func PackageVersion() string
- func ValidateTaskType(taskType string) bool
- type AppConfig
- type ConfigHandle
- type Counter
- type CounterInfo
- type CronTask
- type ExceptionHandleFunc
- type ExpressSet
- type Logger
- type LoopTask
- type QueueTask
- type ShowCountInfo
- type StandardCounter
- type Task
- func NewCronTask(taskID string, isRun bool, express string, handler TaskHandle, ...) (Task, error)
- func NewLoopTask(taskID string, isRun bool, dueTime int64, interval int64, handler TaskHandle, ...) (Task, error)
- func NewQueueTask(taskID string, isRun bool, interval int64, handler TaskHandle, ...) (Task, error)
- type TaskConfig
- type TaskContext
- type TaskHandle
- type TaskInfo
- type TaskService
- func (service *TaskService) AddTask(t Task)
- func (service *TaskService) Count() int
- func (service *TaskService) CounterOutputHttpHandler(w http.ResponseWriter, r *http.Request)
- func (service *TaskService) CreateCronTask(taskID string, isRun bool, express string, handler TaskHandle, ...) (Task, error)
- func (service *TaskService) CreateLoopTask(taskID string, isRun bool, dueTime int64, interval int64, handler TaskHandle, ...) (Task, error)
- func (service *TaskService) CreateQueueTask(taskID string, isRun bool, interval int64, handler TaskHandle, ...) (Task, error)
- func (service *TaskService) CreateTask(config TaskConfig) (Task, error)
- func (service *TaskService) GetAllTaskCountInfo() []ShowCountInfo
- func (service *TaskService) GetAllTasks() map[string]Task
- func (service *TaskService) GetHandler(name string) (TaskHandle, bool)
- func (service *TaskService) GetTask(taskID string) (t Task, exists bool)
- func (service *TaskService) LoadConfig(configFile string, confType ...interface{}) *TaskService
- func (service *TaskService) LoadConfigHandler(configHandler ConfigHandle, configSource string) *TaskService
- func (service *TaskService) LoadFileConfig(configFile string, confType ...interface{}) *TaskService
- func (service *TaskService) Logger() Logger
- func (service *TaskService) PrintAllCronTask() string
- func (service *TaskService) PrintAllTaskCounterInfo() string
- func (service *TaskService) PrintAllTasks() string
- func (service *TaskService) RegisterHandler(name string, handler TaskHandle) *TaskService
- func (service *TaskService) RemoveAllTask()
- func (service *TaskService) RemoveTask(taskID string)
- func (service *TaskService) SetExceptionHandler(handler ExceptionHandleFunc)
- func (service *TaskService) SetLogger(logger Logger)
- func (service *TaskService) SetOnBeforeHandler(handler TaskHandle)
- func (service *TaskService) SetOnEndHandler(handler TaskHandle)
- func (service *TaskService) StartAllTask()
- func (service *TaskService) StopAllTask()
- func (service *TaskService) TaskOutputHttpHandler(w http.ResponseWriter, r *http.Request)
- func (service *TaskService) UseDefaultLogCounterTask()
Constants ¶
const ( TaskState_Init = "0" TaskState_Run = "1" TaskState_Stop = "2" TaskType_Loop = "loop" TaskType_Cron = "cron" TaskType_Queue = "queue" ConfigType_Xml = "xml" ConfigType_Json = "json" ConfigType_Yaml = "yaml" )
const ( Max_WeekDay = 7 //max weekday value Min_WeekDay = 0 //min weekday value Max_Month = 12 //max month value Min_Month = 1 //min month value Max_Day = 31 //max day value Min_Day = 1 //min day value Max_Hour = 23 //max hour value Min_Hour = 0 //min hour value Max_Minute = 59 //max minute value Min_Minute = 0 //min minute value Max_Second = 59 //max second value Min_Second = 0 //min second value )
const ( ExpressType_WeekDay = "weekday" ExpressType_Month = "month" ExpressType_Day = "day" ExpressType_Hour = "hour" ExpressType_Minute = "minute" ExpressType_Second = "second" )
const (
DefaultPeriod = time.Second //默认执行周期
)
const (
DefaultQueueSize = 1000
)
Variables ¶
var ErrNotSupportTaskType = errors.New("not support task type")
Functions ¶
func NewFileLogger ¶
func NewFileLogger(filePath string) *fileLogger
func NewFmtLogger ¶
func NewFmtLogger() *fmtLogger
func ValidateTaskType ¶
ValidateTaskType validate the TaskType is supported
Types ¶
type AppConfig ¶
type AppConfig struct { XMLName xml.Name `xml:"config"` Global struct { LogPath string `xml:"logpath,attr" yaml:"logpath"` IsRun bool `xml:"isrun,attr" yaml:"isrun"` Timeout int64 `xml:"timeout,attr" yaml:"timeout"` //全局超时配置,单位为毫秒 } `xml:"global" yaml:"global"` Tasks []struct { TaskID string `xml:"taskid,attr" yaml:"taskid"` //task编号,需唯一 IsRun bool `xml:"isrun,attr" yaml:"isrun"` //标识是否允许task执行,默认为false,如设为flash,则启动后不执行task TaskType string `xml:"type,attr" yaml:"type"` //Task类型,目前支持loop、cron、queue DueTime int64 `xml:"duetime,attr" yaml:"duetime"` //开始任务的延迟时间(以毫秒为单位),如果<=0则不延迟 Interval int64 `xml:"interval,attr" yaml:"interval"` //loop类型下,两次Task执行之间的间隔,单位为毫秒 Express string `xml:"express,attr" yaml:"express"` //cron类型下,task执行的时间表达式,具体参考readme QueueSize int64 `xml:"queuesize,attr" yaml:"queuesize"` //queue类型下,queue初始长度 HandlerName string `xml:"handlername,attr" yaml:"handlername"` //Task对应的HandlerName,需使用RegisterHandler进行统一注册 HandlerData string `xml:"handlerdata,attr" yaml:"handlerdata"` //Task对应的自定义数据,可在配置源中设置 Timeout int64 `xml:"timeout,attr" yaml:"timeout"` //全局超时配置,单位为毫秒 } `xml:"tasks>task" yaml:"tasks"` }
type ConfigHandle ¶
type CounterInfo ¶
type CronTask ¶
type CronTask struct { TaskInfo RawExpress string `json:"express"` //运行周期表达式,当TaskType==TaskType_Cron时有效 // contains filtered or unexported fields }
CronTask cron task info define
func (*CronTask) GetConfig ¶
func (task *CronTask) GetConfig() *TaskConfig
GetConfig get task config info
func (*CronTask) Reset ¶
func (task *CronTask) Reset(conf *TaskConfig) error
Reset first check conf, then reload conf & restart task special, TaskID can not be reset special, if TaskData is nil, it can not be reset special, if Handler is nil, it can not be reset
type ExceptionHandleFunc ¶
type ExceptionHandleFunc func(*TaskContext, error)
type ExpressSet ¶
type ExpressSet struct {
// contains filtered or unexported fields
}
type Logger ¶
type Logger interface { Error(err error, v interface{}) Warn(v interface{}) Info(v interface{}) Debug(v interface{}) }
type LoopTask ¶
type LoopTask struct { TaskInfo Interval int64 `json:"interval"` //运行间隔时间,单位毫秒,当TaskType==TaskType_Loop||TaskType_Queue时有效 }
LoopTask loop task info define
func (*LoopTask) GetConfig ¶
func (task *LoopTask) GetConfig() *TaskConfig
GetConfig get task config info
func (*LoopTask) Reset ¶
func (task *LoopTask) Reset(conf *TaskConfig) error
Reset first check conf, then reload conf & restart task special, TaskID can not be reset special, if TaskData is nil, it can not be reset special, if Handler is nil, it can not be reset
type QueueTask ¶
type QueueTask struct { TaskInfo Interval int64 //运行间隔时间,单位毫秒,当TaskType==TaskType_Loop||TaskType_Queue时有效 MessageChan chan interface{} }
func (*QueueTask) EnQueue ¶
func (task *QueueTask) EnQueue(value interface{})
EnQueue enqueue value into message queue
func (*QueueTask) GetConfig ¶
func (task *QueueTask) GetConfig() *TaskConfig
GetConfig get task config info
func (*QueueTask) Reset ¶
func (task *QueueTask) Reset(conf *TaskConfig) error
Reset first check conf, then reload conf & restart task
type ShowCountInfo ¶
type StandardCounter ¶
type StandardCounter struct {
// contains filtered or unexported fields
}
StandardCounter is the standard implementation of a Counter
func (*StandardCounter) Count ¶
func (c *StandardCounter) Count() int64
Count returns the current count.
func (*StandardCounter) Dec ¶
func (c *StandardCounter) Dec(i int64)
Dec decrements the counter by the given amount.
func (*StandardCounter) Inc ¶
func (c *StandardCounter) Inc(i int64)
Inc increments the counter by the given amount.
func (*StandardCounter) StartTime ¶
func (c *StandardCounter) StartTime() time.Time
type Task ¶
type Task interface { TaskID() string GetConfig() *TaskConfig Start() Stop() SetTimeout(int64) RunOnce() error SetTaskService(service *TaskService) Reset(conf *TaskConfig) error CounterInfo() *CounterInfo }
func NewCronTask ¶
func NewCronTask(taskID string, isRun bool, express string, handler TaskHandle, taskData interface{}) (Task, error)
NewCronTask create new cron task
func NewLoopTask ¶
func NewLoopTask(taskID string, isRun bool, dueTime int64, interval int64, handler TaskHandle, taskData interface{}) (Task, error)
NewLoopTask create new loop task
func NewQueueTask ¶
func NewQueueTask(taskID string, isRun bool, interval int64, handler TaskHandle, taskData interface{}, queueSize int64) (Task, error)
NewQueueTask create new queue task
type TaskConfig ¶
type TaskConfig struct { TaskID string TaskType string IsRun bool Handler TaskHandle `json:"-"` DueTime int64 Interval int64 Express string TaskData interface{} }
TaskConfig task config
type TaskContext ¶
type TaskContext struct { TaskID string TaskData interface{} //用于当前Task全局设置的数据项 Message interface{} //用于每次Task执行上下文消息传输 IsEnd bool //如果设置该属性为true,则停止当次任务的后续执行,一般用在OnBegin中 Error error Header map[string]interface{} TimeoutContext context.Context TimeoutCancel context.CancelFunc // contains filtered or unexported fields }
Task上下文信息
type TaskHandle ¶
type TaskHandle func(*TaskContext) error
type TaskInfo ¶
type TaskInfo struct { IsRun bool `json:"isrun"` TimeTicker *time.Ticker `json:"-"` TaskType string `json:"tasktype"` TaskData interface{} State string `json:"state"` //匹配 TskState_Init、TaskState_Run、TaskState_Stop DueTime int64 `json:"duetime"` //开始任务的延迟时间(以毫秒为单位),如果<=0则不延迟 Timeout int64 // contains filtered or unexported fields }
TaskInfo task info define
func (*TaskInfo) CounterInfo ¶
func (task *TaskInfo) CounterInfo() *CounterInfo
func (*TaskInfo) RunOnce ¶
RunOnce do task only once no match Express or Interval no recover panic support for #6 新增RunOnce方法建议
func (*TaskInfo) SetTaskService ¶
func (task *TaskInfo) SetTaskService(service *TaskService)
SetTaskService Set up the associated service
func (*TaskInfo) SetTimeout ¶
type TaskService ¶
type TaskService struct { Config *AppConfig ExceptionHandler ExceptionHandleFunc OnBeforeHandler TaskHandle OnEndHandler TaskHandle // contains filtered or unexported fields }
TaskService task 容器
func StartNewService ¶
func StartNewService() *TaskService
func (*TaskService) AddTask ¶
func (service *TaskService) AddTask(t Task)
AddTask add new task point
func (*TaskService) CounterOutputHttpHandler ¶
func (service *TaskService) CounterOutputHttpHandler(w http.ResponseWriter, r *http.Request)
CounterOutputHttpHandler Http Handler for output counter info
func (*TaskService) CreateCronTask ¶
func (service *TaskService) CreateCronTask(taskID string, isRun bool, express string, handler TaskHandle, taskData interface{}) (Task, error)
CreateCronTask create new cron task and register to task service
func (*TaskService) CreateLoopTask ¶
func (service *TaskService) CreateLoopTask(taskID string, isRun bool, dueTime int64, interval int64, handler TaskHandle, taskData interface{}) (Task, error)
CreateLoopTask create new loop task and register to task service
func (*TaskService) CreateQueueTask ¶
func (service *TaskService) CreateQueueTask(taskID string, isRun bool, interval int64, handler TaskHandle, taskData interface{}, queueSize int64) (Task, error)
CreateQueueTask create new queue task and register to task service
func (*TaskService) CreateTask ¶
func (service *TaskService) CreateTask(config TaskConfig) (Task, error)
CreateTask create new task with TaskConfig and register to task service
func (*TaskService) GetAllTaskCountInfo ¶
func (service *TaskService) GetAllTaskCountInfo() []ShowCountInfo
GetAllTaskCountInfo return all show count info
func (*TaskService) GetAllTasks ¶
func (service *TaskService) GetAllTasks() map[string]Task
GetAllTasks get all tasks
func (*TaskService) GetHandler ¶
func (service *TaskService) GetHandler(name string) (TaskHandle, bool)
GetHandler get handler by handler name
func (*TaskService) GetTask ¶
func (service *TaskService) GetTask(taskID string) (t Task, exists bool)
GetTask get TaskInfo by TaskID
func (*TaskService) LoadConfig ¶
func (service *TaskService) LoadConfig(configFile string, confType ...interface{}) *TaskService
LoadConfig 如果指定配置文件,初始化配置 Deprecated: Use the LoadFileConfig instead
func (*TaskService) LoadConfigHandler ¶
func (service *TaskService) LoadConfigHandler(configHandler ConfigHandle, configSource string) *TaskService
LoadConfigHandler load config handler and init task config
func (*TaskService) LoadFileConfig ¶
func (service *TaskService) LoadFileConfig(configFile string, confType ...interface{}) *TaskService
LoadFileConfig 如果指定配置文件,初始化配置
func (*TaskService) PrintAllCronTask ¶
func (service *TaskService) PrintAllCronTask() string
PrintAllCronTask print all task info Deprecated: Use the PrintAllTasks instead
func (*TaskService) PrintAllTaskCounterInfo ¶
func (service *TaskService) PrintAllTaskCounterInfo() string
PrintAllTaskCounterInfo print all task counter data
func (*TaskService) PrintAllTasks ¶
func (service *TaskService) PrintAllTasks() string
PrintAllCronTask print all task info
func (*TaskService) RegisterHandler ¶
func (service *TaskService) RegisterHandler(name string, handler TaskHandle) *TaskService
RegisterHandler register handler by name
func (*TaskService) RemoveAllTask ¶
func (service *TaskService) RemoveAllTask()
RemoveAllTask remove all task
func (*TaskService) RemoveTask ¶
func (service *TaskService) RemoveTask(taskID string)
RemoveTask remove task by taskid
func (*TaskService) SetExceptionHandler ¶
func (service *TaskService) SetExceptionHandler(handler ExceptionHandleFunc)
SetExceptionHandler 设置自定义异常处理方法
func (*TaskService) SetLogger ¶
func (service *TaskService) SetLogger(logger Logger)
SetLogger set logger which Implements Logger interface
func (*TaskService) SetOnBeforeHandler ¶
func (service *TaskService) SetOnBeforeHandler(handler TaskHandle)
SetOnBeforeHandler set handler which exec before task run
func (*TaskService) SetOnEndHandler ¶
func (service *TaskService) SetOnEndHandler(handler TaskHandle)
SetOnEndHandler set handler which exec after task run
func (*TaskService) StartAllTask ¶
func (service *TaskService) StartAllTask()
StartAllTask start all task
func (*TaskService) StopAllTask ¶
func (service *TaskService) StopAllTask()
StopAllTask stop all task
func (*TaskService) TaskOutputHttpHandler ¶
func (service *TaskService) TaskOutputHttpHandler(w http.ResponseWriter, r *http.Request)
TaskOutputHttpHandler Http Handler for output task info
func (*TaskService) UseDefaultLogCounterTask ¶
func (service *TaskService) UseDefaultLogCounterTask()
UseDefaultLogCounterTask use default LogCounterTask in TaskService