Tasks are useful for handling large, simple, repetitive jobs.
A Task has a defined amount of workers, which share an RPS rate limit.
go get github.com/matt-lewandowski/task/workers
Three functions need to be passed in when creating a task worker
- The workerFunction will receive each task, process it, and return the results/error.
workerFunction := func(ctx context.Context, task interface{}) (interface{}, error) {
err, response := someClientRequest(ctx, task.(requestStruct))
return response, err
}- The errorFunction will receive errors, and has the ability to stop the process.
errorFunction := func(result workers.JobData, stop func()) {
fmt.Println("ERROR: Stopping the job")
stop()
}- The resultFunction will handle further processing of results from the workerFunction.
resultFunction := func(result workers.JobData) {
fmt.Println(result.Result)
}Then you can just create a new task and tell it to start
task := workers.NewTask(workers.Config{
Workers: 250,
RateLimit: 50,
Jobs: tasks,
HandlerFunction: workerFunction,
ErrorHandler: errorFunction,
ResultHandler: resultFunction,
})
task.Start()A more detailed example can be found in example.go