-
Notifications
You must be signed in to change notification settings - Fork 111
/
toBeanstalkd.go
102 lines (97 loc) · 2.6 KB
/
toBeanstalkd.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
92
93
94
95
96
97
98
99
100
101
102
package library
import (
"encoding/json"
"errors"
"github.com/nutrun/lentil"
"github.com/nytlabs/streamtools/st/blocks" // blocks
"github.com/nytlabs/streamtools/st/util"
)
// specify those channels we're going to use to communicate with streamtools
type ToBeanstalkd struct {
blocks.Block
queryrule chan chan interface{}
inrule chan interface{}
in chan interface{}
quit chan interface{}
}
// we need to build a simple factory so that streamtools can make new blocks of this kind
func NewToBeanstalkd() blocks.BlockInterface {
return &ToBeanstalkd{}
}
// Setup is called once before running the block. We build up the channels and specify what kind of block this is.
func (b *ToBeanstalkd) Setup() {
b.Kind = "ToBeanstalkd"
b.Desc = "sends jobs to beanstalkd tube"
b.in = b.InRoute("in")
b.inrule = b.InRoute("rule")
b.queryrule = b.QueryRoute("rule")
b.quit = b.Quit()
}
// Run is the block's main loop. Here we listen on the different channels we set up.
func (b *ToBeanstalkd) Run() {
var conn *lentil.Beanstalkd
var tube = "default"
var ttr = 0
var host = ""
var err error
for {
select {
case msgI := <-b.inrule:
// set hostname for beanstalkd server
host, err = util.ParseString(msgI, "Host")
if err != nil {
b.Error(err.Error())
continue
}
// set tube name
tube, err = util.ParseString(msgI, "Tube")
if err != nil {
b.Error(errors.New("Could not parse tube name, setting to 'default'"))
tube = "default"
}
// set time to reserve
ttr, err = util.ParseInt(msgI, "TTR")
if err != nil || ttr < 0 {
b.Error(errors.New("Error parsing TTR. Setting TTR to 0"))
ttr = 0
}
// create beanstalkd connection
conn, err = lentil.Dial(host)
if err != nil {
// swallowing a panic from lentil here - streamtools must not die
b.Error(errors.New("Could not initiate connection with beanstalkd server"))
continue
}
// use the specified tube
conn.Use(tube)
case <-b.quit:
// close connection to beanstalkd and quit
if conn != nil {
conn.Quit()
}
return
case msg := <-b.in:
// deal with inbound data
msgStr, err := json.Marshal(msg)
if err != nil {
b.Error(err)
continue
}
if conn != nil {
_, err := conn.Put(0, 0, ttr, msgStr)
if err != nil {
b.Error(err.Error())
}
} else {
b.Error(errors.New("Beanstalkd connection not initated or lost. Please check your beanstalkd server or block settings."))
}
case respChan := <-b.queryrule:
// deal with a query request
respChan <- map[string]interface{}{
"Host": host,
"Tube": tube,
"TTR": ttr,
}
}
}
}