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

import (
	"sync"
	"time"

	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/common/jobs"
	"github.com/spiral/roadrunner/v2/pkg/priorityqueue"
	"github.com/spiral/roadrunner/v2/plugins/config"
	"github.com/spiral/roadrunner/v2/plugins/jobs/pipeline"
	"github.com/spiral/roadrunner/v2/plugins/jobs/structs"
	"github.com/spiral/roadrunner/v2/plugins/logger"
	"github.com/streadway/amqp"
)

type Config struct {
	Addr  string
	Queue string
}

type JobsConsumer struct {
	sync.RWMutex
	logger logger.Logger
	pq     priorityqueue.Queue

	pipelines sync.Map

	// amqp connection
	conn          *amqp.Connection
	retryTimeout  time.Duration
	prefetchCount int
	exchangeName  string
	connStr       string
	exchangeType  string
	routingKey    string

	stop chan struct{}
}

func NewAMQPConsumer(configKey string, log logger.Logger, cfg config.Configurer, pq priorityqueue.Queue) (jobs.Consumer, error) {
	// we need to obtain two parts of the amqp information here.
	// firs part - address to connect, it is located in the global section under the amqp name
	// second part - queues and other pipeline information
	jb := &JobsConsumer{
		logger: log,
		pq:     pq,
	}

	d, err := jb.initRabbitMQ()
	if err != nil {
		return nil, err
	}

	// run listener
	jb.listener(d)

	// run redialer
	jb.redialer()

	return jb, nil
}

func (j *JobsConsumer) Push(job *structs.Job) error {
	const op = errors.Op("ephemeral_push")

	// check if the pipeline registered
	if b, ok := j.pipelines.Load(job.Options.Pipeline); ok {
		if !b.(bool) {
			return errors.E(op, errors.Errorf("pipeline disabled: %s", job.Options.Pipeline))
		}

		// handle timeouts
		if job.Options.Timeout > 0 {
			go func(jj *structs.Job) {
				time.Sleep(jj.Options.TimeoutDuration())

				// TODO push

				// send the item after timeout expired
			}(job)

			return nil
		}

		// insert to the local, limited pipeline

		return nil
	}

	return errors.E(op, errors.Errorf("no such pipeline: %s", job.Options.Pipeline))
}

func (j *JobsConsumer) Register(pipeline *pipeline.Pipeline) error {
	panic("implement me")
}

func (j *JobsConsumer) List() []*pipeline.Pipeline {
	panic("implement me")
}

func (j *JobsConsumer) Pause(pipeline string) {
	panic("implement me")
}

func (j *JobsConsumer) Resume(pipeline string) {
	panic("implement me")
}