summaryrefslogtreecommitdiff
path: root/plugins/jobs/broker/amqp/queue.go
blob: 6ef5f20f319dbfb7ad31c3e9a99f33504586f58c (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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package amqp

import (
	"errors"
	"fmt"
	"github.com/spiral/jobs/v2"
	"github.com/streadway/amqp"
	"os"
	"sync"
	"sync/atomic"
	"time"
)

type ExchangeType string

const (
	Direct  ExchangeType = "direct"
	Fanout  ExchangeType = "fanout"
	Topic   ExchangeType = "topic"
	Headers ExchangeType = "headers"
)

func (et ExchangeType) IsValid() error {
	switch et {
	case Direct, Fanout, Topic, Headers:
		return nil
	}
	return errors.New("unknown exchange-type")
}

func (et ExchangeType) String() string {
	switch et {
	case Direct, Fanout, Topic, Headers:
		return string(et)
	default:
		return "direct"
	}
}


type queue struct {
	active                 int32
	pipe                   *jobs.Pipeline
	exchange               string
	exchangeType           ExchangeType
	name, key              string
	consumer               string

	// active consuming channel
	muc sync.Mutex
	cc  *channel

	// queue events
	lsn func(event int, ctx interface{})

	// active operations
	muw sync.RWMutex
	wg  sync.WaitGroup

	// exec handlers
	running    int32
	execPool   chan jobs.Handler
	errHandler jobs.ErrorHandler
}

// newQueue creates new queue wrapper for AMQP.
func newQueue(pipe *jobs.Pipeline, lsn func(event int, ctx interface{})) (*queue, error) {
	if pipe.String("queue", "") == "" {
		return nil, fmt.Errorf("missing `queue` parameter on amqp pipeline")
	}

	exchangeType := ExchangeType(pipe.String("exchange-type", "direct"))

	err := exchangeType.IsValid()
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &queue{
		exchange: pipe.String("exchange", "amqp.direct"),
		exchangeType: exchangeType,
		name:     pipe.String("queue", ""),
		key:      pipe.String("routing-key", pipe.String("queue", "")),
		consumer: pipe.String("consumer", fmt.Sprintf("rr-jobs:%s-%v", pipe.Name(), os.Getpid())),
		pipe:     pipe,
		lsn:      lsn,
	}, nil
}

// serve consumes queue
func (q *queue) serve(publish, consume *chanPool) {
	atomic.StoreInt32(&q.active, 1)

	for {
		<-consume.waitConnected()
		if atomic.LoadInt32(&q.active) == 0 {
			// stopped
			return
		}

		delivery, cc, err := q.consume(consume)
		if err != nil {
			q.report(err)
			continue
		}

		q.muc.Lock()
		q.cc = cc
		q.muc.Unlock()

		for d := range delivery {
			q.muw.Lock()
			q.wg.Add(1)
			q.muw.Unlock()

			atomic.AddInt32(&q.running, 1)
			h := <-q.execPool

			go func(h jobs.Handler, d amqp.Delivery) {
				err := q.do(publish, h, d)

				atomic.AddInt32(&q.running, ^int32(0))
				q.execPool <- h
				q.wg.Done()
				q.report(err)
			}(h, d)
		}
	}
}

func (q *queue) consume(consume *chanPool) (jobs <-chan amqp.Delivery, cc *channel, err error) {
	// allocate channel for the consuming
	if cc, err = consume.channel(q.name); err != nil {
		return nil, nil, err
	}

	if err := cc.ch.Qos(q.pipe.Integer("prefetch", 4), 0, false); err != nil {
		return nil, nil, consume.closeChan(cc, err)
	}

	delivery, err := cc.ch.Consume(q.name, q.consumer, false, false, false, false, nil)
	if err != nil {
		return nil, nil, consume.closeChan(cc, err)
	}

	// do i like it?
	go func(consume *chanPool) {
		for err := range cc.signal {
			consume.closeChan(cc, err)
			return
		}
	}(consume)

	return delivery, cc, err
}

func (q *queue) do(cp *chanPool, h jobs.Handler, d amqp.Delivery) error {
	id, attempt, j, err := unpack(d)
	if err != nil {
		q.report(err)
		return d.Nack(false, false)
	}
	err = h(id, j)

	if err == nil {
		return d.Ack(false)
	}

	// failed
	q.errHandler(id, j, err)

	if !j.Options.CanRetry(attempt) {
		return d.Nack(false, false)
	}

	// retry as new j (to accommodate attempt number and new delay)
	if err = q.publish(cp, id, attempt+1, j, j.Options.RetryDuration()); err != nil {
		q.report(err)
		return d.Nack(false, true)
	}

	return d.Ack(false)
}

func (q *queue) stop() {
	if atomic.LoadInt32(&q.active) == 0 {
		return
	}

	atomic.StoreInt32(&q.active, 0)

	q.muc.Lock()
	if q.cc != nil {
		// gracefully stopped consuming
		q.report(q.cc.ch.Cancel(q.consumer, true))
	}
	q.muc.Unlock()

	q.muw.Lock()
	q.wg.Wait()
	q.muw.Unlock()
}

// publish message to queue or to delayed queue.
func (q *queue) publish(cp *chanPool, id string, attempt int, j *jobs.Job, delay time.Duration) error {
	c, err := cp.channel(q.name)
	if err != nil {
		return err
	}

	qKey := q.key

	if delay != 0 {
		delayMs := int64(delay.Seconds() * 1000)
		qName := fmt.Sprintf("delayed-%d.%s.%s", delayMs, q.exchange, q.name)
		qKey = qName

		err := q.declare(cp, qName, qName, amqp.Table{
			"x-dead-letter-exchange":    q.exchange,
			"x-dead-letter-routing-key": q.name,
			"x-message-ttl":             delayMs,
			"x-expires":                 delayMs * 2,
		})

		if err != nil {
			return err
		}
	}

	err = c.ch.Publish(
		q.exchange, // exchange
		qKey,      // routing key
		false,      // mandatory
		false,      // immediate
		amqp.Publishing{
			ContentType:  "application/octet-stream",
			Body:         j.Body(),
			DeliveryMode: amqp.Persistent,
			Headers:      pack(id, attempt, j),
		},
	)

	if err != nil {
		return cp.closeChan(c, err)
	}

	confirmed, ok := <-c.confirm
	if ok && confirmed.Ack {
		return nil
	}

	return fmt.Errorf("failed to publish: %v", confirmed.DeliveryTag)
}

// declare queue and binding to it
func (q *queue) declare(cp *chanPool, queue string, key string, args amqp.Table) error {
	c, err := cp.channel(q.name)
	if err != nil {
		return err
	}

	err = c.ch.ExchangeDeclare(q.exchange, q.exchangeType.String(), true, false, false, false, nil)
	if err != nil {
		return cp.closeChan(c, err)
	}

	_, err = c.ch.QueueDeclare(queue, true, false, false, false, args)
	if err != nil {
		return cp.closeChan(c, err)
	}

	err = c.ch.QueueBind(queue, key, q.exchange, false, nil)
	if err != nil {
		return cp.closeChan(c, err)
	}

	// keep channel open
	return err
}

// inspect the queue
func (q *queue) inspect(cp *chanPool) (*amqp.Queue, error) {
	c, err := cp.channel("stat")
	if err != nil {
		return nil, err
	}

	queue, err := c.ch.QueueInspect(q.name)
	if err != nil {
		return nil, cp.closeChan(c, err)
	}

	// keep channel open
	return &queue, err
}

// throw handles service, server and pool events.
func (q *queue) report(err error) {
	if err != nil {
		q.lsn(jobs.EventPipeError, &jobs.PipelineError{Pipeline: q.pipe, Caused: err})
	}
}