2021-07-16 04:10:34 +00:00
|
|
|
package simple
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-07-16 22:56:42 +00:00
|
|
|
"runtime"
|
2021-07-16 04:10:34 +00:00
|
|
|
|
2021-07-23 17:56:33 +00:00
|
|
|
"github.com/appleboy/gorush/notify"
|
2021-07-16 11:26:19 +00:00
|
|
|
"github.com/appleboy/gorush/queue"
|
2021-07-16 04:10:34 +00:00
|
|
|
)
|
|
|
|
|
2021-07-16 11:26:19 +00:00
|
|
|
var _ queue.Worker = (*Worker)(nil)
|
|
|
|
|
2021-07-16 22:56:42 +00:00
|
|
|
// Option for queue system
|
|
|
|
type Option func(*Worker)
|
|
|
|
|
2021-07-17 00:20:45 +00:00
|
|
|
var errMaxCapacity = errors.New("max capacity reached")
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
// Worker for simple queue using channel
|
|
|
|
type Worker struct {
|
2021-07-18 11:34:30 +00:00
|
|
|
queueNotification chan queue.QueuedMessage
|
|
|
|
runFunc func(queue.QueuedMessage) error
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 12:14:19 +00:00
|
|
|
// BeforeRun run script before start worker
|
|
|
|
func (s *Worker) BeforeRun() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AfterRun run script after start worker
|
|
|
|
func (s *Worker) AfterRun() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
// Run start the worker
|
2021-07-16 11:26:19 +00:00
|
|
|
func (s *Worker) Run(_ chan struct{}) error {
|
2021-07-18 11:34:30 +00:00
|
|
|
for notification := range s.queueNotification {
|
|
|
|
s.runFunc(notification)
|
|
|
|
}
|
|
|
|
return nil
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 11:26:19 +00:00
|
|
|
// Shutdown worker
|
|
|
|
func (s *Worker) Shutdown() error {
|
2021-07-18 11:34:30 +00:00
|
|
|
close(s.queueNotification)
|
2021-07-16 11:26:19 +00:00
|
|
|
return nil
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Capacity for channel
|
|
|
|
func (s *Worker) Capacity() int {
|
2021-07-18 11:34:30 +00:00
|
|
|
return cap(s.queueNotification)
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Usage for count of channel usage
|
|
|
|
func (s *Worker) Usage() int {
|
2021-07-18 11:34:30 +00:00
|
|
|
return len(s.queueNotification)
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 11:26:19 +00:00
|
|
|
// Queue send notification to queue
|
2021-07-17 17:58:46 +00:00
|
|
|
func (s *Worker) Queue(job queue.QueuedMessage) error {
|
2021-07-16 04:10:34 +00:00
|
|
|
select {
|
2021-07-18 11:34:30 +00:00
|
|
|
case s.queueNotification <- job:
|
2021-07-16 04:10:34 +00:00
|
|
|
return nil
|
|
|
|
default:
|
2021-07-17 00:20:45 +00:00
|
|
|
return errMaxCapacity
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:56:42 +00:00
|
|
|
// WithQueueNum setup the capcity of queue
|
|
|
|
func WithQueueNum(num int) Option {
|
|
|
|
return func(w *Worker) {
|
2021-07-18 11:34:30 +00:00
|
|
|
w.queueNotification = make(chan queue.QueuedMessage, num)
|
2021-07-18 01:03:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithRunFunc setup the run func of queue
|
2021-07-18 11:34:30 +00:00
|
|
|
func WithRunFunc(fn func(queue.QueuedMessage) error) Option {
|
2021-07-18 01:03:57 +00:00
|
|
|
return func(w *Worker) {
|
|
|
|
w.runFunc = fn
|
2021-07-16 22:56:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWorker for struc
|
|
|
|
func NewWorker(opts ...Option) *Worker {
|
|
|
|
w := &Worker{
|
2021-07-18 11:34:30 +00:00
|
|
|
queueNotification: make(chan queue.QueuedMessage, runtime.NumCPU()<<1),
|
|
|
|
runFunc: func(msg queue.QueuedMessage) error {
|
2021-07-23 17:56:33 +00:00
|
|
|
notify.SendNotification(msg)
|
2021-07-18 01:03:57 +00:00
|
|
|
return nil
|
|
|
|
},
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
2021-07-16 22:56:42 +00:00
|
|
|
|
|
|
|
// Loop through each option
|
|
|
|
for _, opt := range opts {
|
|
|
|
// Call the option giving the instantiated
|
|
|
|
opt(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
return w
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|