-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjob.go
87 lines (73 loc) · 1.91 KB
/
job.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package workers
import (
"strconv"
"time"
"github.com/kr/beanstalk"
)
// Job represents a job received by a worker.
type Job struct {
ID uint64
Tube string
Body []byte
conn *beanstalk.Conn
}
// JobStats represents statistical information about a job.
type JobStats struct {
Priority uint32
Age time.Duration
TimeLeft time.Duration
}
// NewJob creates a Job.
func NewJob(conn *beanstalk.Conn, tube string, id uint64, body []byte) *Job {
return &Job{
ID: id,
Tube: tube,
Body: body,
conn: conn,
}
}
// Delete deletes the current job.
// It removes the job from the server entirely.
func (j *Job) Delete() error {
return j.conn.Delete(j.ID)
}
// Release releases the current job. Release puts the reserved job back
// into the ready queue (and marks its state as ready) to be run by any client.
func (j *Job) Release(pri uint32, delay time.Duration) error {
return j.conn.Release(j.ID, pri, delay)
}
// Touch touches the current job. It allows the worker to request more
// time to work on the job.
func (j *Job) Touch() error {
return j.conn.Touch(j.ID)
}
// Bury buries the current job. Bury puts the job into the "buried" state.
// Buried jobs are put into a FIFO linked list and will not be touched by
// the server again until a client kicks them manually.
func (j *Job) Bury(pri uint32) error {
return j.conn.Bury(j.ID, pri)
}
// Stats gives statistical information about the current job.
func (j *Job) Stats() (*JobStats, error) {
m, err := j.conn.StatsJob(j.ID)
if err != nil {
return nil, err
}
pri, err := strconv.Atoi(m["pri"])
if err != nil {
return nil, err
}
age, err := strconv.Atoi(m["age"])
if err != nil {
return nil, err
}
left, err := strconv.Atoi(m["time-left"])
if err != nil {
return nil, err
}
return &JobStats{
Priority: uint32(pri),
Age: time.Duration(time.Duration(age) * time.Second),
TimeLeft: time.Duration(time.Duration(left) * time.Second),
}, nil
}