-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
45 lines (39 loc) · 1.03 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
package hop
import (
"github.com/pkg/errors"
"github.com/streadway/amqp"
)
// Job represents a work unit taken from the work queue.
type Job struct {
topic *Topic
del *amqp.Delivery
ch *amqp.Channel
}
// Done marks the Job for queue removal.
func (j *Job) Done() error {
defer j.topic.queue.putChannel(j.ch)
err := j.ch.Ack(j.del.DeliveryTag, false)
if err != nil {
return errors.Wrap(err, "error acknowledging delivery")
}
return nil
}
// Fail marks the Job as failed. If requeue is true, the Job will be added
// back to the queue; otherwise, it will be dropped.
func (j *Job) Fail(requeue bool) error {
defer j.topic.queue.putChannel(j.ch)
err := j.ch.Nack(j.del.DeliveryTag, false, requeue)
if err != nil {
return errors.Wrap(err, "error rejecting delivery")
}
return nil
}
// Body returns the Job's body
func (j *Job) Body() []byte {
return j.del.Body
}
// Delivery returns the Job's underlying delivery. Use this if you need more
// control over the AMQP message.
func (j *Job) Delivery() *amqp.Delivery {
return j.del
}