Skip to content

Commit 4add073

Browse files
committed
Bury command
1 parent 4089d46 commit 4add073

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

beanstool.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func main() {
1515
parser.AddCommand("peek", "peeks a job from a queue", "", &cli.PeekCommand{})
1616
parser.AddCommand("kick", "kicks jobs from buried back into ready", "", &cli.KickCommand{})
1717
parser.AddCommand("put", "put a job into a tube", "", &cli.PutCommand{})
18+
parser.AddCommand("bury", "bury existing jobs from ready state", "", &cli.BuryCommand{})
1819

1920
_, err := parser.Parse()
2021
if err != nil {

cli/bury.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"time"
7+
8+
"github.com/kr/beanstalk"
9+
)
10+
11+
type BuryCommand struct {
12+
Tube string `short:"t" long:"tube" description:"tube to bury jobs in." required:"true"`
13+
Num int `short:"n" long:"num" description:"number of jobs to bury."`
14+
Command
15+
}
16+
17+
func (c *BuryCommand) Execute(args []string) error {
18+
if err := c.Init(); err != nil {
19+
return err
20+
}
21+
22+
if err := c.Bury(); err != nil {
23+
return err
24+
}
25+
26+
return nil
27+
}
28+
29+
func (c *BuryCommand) Bury() error {
30+
if err := c.calcNumIfNeeded(); err != nil {
31+
return err
32+
}
33+
34+
if c.Num == 0 {
35+
fmt.Printf("Empty ready queue at tube %q.\n", c.Tube)
36+
return nil
37+
}
38+
39+
fmt.Printf("Trying to bury %d jobs from %q...\n", c.Num, c.Tube)
40+
41+
count := 0
42+
ts := beanstalk.NewTubeSet(c.conn, c.Tube)
43+
for count < c.Num {
44+
id, _, err := ts.Reserve(time.Hour * 24)
45+
if err != nil {
46+
return err
47+
}
48+
49+
s, err := c.conn.StatsJob(id)
50+
if err != nil {
51+
return err
52+
}
53+
54+
pri, err := strconv.Atoi(s["pri"])
55+
if err != nil {
56+
return err
57+
}
58+
59+
if err := c.conn.Bury(id, uint32(pri)); err != nil {
60+
return err
61+
}
62+
fmt.Println(pri)
63+
count++
64+
}
65+
66+
fmt.Printf("Actually buried %d.\n", count)
67+
return nil
68+
}
69+
70+
func (c *BuryCommand) calcNumIfNeeded() error {
71+
if c.Num == 0 {
72+
s, err := c.GetStatsForTube(c.Tube)
73+
if err != nil {
74+
return err
75+
}
76+
77+
c.Num = s.JobsReady
78+
}
79+
80+
return nil
81+
}

cli/put.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ func (c *PutCommand) Execute(args []string) error {
2222
return err
2323
}
2424

25-
if err := c.Insert(); err != nil {
25+
if err := c.Put(); err != nil {
2626
return err
2727
}
2828

2929
return nil
3030
}
3131

32-
func (c *PutCommand) Insert() error {
32+
func (c *PutCommand) Put() error {
3333
t := beanstalk.Tube{c.conn, c.Tube}
3434

3535
id, err := t.Put([]byte(c.Body), c.Priority, c.Delay, c.TTR)

0 commit comments

Comments
 (0)