-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtail.go
More file actions
77 lines (59 loc) · 1.33 KB
/
tail.go
File metadata and controls
77 lines (59 loc) · 1.33 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
66
67
68
69
70
71
72
73
74
75
76
77
package cli
import (
"errors"
"fmt"
"strings"
"time"
"github.com/kr/beanstalk"
)
var TooManyErrorsError = errors.New("Too many errors")
type TailCommand struct {
Tube string `short:"t" long:"tube" description:"tube to be tailed." required:"true"`
Action string `short:"" long:"action" description:"action to perform after reserver the job. (release, bury, delete)" default:"release"`
Command
}
func (c *TailCommand) Execute(args []string) error {
if err := c.Init(); err != nil {
return err
}
return c.Tail()
}
func (c *TailCommand) Tail() error {
ts := beanstalk.NewTubeSet(c.conn, c.Tube)
errors := 0
for {
if errors > 100 {
return TooManyErrorsError
}
id, body, err := ts.Reserve(time.Hour * 24)
if err != nil {
if err.Error() != "reserve-with-timeout: deadline soon" {
errors++
fmt.Println("Error", err)
}
continue
}
if err := c.PrintJob(id, body); err != nil {
errors++
fmt.Println("Error", err)
continue
}
if err := c.postPrintAction(id); err != nil {
return err
}
fmt.Println(strings.Repeat("-", 80))
}
return nil
}
func (c *TailCommand) postPrintAction(id uint64) error {
var err error
switch c.Action {
case "release":
err = c.conn.Release(id, 1024, 0)
case "bury":
err = c.conn.Bury(id, 1024)
case "delete":
err = c.conn.Delete(id)
}
return err
}