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

import (
	"github.com/spiral/jobs/v2"
	"sync"
	"sync/atomic"
	"time"
)

type queue struct {
	on    int32
	state *jobs.Stat

	// job pipeline
	concurPool chan interface{}
	jobs       chan *entry

	// on operations
	muw sync.Mutex
	wg  sync.WaitGroup

	// stop channel
	wait chan interface{}

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

type entry struct {
	id      string
	job     *jobs.Job
	attempt int
}

// create new queue
func newQueue(maxConcur int) *queue {
	q := &queue{state: &jobs.Stat{}, jobs: make(chan *entry)}

	if maxConcur != 0 {
		q.concurPool = make(chan interface{}, maxConcur)
		for i := 0; i < maxConcur; i++ {
			q.concurPool <- nil
		}
	}

	return q
}

// serve consumers
func (q *queue) serve() {
	q.wait = make(chan interface{})
	atomic.StoreInt32(&q.on, 1)

	for {
		e := q.consume()
		if e == nil {
			q.wg.Wait()
			return
		}

		if q.concurPool != nil {
			<-q.concurPool
		}

		atomic.AddInt64(&q.state.Active, 1)
		h := <-q.execPool

		go func(h jobs.Handler, e *entry) {
			defer q.wg.Done()

			q.do(h, e)
			atomic.AddInt64(&q.state.Active, ^int64(0))

			q.execPool <- h

			if q.concurPool != nil {
				q.concurPool <- nil
			}
		}(h, e)
	}
}

// allocate one job entry
func (q *queue) consume() *entry {
	q.muw.Lock()
	defer q.muw.Unlock()

	select {
	case <-q.wait:
		return nil
	case e := <-q.jobs:
		q.wg.Add(1)

		return e
	}
}

// do singe job
func (q *queue) do(h jobs.Handler, e *entry) {
	err := h(e.id, e.job)

	if err == nil {
		atomic.AddInt64(&q.state.Queue, ^int64(0))
		return
	}

	q.errHandler(e.id, e.job, err)

	if !e.job.Options.CanRetry(e.attempt) {
		atomic.AddInt64(&q.state.Queue, ^int64(0))
		return
	}

	q.push(e.id, e.job, e.attempt+1, e.job.Options.RetryDuration())
}

// stop the queue consuming
func (q *queue) stop() {
	if atomic.LoadInt32(&q.on) == 0 {
		return
	}

	close(q.wait)

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

	atomic.StoreInt32(&q.on, 0)
}

// add job to the queue
func (q *queue) push(id string, j *jobs.Job, attempt int, delay time.Duration) {
	if delay == 0 {
		atomic.AddInt64(&q.state.Queue, 1)
		go func() {
			q.jobs <- &entry{id: id, job: j, attempt: attempt}
		}()

		return
	}

	atomic.AddInt64(&q.state.Delayed, 1)
	go func() {
		time.Sleep(delay)
		atomic.AddInt64(&q.state.Delayed, ^int64(0))
		atomic.AddInt64(&q.state.Queue, 1)

		q.jobs <- &entry{id: id, job: j, attempt: attempt}
	}()
}

func (q *queue) stat() *jobs.Stat {
	return &jobs.Stat{
		InternalName: ":memory:",
		Queue:        atomic.LoadInt64(&q.state.Queue),
		Active:       atomic.LoadInt64(&q.state.Active),
		Delayed:      atomic.LoadInt64(&q.state.Delayed),
	}
}