simple-worker is a minimal Go library that allows you to create Queue->Worker systems easily.
go get github.com/simplehq/simple-worker
To use simple-worker, you first need a Processor. A processor is simply a function that takes a interface{} that it will then process.
import (
"fmt"
"github.com/simplehq/simple-worker"
)
type WorkRequest struct {
Message string
}
func ProcessTask(job WorkRequest) {
fmt.Println(job.Message)
}To start a dispatcher, you simply call StartDispatcher.
maxWorkers := 10
maxQueue := 100
queue := worker.StartDispatcher(params.MaxWorkers, workQueue, ProcessTask)StartDispatcher returns a chan interface{} for you to start adding jobs for processing.
queue := worker.StartDispatcher(params.MaxWorkers, workQueue, ProcessTask)
job := WorkRequest{
Message: "Oh Boy"
}
queue <- jobThis would ouput the message "Oh Boy" once the job has been processed.
- Dispatcher interface