Skip to content

Commit

Permalink
delete improved
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Jan 27, 2016
1 parent 65d1861 commit 7c1691e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
2 changes: 1 addition & 1 deletion beanstool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"os"

"github.com/tyba/beanstool/cli"
"github.com/src-d/beanstool/cli"

"github.com/jessevdk/go-flags"
)
Expand Down
25 changes: 23 additions & 2 deletions cli/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
)

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

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

func (c *DeleteCommand) Delete() error {
t := &beanstalk.Tube{c.conn, c.Tube}
for {
if err := c.deleteJob(t); err != nil {
if err.Error() == "peek-ready: not found" {
return nil
}

return err
}

if !c.Empty {
return nil
}
}
}

func (c *DeleteCommand) deleteJob(t *beanstalk.Tube) error {
var id uint64
var body []byte
var err error
Expand All @@ -41,7 +59,10 @@ func (c *DeleteCommand) Delete() error {
return err
}

c.PrintJob(id, body)
if c.Print {
c.PrintJob(id, body)
}

c.conn.Delete(id)

return nil
Expand Down
58 changes: 58 additions & 0 deletions cli/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cli

import (
"github.com/kr/beanstalk"
. "gopkg.in/check.v1"
)

type DeleteCommandSuite struct {
c *DeleteCommand
t *beanstalk.Tube
}

var _ = Suite(&DeleteCommandSuite{})

func (s *DeleteCommandSuite) SetUpTest(c *C) {
s.c = &DeleteCommand{}
s.c.Host = "localhost:11300"
s.c.Init()

s.t = getRandomTube(s.c.conn)
s.c.Tube = s.t.Name
}

func (s *DeleteCommandSuite) TestDeleteReady(c *C) {
s.t.Put([]byte(""), 1024, 0, 0)
s.t.Put([]byte(""), 1024, 0, 0)
s.t.Put([]byte(""), 1024, 0, 0)

stats, _ := s.c.GetStatsForTube(s.c.Tube)
c.Assert(stats.JobsReady, Equals, 3)

s.c.Empty = false
s.c.State = "ready"

err := s.c.Delete()
c.Assert(err, IsNil)

stats, _ = s.c.GetStatsForTube(s.c.Tube)
c.Assert(stats.JobsReady, Equals, 2)
}

func (s *DeleteCommandSuite) TestDeleteReadyEmpty(c *C) {
s.t.Put([]byte(""), 1024, 0, 0)
s.t.Put([]byte(""), 1024, 0, 0)
s.t.Put([]byte(""), 1024, 0, 0)

stats, _ := s.c.GetStatsForTube(s.c.Tube)
c.Assert(stats.JobsReady, Equals, 3)

s.c.Empty = true
s.c.State = "ready"

err := s.c.Delete()
c.Assert(err, IsNil)

stats, _ = s.c.GetStatsForTube(s.c.Tube)
c.Assert(stats.JobsReady, Equals, 0)
}

0 comments on commit 7c1691e

Please sign in to comment.