Пример #1
0
func main() {
	wg := worker.NewGroup(nil)

	wg.Add(firstTube, first)
	wg.Add(secondTube, second)

	wg.Run()
	defer wg.Shutdown()

	a := &firstData{
		A: 2,
	}

	resp, err := worker.Send(firstTube, a, true, nil)
	if err != nil {
		log.Println("err:", err)
		return
	}

	var data int
	err = json.Unmarshal(resp, &data)
	if err != nil {
		log.Println("err:", err)
		return
	}

	log.Println("2 + 2 =", data)
}
Пример #2
0
func main() {
	add := worker.New(addResponseTube, add, nil)
	add.Run()

	defer func() {
		f := make(chan struct{})
		add.Shutdown(f)
		<-f
	}()

	a := &addData{
		A: 2,
		B: 2,
	}

	resp, err := worker.Send(addResponseTube, a, true, nil)
	if err != nil {
		log.Println("err:", err)
		return
	}

	var data int
	err = json.Unmarshal(resp, &data)
	if err != nil {
		log.Println("err:", err)
		return
	}

	log.Println("2 + 2 =", data)
}
Пример #3
0
// Typically, the Run and Send functions would not be called
// from the same process, but, for ease of demonstration, they
// are combined for this example.
func main() {
	// define and run a worker
	add := worker.New(addTube, add, nil)
	add.Run()

	// shutdown the worker on exit
	defer func() {
		f := make(chan struct{})
		add.Shutdown(f)
		<-f
	}()

	// create a unit of work
	a := addData{
		A: 2,
		B: 2,
	}

	// send it to our worker
	_, err := worker.Send(addTube, a, false, nil)
	if err != nil {
		log.Println("err:", err)
	}
}
Пример #4
0
	B int `json:"b"`
}

var first = func(req *worker.Request) (res worker.Response) {
	f := &firstData{}
	err := json.Unmarshal(req.Data, f)
	if err != nil {
		return req.RetryJob(err, 3, nil)
	}

	s := &secondData{
		A: f.A,
		B: 2,
	}

	resp, err := worker.Send(secondTube, s, true, nil)
	if err != nil {
		log.Println("err:", err)
	}

	var data int
	err = json.Unmarshal(resp, &data)
	if err != nil {
		log.Println("err:", err)
		return
	}

	res.Data = data
	return
}