/
pop.go
67 lines (54 loc) · 1.61 KB
/
pop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package command
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
// Pop removes and displays a job from the selected tube.
func (c *Command) Pop(cli *cli.Context) error {
log := c.GetLogger(cli)
// Build and connect to beanstalkd
client, err := c.GetBeanstalkdClient(cli)
if err != nil {
log.WithError(err).Error("Could not connect to beanstalkd server")
return err
}
log.Debug("Selecting correct tube...")
if cli.String("tube") != "default" {
// Watch a specified tube.
if _, err := client.Watch(cli.String("tube")); err != nil {
log.WithError(err).WithField("tube", cli.String("tube")).Error("Failed to select tube")
return err
}
// By default the default tube is always in the watch list.
// To prevent flushing any jobs from the default tube we can ignore it
// after watching a different tube.
if _, err := client.Ignore("default"); err != nil {
log.WithError(err).Error("Failed to ignore default tube")
return err
}
}
// Here we reserve a job to retrieve its contents
log.Debug("Reserving a job...")
job, err := client.Reserve(3)
if err != nil {
if err.Error() == "timed out" {
log.Info("No jobs found in tube, exiting...")
return err
}
log.WithError(err).Error()
return err
}
// After reserving a job, we can delete it
log.WithField("id", job.Id).Debug("Deleting the reserved job")
if err := client.Delete(job.Id); err != nil {
log.WithError(err).WithFields(logrus.Fields{
"tube": cli.String("tube"),
"job": job.Id,
}).Error("Failed to delete job")
return err
}
fmt.Println(string(job.Data[:]))
client.Quit()
return nil
}