Skip to content

Commit 7c1691e

Browse files
committed
delete improved
1 parent 65d1861 commit 7c1691e

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

beanstool.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"os"
55

6-
"github.com/tyba/beanstool/cli"
6+
"github.com/src-d/beanstool/cli"
77

88
"github.com/jessevdk/go-flags"
99
)

cli/delete.go

+23-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
)
66

77
type DeleteCommand struct {
8-
Tube string `short:"t" long:"tube" description:"tube to be tailed." required:"true"`
8+
Tube string `short:"t" long:"tube" description:"tube to be delete." required:"true"`
99
State string `short:"" long:"state" description:"peek from 'buried', 'ready' or 'delayed' queues." default:"buried"`
10+
Print bool `short:"" long:"print" description:"prints the jobs after delete it." default:"true"`
11+
Empty bool `short:"" long:"empty" description:"delete all jobs with the given status in the given tube." default:"false"`
1012
Command
1113
}
1214

@@ -24,6 +26,22 @@ func (c *DeleteCommand) Execute(args []string) error {
2426

2527
func (c *DeleteCommand) Delete() error {
2628
t := &beanstalk.Tube{c.conn, c.Tube}
29+
for {
30+
if err := c.deleteJob(t); err != nil {
31+
if err.Error() == "peek-ready: not found" {
32+
return nil
33+
}
34+
35+
return err
36+
}
37+
38+
if !c.Empty {
39+
return nil
40+
}
41+
}
42+
}
43+
44+
func (c *DeleteCommand) deleteJob(t *beanstalk.Tube) error {
2745
var id uint64
2846
var body []byte
2947
var err error
@@ -41,7 +59,10 @@ func (c *DeleteCommand) Delete() error {
4159
return err
4260
}
4361

44-
c.PrintJob(id, body)
62+
if c.Print {
63+
c.PrintJob(id, body)
64+
}
65+
4566
c.conn.Delete(id)
4667

4768
return nil

cli/delete_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cli
2+
3+
import (
4+
"github.com/kr/beanstalk"
5+
. "gopkg.in/check.v1"
6+
)
7+
8+
type DeleteCommandSuite struct {
9+
c *DeleteCommand
10+
t *beanstalk.Tube
11+
}
12+
13+
var _ = Suite(&DeleteCommandSuite{})
14+
15+
func (s *DeleteCommandSuite) SetUpTest(c *C) {
16+
s.c = &DeleteCommand{}
17+
s.c.Host = "localhost:11300"
18+
s.c.Init()
19+
20+
s.t = getRandomTube(s.c.conn)
21+
s.c.Tube = s.t.Name
22+
}
23+
24+
func (s *DeleteCommandSuite) TestDeleteReady(c *C) {
25+
s.t.Put([]byte(""), 1024, 0, 0)
26+
s.t.Put([]byte(""), 1024, 0, 0)
27+
s.t.Put([]byte(""), 1024, 0, 0)
28+
29+
stats, _ := s.c.GetStatsForTube(s.c.Tube)
30+
c.Assert(stats.JobsReady, Equals, 3)
31+
32+
s.c.Empty = false
33+
s.c.State = "ready"
34+
35+
err := s.c.Delete()
36+
c.Assert(err, IsNil)
37+
38+
stats, _ = s.c.GetStatsForTube(s.c.Tube)
39+
c.Assert(stats.JobsReady, Equals, 2)
40+
}
41+
42+
func (s *DeleteCommandSuite) TestDeleteReadyEmpty(c *C) {
43+
s.t.Put([]byte(""), 1024, 0, 0)
44+
s.t.Put([]byte(""), 1024, 0, 0)
45+
s.t.Put([]byte(""), 1024, 0, 0)
46+
47+
stats, _ := s.c.GetStatsForTube(s.c.Tube)
48+
c.Assert(stats.JobsReady, Equals, 3)
49+
50+
s.c.Empty = true
51+
s.c.State = "ready"
52+
53+
err := s.c.Delete()
54+
c.Assert(err, IsNil)
55+
56+
stats, _ = s.c.GetStatsForTube(s.c.Tube)
57+
c.Assert(stats.JobsReady, Equals, 0)
58+
}

0 commit comments

Comments
 (0)