summaryrefslogtreecommitdiff
path: root/plugins/jobs/brokers/amqp/plugin.go
blob: 7b6562c7ba95ff01390683aaf264f87c076e17bf (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
package amqp

import (
	"sync/atomic"

	"github.com/spiral/roadrunner/v2/common/jobs"
	priorityqueue "github.com/spiral/roadrunner/v2/pkg/priority_queue"
	"github.com/spiral/roadrunner/v2/plugins/config"
	"github.com/spiral/roadrunner/v2/plugins/logger"
)

const (
	pluginName string = "amqp"
)

type Plugin struct {
	log logger.Logger
	cfg config.Configurer

	numConsumers uint32
	stopCh       chan struct{}
}

func (p *Plugin) Init(log logger.Logger, cfg config.Configurer) error {
	p.log = log
	p.cfg = cfg
	p.stopCh = make(chan struct{})
	return nil
}

func (p *Plugin) Serve() chan error {
	return make(chan error)
}

func (p *Plugin) Stop() error {
	// send stop to the all consumers delivery
	for i := uint32(0); i < atomic.LoadUint32(&p.numConsumers); i++ {
		p.stopCh <- struct{}{}
	}
	return nil
}

func (p *Plugin) Name() string {
	return pluginName
}

func (p *Plugin) Available() {}

func (p *Plugin) JobsConstruct(configKey string, pq priorityqueue.Queue) (jobs.Consumer, error) {
	atomic.AddUint32(&p.numConsumers, 1)
	return NewAMQPConsumer(configKey, p.log, p.cfg, p.stopCh, pq)
}