-
Notifications
You must be signed in to change notification settings - Fork 0
/
beanstalk.go
91 lines (72 loc) · 1.86 KB
/
beanstalk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
"time"
"log"
"os"
"strings"
"encoding/json"
"github.com/beanstalkd/go-beanstalk"
)
// beanstalk config struct
type BeanstalkConfig struct {
Uri string `json:"uri"`
Tube string `json:"tube"`
ReplyTubePrefix string `json:"reply_tube_prefix"`
ReconnectTimeout int `json:"reconnect_timeout"`
ReserveTimeout int `json:"reserve_timeout"`
PublishTimeout int `json:"publish_timeout"`
}
func beanstalkSend(config BeanstalkConfig, body string) error {
amqpURI := config.Uri
tube := config.Tube
c, err := beanstalk.Dial("tcp", amqpURI)
if err != nil {
log.Printf("Unable connect to beanstalkd broker:%s", err)
return nil
}
mytube := &beanstalk.Tube{Conn: c, Name: tube}
id, err := mytube.Put([]byte(body), 1, 0, time.Duration(config.PublishTimeout)*time.Second)
if err != nil {
fmt.Printf("\nerr: %d\n",err)
panic(err)
}
callbackQueueName := fmt.Sprintf("%s%d",config.ReplyTubePrefix,id)
fmt.Printf("got id: %d,callback queue name: %s\n",id,callbackQueueName)
c1 := make(chan string)
go func() {
// todo: global timeout
for {
c.TubeSet = *beanstalk.NewTubeSet(c, callbackQueueName)
id, body, err := c.Reserve(time.Duration(config.ReserveTimeout) * time.Second)
if err != nil {
fmt.Printf("\nid: %d, res: %s\n",id, err.Error())
}
if id == 0 {
continue
}
cbsdTask := CbsdTask{}
err = json.Unmarshal(body, &cbsdTask)
if err != nil {
log.Printf("json decode error %s", err.Error())
c.Delete(id)
panic(err)
}
fmt.Printf("%s\n",cbsdTask.Message)
if cbsdTask.Progress==100 {
c1 <- "EOF"
}
c.Delete(id)
}
}()
select {
case msg1 := <-c1:
if strings.Compare(msg1,"EOF") == 0 {
os.Exit(0)
} else {
fmt.Println("received:", msg1)
os.Exit(1)
}
}
return nil
}