Documentation ¶
Index ¶
- Variables
- type Runner
- func (r *Runner) Backend() db.Backend
- func (r *Runner) GetTaskCheckInterval() time.Duration
- func (r *Runner) GetTaskSpecs() map[string]kit.TaskSpec
- func (r *Runner) MaximumConcurrentTasks() int
- func (r *Runner) RegisterTask(spec kit.TaskSpec)
- func (r *Runner) Registry() kit.Registry
- func (r *Runner) Run() apperror.Error
- func (r *Runner) SetBackend(backend db.Backend)
- func (r *Runner) SetMaximumConcurrentTasks(count int)
- func (r *Runner) SetRegistry(registry kit.Registry)
- func (r *Runner) SetTaskCheckInterval(duration time.Duration)
- func (r *Runner) Shutdown() chan bool
- type Service
- type Task
- func (Task) Collection() string
- func (t *Task) GetCreatedAt() time.Time
- func (t Task) GetData() interface{}
- func (t Task) GetError() string
- func (t Task) GetFinishedAt() *time.Time
- func (t Task) GetLog() string
- func (t Task) GetName() string
- func (t *Task) GetPriority() int
- func (t *Task) GetProgress() int
- func (t Task) GetResult() interface{}
- func (t *Task) GetRunAt() *time.Time
- func (t Task) GetStartedAt() *time.Time
- func (t Task) GetTryCount() int
- func (t *Task) IsCancelled() bool
- func (t Task) IsComplete() bool
- func (t *Task) IsRunning() bool
- func (t Task) IsSuccess() bool
- func (t *Task) SetCreatedAt(tm time.Time)
- func (t *Task) SetData(data interface{})
- func (t *Task) SetError(err string)
- func (t *Task) SetFinishedAt(tm *time.Time)
- func (t *Task) SetIsCancelled(x bool)
- func (t *Task) SetIsComplete(flag bool)
- func (t *Task) SetIsRunning(x bool)
- func (t *Task) SetIsSuccess(flag bool)
- func (t *Task) SetLog(log string)
- func (t *Task) SetName(name string)
- func (t *Task) SetPriority(x int)
- func (t *Task) SetProgress(x int)
- func (t *Task) SetResult(result interface{})
- func (t *Task) SetRunAt(x *time.Time)
- func (t *Task) SetStartedAt(tm *time.Time)
- func (t *Task) SetTryCount(count int)
- type TaskIntId
- type TaskSpec
- type TaskStrId
Constants ¶
This section is empty.
Variables ¶
View Source
var CancelTaskMethod = &methods.Method{ Name: "task.cancel", Blocking: false, Handler: func(registry kit.Registry, r kit.Request, unblock func()) kit.Response { user := r.GetUser() if user == nil { return kit.NewErrorResponse("not_authenticated", true) } taskId := r.GetData() if taskId == nil { return kit.NewErrorResponse("invalid_task_id", "Expected 'data' to be the task Id.", true) } backend := registry.DefaultBackend() rawTask, err := backend.FindOne("tasks", taskId) if err != nil { return kit.NewErrorResponse(err) } else if rawTask == nil { return kit.NewErrorResponse("not_found", "Task does not exist.") } task := rawTask.(kit.Task) if !(user.HasRole("admin") || user.GetId() == task.GetUserId()) { return kit.NewErrorResponse("permission_denied") } if task.IsComplete() { return kit.NewErrorResponse("task_complete", "Can't cancel a completed task.") } else if task.IsRunning() { return kit.NewErrorResponse("task_running", "Can't cancel a running task.") } if err := backend.Update(task); err != nil { return kit.NewErrorResponse(err) } return &kit.AppResponse{ Data: map[string]interface{}{"success": true}, } }, }
View Source
var RetryTaskMethod = &methods.Method{ Name: "task.retry", Blocking: false, Handler: func(registry kit.Registry, r kit.Request, unblock func()) kit.Response { user := r.GetUser() if user == nil { return kit.NewErrorResponse("not_authenticated", true) } taskId := r.GetData() if taskId == nil { return kit.NewErrorResponse("invalid_task_id", "Expected 'data' to be the task Id.", true) } backend := registry.DefaultBackend() rawTask, err := backend.FindOne("tasks", taskId) if err != nil { return kit.NewErrorResponse(err) } else if rawTask == nil { return kit.NewErrorResponse("not_found", "Task does not exist.") } task := rawTask.(kit.Task) if !(user.HasRole("admin") || user.GetId() == task.GetUserId()) { return kit.NewErrorResponse("permission_denied") } if task.IsSuccess() { return kit.NewErrorResponse("task_succeeded", "Can't retry a succeeded task") } else if !task.IsComplete() { return kit.NewErrorResponse("task_not_complete", "Can't retry a task that has not completed yet.") } task.SetIsComplete(false) task.SetTryCount(0) task.SetRunAt(nil) if err := backend.Update(task); err != nil { return kit.NewErrorResponse(err) } return &kit.AppResponse{ Data: map[string]interface{}{"success": true}, } }, }
Functions ¶
This section is empty.
Types ¶
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
func (*Runner) GetTaskCheckInterval ¶
func (*Runner) GetTaskSpecs ¶
GetTaskSpecs returns a slice with all registered tasks.
func (*Runner) MaximumConcurrentTasks ¶
func (*Runner) RegisterTask ¶
func (*Runner) SetBackend ¶
func (*Runner) SetMaximumConcurrentTasks ¶
func (*Runner) SetRegistry ¶
func (*Runner) SetTaskCheckInterval ¶
type Task ¶
type Task struct { Name string `db:"required"` Data interface{} `db:"marshal"` RunAt *time.Time Priority int Progress int CreatedAt time.Time Cancelled bool Result interface{} `db:"marshal"` TryCount int StartedAt *time.Time FinishedAt *time.Time Running bool Complete bool Success bool Error string Log string }
func (Task) Collection ¶
func (*Task) GetCreatedAt ¶
func (Task) GetData ¶
func (t Task) GetData() interface{}
GetData returns the data associated with the task.
func (Task) GetFinishedAt ¶
FinishedAt returns the time the task was finished, or zero value.
func (*Task) GetPriority ¶
func (*Task) GetProgress ¶
func (Task) GetResult ¶
func (t Task) GetResult() interface{}
GetResult returns the result data omitted by the task.
func (Task) GetStartedAt ¶
StartedAt returns a time if the task was started, or zero value otherwise.
func (Task) GetTryCount ¶
TryCount returns the number of times the task has been tried.
func (*Task) IsCancelled ¶
func (Task) IsComplete ¶
func (*Task) SetCreatedAt ¶
func (*Task) SetFinishedAt ¶
func (*Task) SetIsCancelled ¶
func (*Task) SetIsComplete ¶
func (*Task) SetIsRunning ¶
func (*Task) SetIsSuccess ¶
func (*Task) SetPriority ¶
func (*Task) SetProgress ¶
func (*Task) SetResult ¶
func (t *Task) SetResult(result interface{})
SetResult sets the result data omitted by the task.
func (*Task) SetStartedAt ¶
func (*Task) SetTryCount ¶
type TaskSpec ¶
type TaskSpec struct { Name string AllowedRetries int RetryInterval time.Duration Handler kit.TaskHandler OnCompleteHandker kit.TaskOnCompleteHandler }
func (TaskSpec) GetAllowedRetries ¶
GetAllowedRetries returns the number of allowed retries.
func (TaskSpec) GetHandler ¶
func (s TaskSpec) GetHandler() kit.TaskHandler
GetHandler returns the TaskHandler function that will execute the task.
func (TaskSpec) GetOnCompleteHandler ¶
func (s TaskSpec) GetOnCompleteHandler() kit.TaskOnCompleteHandler
func (TaskSpec) GetRetryInterval ¶
GetRetryInterval returns the time in seconds that must pass before a retry is attempted.
Click to show internal directories.
Click to hide internal directories.