summaryrefslogtreecommitdiff
path: root/plugins/jobs/oooold/broker/amqp/job.go
blob: bd559715b7ed136345bccd6241cd707f6d661a59 (plain)
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
package amqp

import (
	"fmt"
	"github.com/spiral/jobs/v2"
	"github.com/streadway/amqp"
)

// pack job metadata into headers
func pack(id string, attempt int, j *jobs.Job) amqp.Table {
	return amqp.Table{
		"rr-id":          id,
		"rr-job":         j.Job,
		"rr-attempt":     int64(attempt),
		"rr-maxAttempts": int64(j.Options.Attempts),
		"rr-timeout":     int64(j.Options.Timeout),
		"rr-delay":       int64(j.Options.Delay),
		"rr-retryDelay":  int64(j.Options.RetryDelay),
	}
}

// unpack restores jobs.Options
func unpack(d amqp.Delivery) (id string, attempt int, j *jobs.Job, err error) {
	j = &jobs.Job{Payload: string(d.Body), Options: &jobs.Options{}}

	if _, ok := d.Headers["rr-id"].(string); !ok {
		return "", 0, nil, fmt.Errorf("missing header `%s`", "rr-id")
	}

	if _, ok := d.Headers["rr-attempt"].(int64); !ok {
		return "", 0, nil, fmt.Errorf("missing header `%s`", "rr-attempt")
	}

	if _, ok := d.Headers["rr-job"].(string); !ok {
		return "", 0, nil, fmt.Errorf("missing header `%s`", "rr-job")
	}
	j.Job = d.Headers["rr-job"].(string)

	if _, ok := d.Headers["rr-maxAttempts"].(int64); ok {
		j.Options.Attempts = int(d.Headers["rr-maxAttempts"].(int64))
	}

	if _, ok := d.Headers["rr-timeout"].(int64); ok {
		j.Options.Timeout = int(d.Headers["rr-timeout"].(int64))
	}

	if _, ok := d.Headers["rr-delay"].(int64); ok {
		j.Options.Delay = int(d.Headers["rr-delay"].(int64))
	}

	if _, ok := d.Headers["rr-retryDelay"].(int64); ok {
		j.Options.RetryDelay = int(d.Headers["rr-retryDelay"].(int64))
	}

	return d.Headers["rr-id"].(string), int(d.Headers["rr-attempt"].(int64)), j, nil
}