Skip to content

Commit

Permalink
Add stats-job command support.
Browse files Browse the repository at this point in the history
  • Loading branch information
James Cook committed Apr 15, 2014
1 parent 5f2d232 commit 7aa980d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
42 changes: 42 additions & 0 deletions gobeanstalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,48 @@ func (c *Conn) Reserve() (*Job, error) {
return &Job{id, body}, nil
}

/*
Fetch Job Stats
The "stats-job" command is for both producers/consumers and passes through the
raw YAML returned by beanstalkd for the given job ID.
*/
func (c *Conn) StatsJob(id uint64) ([]byte, error) {
//send command and read response
cmd := fmt.Sprintf("stats-job %d\r\n", id)
resp, err := sendGetResp(c, cmd)
if err != nil {
return nil, err
}

//parse response
var bodyLen int

switch {
case strings.Index(resp, "OK") == 0:
_, err = fmt.Sscanf(resp, "OK %d\r\n", &bodyLen)
if err != nil {
return nil, err
}
case resp == "NOT_FOUND\r\n":
return nil, errNotFound
default:
return nil, parseCommonError(resp)
}

//read job body
body := make([]byte, bodyLen+2) //+2 is for trailing \r\n
n, err := io.ReadFull(c.bufReader, body)
if err != nil {
log.Println("failed reading body:", err.Error())
return nil, err
}

body = body[:n-2] //strip \r\n trail

return body, nil
}

//Delete a job
func (c *Conn) Delete(id uint64) error {
cmd := fmt.Sprintf("delete %d\r\n", id)
Expand Down
12 changes: 12 additions & 0 deletions gobeanstalk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ func TestReserve(t *testing.T) {
reserve(t, testtube)
}

func statsJob(t *testing.T, tubename string) {
conn, j := reserve(t, testtube)
yaml, err := conn.StatsJob(j.Id)
if err != nil {
t.Fatal("StatsJob failed.Err = ", err.Error())
}
t.Log(string(yaml))
}
func TestStatsJob(t *testing.T) {
statsJob(t, testtube)
}

func TestDelete(t *testing.T) {
conn, j := reserve(t, testtube)
err := conn.Delete(j.Id)
Expand Down

0 comments on commit 7aa980d

Please sign in to comment.