2021-07-16 04:10:34 +00:00
|
|
|
package queue
|
|
|
|
|
|
|
|
import (
|
2021-07-23 17:29:47 +00:00
|
|
|
"errors"
|
2021-07-16 04:10:34 +00:00
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// A Queue is a message queue.
|
|
|
|
Queue struct {
|
2021-07-23 16:27:11 +00:00
|
|
|
logger Logger
|
2021-07-16 04:10:34 +00:00
|
|
|
workerCount int
|
|
|
|
routineGroup *routineGroup
|
|
|
|
quit chan struct{}
|
|
|
|
worker Worker
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-07-23 17:29:47 +00:00
|
|
|
// Option for queue system
|
|
|
|
type Option func(*Queue)
|
|
|
|
|
|
|
|
// ErrMissingWorker missing define worker
|
|
|
|
var ErrMissingWorker = errors.New("missing worker module")
|
|
|
|
|
|
|
|
// WithWorkerCount set worker count
|
|
|
|
func WithWorkerCount(num int) Option {
|
|
|
|
return func(q *Queue) {
|
|
|
|
q.workerCount = num
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLogger set custom logger
|
|
|
|
func WithLogger(l Logger) Option {
|
|
|
|
return func(q *Queue) {
|
|
|
|
q.logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithWorker set custom worker
|
|
|
|
func WithWorker(w Worker) Option {
|
|
|
|
return func(q *Queue) {
|
|
|
|
q.worker = w
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
// NewQueue returns a Queue.
|
2021-07-23 17:29:47 +00:00
|
|
|
func NewQueue(opts ...Option) (*Queue, error) {
|
2021-07-16 04:10:34 +00:00
|
|
|
q := &Queue{
|
2021-07-16 08:30:01 +00:00
|
|
|
workerCount: runtime.NumCPU(),
|
2021-07-16 04:10:34 +00:00
|
|
|
routineGroup: newRoutineGroup(),
|
|
|
|
quit: make(chan struct{}),
|
2021-07-23 16:27:11 +00:00
|
|
|
logger: new(defaultLogger),
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 17:29:47 +00:00
|
|
|
// Loop through each option
|
|
|
|
for _, opt := range opts {
|
|
|
|
// Call the option giving the instantiated
|
|
|
|
opt(q)
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.worker == nil {
|
|
|
|
return nil, ErrMissingWorker
|
2021-07-16 11:26:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 17:29:47 +00:00
|
|
|
return q, nil
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Capacity for queue max size
|
|
|
|
func (q *Queue) Capacity() int {
|
|
|
|
return q.worker.Capacity()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Usage for count of queue usage
|
|
|
|
func (q *Queue) Usage() int {
|
|
|
|
return q.worker.Usage()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start to enable all worker
|
|
|
|
func (q *Queue) Start() {
|
|
|
|
q.startWorker()
|
|
|
|
}
|
|
|
|
|
2021-07-16 11:26:19 +00:00
|
|
|
// Shutdown stops all queues.
|
|
|
|
func (q *Queue) Shutdown() {
|
|
|
|
q.worker.Shutdown()
|
2021-07-16 04:10:34 +00:00
|
|
|
close(q.quit)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait all process
|
|
|
|
func (q *Queue) Wait() {
|
|
|
|
q.routineGroup.Wait()
|
|
|
|
}
|
|
|
|
|
2021-07-16 11:26:19 +00:00
|
|
|
// Queue to queue all job
|
2021-07-17 17:58:46 +00:00
|
|
|
func (q *Queue) Queue(job QueuedMessage) error {
|
2021-07-16 11:26:19 +00:00
|
|
|
return q.worker.Queue(job)
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 00:20:45 +00:00
|
|
|
func (q *Queue) work(num int) {
|
2021-07-17 16:19:17 +00:00
|
|
|
if err := q.worker.BeforeRun(); err != nil {
|
2021-07-23 16:27:11 +00:00
|
|
|
q.logger.Fatal(err)
|
2021-07-17 16:19:17 +00:00
|
|
|
}
|
2021-07-17 00:20:45 +00:00
|
|
|
q.routineGroup.Run(func() {
|
|
|
|
// to handle panic cases from inside the worker
|
|
|
|
// in such case, we start a new goroutine
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2021-07-23 16:27:11 +00:00
|
|
|
q.logger.Error(err)
|
2021-07-17 00:20:45 +00:00
|
|
|
go q.work(num)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-07-23 16:27:11 +00:00
|
|
|
q.logger.Info("started the worker num ", num)
|
2021-07-17 00:20:45 +00:00
|
|
|
q.worker.Run(q.quit)
|
2021-07-23 16:27:11 +00:00
|
|
|
q.logger.Info("closed the worker num ", num)
|
2021-07-17 00:20:45 +00:00
|
|
|
})
|
2021-07-17 16:19:17 +00:00
|
|
|
if err := q.worker.AfterRun(); err != nil {
|
2021-07-23 16:27:11 +00:00
|
|
|
q.logger.Fatal(err)
|
2021-07-17 16:19:17 +00:00
|
|
|
}
|
2021-07-17 00:20:45 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
func (q *Queue) startWorker() {
|
|
|
|
for i := 0; i < q.workerCount; i++ {
|
2021-07-17 00:20:45 +00:00
|
|
|
go q.work(i)
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
|
|
|
}
|