repositorypackage
0.0.3
Repository: https://github.com/prasanthmj/machine.git
Documentation: pkg.go.dev
# Packages
No description provided by the author
# README
Machine
Wrapper around the awesome machinery go library for background task execution
Status: Work In progress
Why?
- Limited and simplified wrapper.
- Pass around struct as task parameter (gets converted to gob)
Usage
Create the JobQueue. Pass the redis URL as parameter to New()
jq, err := machine.New("redis://127.0.0.1:6379")
jq.Start()
Implement a "TaskExecuter" that can execute a "task" TaskExecuter interface
type TaskExecutor interface {
Execute(interface{}) error
}
Task
type RunnableTask interface {
GetTaskID() string
}
Register the task executer with JobQueue
jq.Register(&OrderEmail{}, orderSvc)
Now push tasks to the queue
task := &OrderEmail{email, orderID}
job := machine.NewJob(&task)
jq.QueueUp(job)
Your TaskExecutor gets called when the task is due
func (*OrderService) Execute(t interface{})error{
switch t.(type){
case OrderEmail:
return sendEmail(t)
case PaymentUpdate:
return updatePayment(t)
}
return nil
}
Delayed jobs
task := &ConfirmationEmail{email}
job := machine.NewJob(task).After(5*time.Minutes)
jq.QueueUp(job)
Stop the queue (and the workers)
jq.Stop()