goworkers is a library in Golang that allow users launch and manage workers
go get github.com/CodelinkIO/goworkers
You can find more samples with different complexities inside samples
folder. Here is a simple sample of how to use:
type helloTask struct {
Message string
}
func (t *helloTask) Type() string {
return "Hello"
}
func (t *helloTask) Complete() {
fmt.Println(fmt.Sprintf("Task %v complete", t.Message))
}
func (t *helloTask) Fail(err error) {
fmt.Println(fmt.Sprintf("Task %v fail: %v", t.Message, err.Error()))
}
func handleHello(ctx context.Context, task tasks.Task) error {
workerContext, ok := ctx.Value(workers.WorkerContextKey).(workers.WorkerContext)
if !ok {
return fmt.Errorf("Cannot get worker context")
}
processingTask, ok := task.(*helloTask)
if !ok {
return fmt.Errorf("Cannot parse task")
}
fmt.Println(fmt.Sprintf("%v: Handle task %v %v", workerContext.ID, processingTask.Type(), processingTask.Message))
time.Sleep(200 * time.Millisecond)
return nil
}
options := workers.ControllerOptions{
NumOfWorker: 10,
}
ctx := context.Background()
router := workers.NewRouter()
router.Register("Hello", handleHello)
trigger := notifiers.NewTrigger()
controller := workers.NewController(ctx, trigger, router, options)
controller.Run()
for i := 0; i < 20; i++ {
trigger.Notify(&helloTask{Message: fmt.Sprintf("tsk_%d", i)})
time.Sleep(100 * time.Millisecond)
}
Thanks goes to these wonderful people (emoji key):
Huy Ngo 🤔 👀 💡 💻 |
Peter Phan 🤔 📖 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!