-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnotify_beanstalk.go
More file actions
65 lines (53 loc) · 1.1 KB
/
notify_beanstalk.go
File metadata and controls
65 lines (53 loc) · 1.1 KB
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
package main
import (
"bytes"
"log"
"text/template"
"time"
"github.com/kr/beanstalk"
)
type NotifyBeanstalkd struct {
addr string
tube string
t *template.Template
Deadline time.Duration
Priority uint32
Delay time.Duration
}
func NewNotifyBeanstalkd(addr, tube, body_template string) NotifyBeanstalkd {
return NotifyBeanstalkd{
addr: addr,
tube: tube,
t: template.Must(template.New("body").Parse(body_template)),
Deadline: time.Minute,
Priority: 10,
Delay: 0,
}
}
func (n NotifyBeanstalkd) Notify(p *Payload) {
body := n.parseBody(p)
if len(body) == 0 {
log.Println("Notify body is empty!")
return
}
bs, err := beanstalk.Dial("tcp", n.addr)
if err != nil {
log.Println(err)
return
}
defer bs.Close()
tube := &beanstalk.Tube{bs, n.tube}
job_id, err := tube.Put(body, n.Priority, n.Delay, n.Deadline)
if err != nil {
log.Println(err)
return
}
log.Printf("Added job with id %d\n", job_id)
}
func (n NotifyBeanstalkd) parseBody(p *Payload) []byte {
var buf bytes.Buffer
if err := n.t.Execute(&buf, p); err != nil {
log.Println(err)
}
return buf.Bytes()
}