Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrHandleCronFailed = errors.New("failed to handle cron") ErrServerStartFailed = errors.New("failed to start the worker server") ErrClientStartFailed = errors.New("failed to start the worker client") ErrTaskPatternInvalid = errors.New("task pattern is invalid") ErrCronSpecInvalid = errors.New("cron specification is invalid") ErrSubmitFailed = errors.New("failed to submit the payload") )
The errors this package returns. They are compared with errors.Is, so a caller can tell a misconfigured worker (an invalid pattern or spec, caught at registration) from a runtime failure.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client interface {
// Submit sends a payload to be processed by the task handler registered with the specified pattern.
// The task will be executed immediately if it matches the pattern.
//
// It returns an error if the pattern is invalid or if there is an issue submitting the task.
Submit(ctx context.Context, pattern TaskPattern, payload []byte) error
// Close closes the client's connection.
Close() error
}
Client represents a client that submits tasks to be handled by the server.
type CronHandler ¶
CronHandler runs one scheduled execution. It takes no payload — a cron job's only input is the fact that it is time.
type CronSpec ¶
type CronSpec string
CronSpec is a cron expression in the five-field form. Validate before use: an invalid spec would otherwise register a job that never fires.
func (CronSpec) MustValidate ¶
func (cs CronSpec) MustValidate()
MustValidate is similar to CronSpec.Validate but panics when invalid.
type Cronjob ¶
type Cronjob struct {
// Identifier is a UUID for the cron job, used internally to register the task with the
// scheduler.
Identifier string
// Spec is the cron expression that defines the schedule for the cron job.
Spec CronSpec
// Handler is the callback function that will be executed when the cron specification is met.
Handler CronHandler
// Unique defines whether the task cannot be perfomed concurrently.
Unique bool
}
Cronjob binds a handler to a schedule. Unique is what stops a long run from overlapping the next tick, which matters for jobs that sweep shared state.
type CronjobOption ¶ added in v0.17.2
type CronjobOption func(c *Cronjob)
CronjobOption configures a cron job at registration.
type Server ¶
type Server interface {
// HandleTask registers a task with the provided pattern. The task will be executed
// every time a client enqueues a payload matching the pattern. Batch tasks will be executed
// when the client enqueues a series of payloads or when the specified time delay is reached.
//
// It panics if the pattern is invalid. Batch tasks are specified in options.
HandleTask(pattern TaskPattern, cb TaskHandler, opts ...TaskOption)
// HandleCron executes the cronFunc every time the cron specification is met.
//
// It panics if the cron specification is invalid.
HandleCron(spec CronSpec, cronFunc CronHandler, opts ...CronjobOption)
// Start initializes and starts the worker in a non-blocking manner. The server is
// turned off whedn the context was done.
//
// It returns an error if any issues occur during the startup process.
Start() error
// Shutdown gracefully shuts down the server.
Shutdown()
}
Server represents a process that handles tasks and cronjobs. A Client can submit tasks.
type Task ¶
type Task struct {
// Pattern is a string to which the task can listen to message/events.
Pattern TaskPattern
// Handler is the callback that the task will execute when receiving messages/events.
Handler TaskHandler
}
Task binds a handler to the pattern it listens on. Build one with NewTask rather than by hand, so the pattern is validated before the worker starts.
type TaskHandler ¶
TaskHandler processes one message. Returning an error hands the message back to the queue for retry, so a handler must be idempotent.
type TaskPattern ¶
type TaskPattern string
TaskPattern represents a pattern to which a task can register to listen. It must be in the format "queue:kind".
func (TaskPattern) MustValidate ¶
func (tp TaskPattern) MustValidate()
MustValidate is similar to TaskPattern.Validate but panics when invalid.
func (TaskPattern) Queue ¶
func (tp TaskPattern) Queue() string
Queue returns the queue component of the pattern.
func (TaskPattern) String ¶
func (tp TaskPattern) String() string
func (TaskPattern) Validate ¶
func (tp TaskPattern) Validate() bool
Validate reports whether the pattern is valid or not.