github.com/songzhibin97/gkit@v1.2.13/distributed/example/worker/worker.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/songzhibin97/gkit/distributed/example"
     7  	"github.com/songzhibin97/gkit/distributed/task"
     8  )
     9  
    10  func main() {
    11  	server := example.InitServer()
    12  	if server == nil {
    13  		panic("server is empty")
    14  	}
    15  	worker := server.NewWorker("worker", 0, server.GetConfig().ConsumeQueue)
    16  	worker.SetErrorHandler(func(err error) {
    17  		fmt.Println("I am an error handler:", err)
    18  	})
    19  	worker.SetBeforeTaskHandler(func(task *task.Signature) {
    20  		fmt.Println("I am a start of task handler for:", task.Name)
    21  	})
    22  
    23  	worker.SetAfterTaskHandler(func(task *task.Signature) {
    24  		fmt.Println("I am a end of task handler for:", task.Name)
    25  	})
    26  	err := worker.Start()
    27  	fmt.Println("end worker:", err)
    28  }