Skip to content

matt-lewandowski/task

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

codecov Go Report Card Build Status contributions welcome

task

Tasks are useful for handling large, simple, repetitive jobs.
A Task has a defined amount of workers, which share an RPS rate limit.

Installation

go get github.com/matt-lewandowski/task/workers

Getting Started

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors