summaryrefslogtreecommitdiff
path: root/plugins/jobs/brokers/ephemeral/broker.go
blob: 95f476a63e68591da40f1873d5fd28fd849e7ca0 (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
package ephemeral

import (
	"github.com/google/uuid"
	"github.com/spiral/errors"
	priorityqueue "github.com/spiral/roadrunner/v2/pkg/priority_queue"
	"github.com/spiral/roadrunner/v2/plugins/jobs/pipeline"
	"github.com/spiral/roadrunner/v2/plugins/jobs/structs"
)

type JobBroker struct {
	jobs   chan *entry
	queues map[*pipeline.Pipeline]*queue
	pq     priorityqueue.Queue
}

func NewJobBroker(q priorityqueue.Queue) (*JobBroker, error) {
	jb := &JobBroker{
		jobs: make(chan *entry, 10),
		pq:   q,
	}

	go jb.serve()

	return jb, nil
}

func (j *JobBroker) Push(pipe *pipeline.Pipeline, job *structs.Job) (string, error) {
	id := uuid.NewString()

	j.jobs <- &entry{
		id: id,
	}

	return id, nil
}

func (j *JobBroker) Stat() {
	panic("implement me")
}

func (j *JobBroker) Consume(pipeline *pipeline.Pipeline) {
	panic("implement me")
}

func (j *JobBroker) Register(pipeline *pipeline.Pipeline) error {
	const op = errors.Op("ephemeral_register")
	if _, ok := j.queues[pipeline]; !ok {
		return errors.E(op, errors.Errorf("queue %s has already been registered", pipeline.Name()))
	}

	j.queues[pipeline] = newQueue()

	return nil
}

func (j *JobBroker) serve() {
	for item := range j.jobs {
		// item should satisfy
		j.pq.Push(item)
	}
}