Skip to content

Commit c282958

Browse files
committed
naive implementation of tail
1 parent 414dfa4 commit c282958

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

beanstool.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
func main() {
1212
parser := flags.NewNamedParser("event-relay", flags.Default)
1313
parser.AddCommand("stats", "print stats on all tubes", "", &cli.Monitor{})
14+
parser.AddCommand("tail", "tails a tube and prints his content", "", &cli.Tail{})
1415

1516
_, err := parser.Parse()
1617
if err != nil {

cli/tail.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/kr/beanstalk"
8+
)
9+
10+
type Tail struct {
11+
Host string `short:"h" long:"host" description:"beanstalkd host addr." required:"true" default:"localhost:11300"`
12+
Tube string `short:"t" long:"tube" description:"tube to be tailed." required:"true"`
13+
14+
conn *beanstalk.Conn
15+
}
16+
17+
func (t *Tail) Execute(args []string) error {
18+
if err := t.Init(); err != nil {
19+
return err
20+
}
21+
22+
if err := t.Tail(); err != nil {
23+
return err
24+
}
25+
26+
return nil
27+
}
28+
29+
func (t *Tail) Init() error {
30+
var err error
31+
t.conn, err = beanstalk.Dial("tcp", t.Host)
32+
if err != nil {
33+
return err
34+
}
35+
36+
return nil
37+
}
38+
39+
func (t *Tail) Tail() error {
40+
ts := beanstalk.NewTubeSet(t.conn, t.Tube)
41+
42+
for {
43+
id, body, err := ts.Reserve(time.Hour * 24)
44+
if err != nil {
45+
return err
46+
}
47+
48+
s, _ := t.conn.StatsJob(id)
49+
50+
fmt.Printf(
51+
"id: %d\nlen: %d\npriority: %s\nbody: %q\n\n",
52+
id, len(body), s["pri"], body,
53+
)
54+
}
55+
56+
}

0 commit comments

Comments
 (0)