summaryrefslogtreecommitdiffstats
path: root/modules/queue/queue.go
blob: a166a935a6777ea00e6b63878f9cf3218a7c2c51 (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
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package queue

import (
	"context"
	"fmt"
	"time"
)

// ErrInvalidConfiguration is called when there is invalid configuration for a queue
type ErrInvalidConfiguration struct {
	cfg interface{}
	err error
}

func (err ErrInvalidConfiguration) Error() string {
	if err.err != nil {
		return fmt.Sprintf("Invalid Configuration Argument: %v: Error: %v", err.cfg, err.err)
	}
	return fmt.Sprintf("Invalid Configuration Argument: %v", err.cfg)
}

// IsErrInvalidConfiguration checks if an error is an ErrInvalidConfiguration
func IsErrInvalidConfiguration(err error) bool {
	_, ok := err.(ErrInvalidConfiguration)
	return ok
}

// Type is a type of Queue
type Type string

// Data defines an type of queuable data
type Data interface{}

// HandlerFunc is a function that takes a variable amount of data and processes it
type HandlerFunc func(...Data) (unhandled []Data)

// NewQueueFunc is a function that creates a queue
type NewQueueFunc func(handler HandlerFunc, config, exemplar interface{}) (Queue, error)

// Shutdownable represents a queue that can be shutdown
type Shutdownable interface {
	Shutdown()
	Terminate()
}

// Named represents a queue with a name
type Named interface {
	Name() string
}

// Queue defines an interface of a queue-like item
//
// Queues will handle their own contents in the Run method
type Queue interface {
	Flushable
	Run(atShutdown, atTerminate func(func()))
	Push(Data) error
}

// PushBackable queues can be pushed back to
type PushBackable interface {
	// PushBack pushes data back to the top of the fifo
	PushBack(Data) error
}

// DummyQueueType is the type for the dummy queue
const DummyQueueType Type = "dummy"

// NewDummyQueue creates a new DummyQueue
func NewDummyQueue(handler HandlerFunc, opts, exemplar interface{}) (Queue, error) {
	return &DummyQueue{}, nil
}

// DummyQueue represents an empty queue
type DummyQueue struct{}

// Run does nothing
func (*DummyQueue) Run(_, _ func(func())) {}

// Push fakes a push of data to the queue
func (*DummyQueue) Push(Data) error {
	return nil
}

// PushFunc fakes a push of data to the queue with a function. The function is never run.
func (*DummyQueue) PushFunc(Data, func() error) error {
	return nil
}

// Has always returns false as this queue never does anything
func (*DummyQueue) Has(Data) (bool, error) {
	return false, nil
}

// Flush always returns nil
func (*DummyQueue) Flush(time.Duration) error {
	return nil
}

// FlushWithContext always returns nil
func (*DummyQueue) FlushWithContext(context.Context) error {
	return nil
}

// IsEmpty asserts that the queue is empty
func (*DummyQueue) IsEmpty() bool {
	return true
}

// ImmediateType is the type to execute the function when push
const ImmediateType Type = "immediate"

// NewImmediate creates a new false queue to execute the function when push
func NewImmediate(handler HandlerFunc, opts, exemplar interface{}) (Queue, error) {
	return &Immediate{
		handler: handler,
	}, nil
}

// Immediate represents an direct execution queue
type Immediate struct {
	handler HandlerFunc
}

// Run does nothing
func (*Immediate) Run(_, _ func(func())) {}

// Push fakes a push of data to the queue
func (q *Immediate) Push(data Data) error {
	return q.PushFunc(data, nil)
}

// PushFunc fakes a push of data to the queue with a function. The function is never run.
func (q *Immediate) PushFunc(data Data, f func() error) error {
	if f != nil {
		if err := f(); err != nil {
			return err
		}
	}
	q.handler(data)
	return nil
}

// Has always returns false as this queue never does anything
func (*Immediate) Has(Data) (bool, error) {
	return false, nil
}

// Flush always returns nil
func (*Immediate) Flush(time.Duration) error {
	return nil
}

// FlushWithContext always returns nil
func (*Immediate) FlushWithContext(context.Context) error {
	return nil
}

// IsEmpty asserts that the queue is empty
func (*Immediate) IsEmpty() bool {
	return true
}

var queuesMap = map[Type]NewQueueFunc{
	DummyQueueType: NewDummyQueue,
	ImmediateType:  NewImmediate,
}

// RegisteredTypes provides the list of requested types of queues
func RegisteredTypes() []Type {
	types := make([]Type, len(queuesMap))
	i := 0
	for key := range queuesMap {
		types[i] = key
		i++
	}
	return types
}

// RegisteredTypesAsString provides the list of requested types of queues
func RegisteredTypesAsString() []string {
	types := make([]string, len(queuesMap))
	i := 0
	for key := range queuesMap {
		types[i] = string(key)
		i++
	}
	return types
}

// NewQueue takes a queue Type, HandlerFunc, some options and possibly an exemplar and returns a Queue or an error
func NewQueue(queueType Type, handlerFunc HandlerFunc, opts, exemplar interface{}) (Queue, error) {
	newFn, ok := queuesMap[queueType]
	if !ok {
		return nil, fmt.Errorf("unsupported queue type: %v", queueType)
	}
	return newFn(handlerFunc, opts, exemplar)
}