Skip to content

Commit

Permalink
naive implementation of tail
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Feb 17, 2015
1 parent 414dfa4 commit c282958
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions beanstool.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
func main() {
parser := flags.NewNamedParser("event-relay", flags.Default)
parser.AddCommand("stats", "print stats on all tubes", "", &cli.Monitor{})
parser.AddCommand("tail", "tails a tube and prints his content", "", &cli.Tail{})

_, err := parser.Parse()
if err != nil {
Expand Down
56 changes: 56 additions & 0 deletions cli/tail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cli

import (
"fmt"
"time"

"github.com/kr/beanstalk"
)

type Tail struct {
Host string `short:"h" long:"host" description:"beanstalkd host addr." required:"true" default:"localhost:11300"`
Tube string `short:"t" long:"tube" description:"tube to be tailed." required:"true"`

conn *beanstalk.Conn
}

func (t *Tail) Execute(args []string) error {
if err := t.Init(); err != nil {
return err
}

if err := t.Tail(); err != nil {
return err
}

return nil
}

func (t *Tail) Init() error {
var err error
t.conn, err = beanstalk.Dial("tcp", t.Host)
if err != nil {
return err
}

return nil
}

func (t *Tail) Tail() error {
ts := beanstalk.NewTubeSet(t.conn, t.Tube)

for {
id, body, err := ts.Reserve(time.Hour * 24)
if err != nil {
return err
}

s, _ := t.conn.StatsJob(id)

fmt.Printf(
"id: %d\nlen: %d\npriority: %s\nbody: %q\n\n",
id, len(body), s["pri"], body,
)
}

}

0 comments on commit c282958

Please sign in to comment.