chore: update worker interface (#592)
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
@@ -10,7 +10,6 @@ type (
|
||||
// A Queue is a message queue.
|
||||
Queue struct {
|
||||
workerCount int
|
||||
queueCount int
|
||||
routineGroup *routineGroup
|
||||
quit chan struct{}
|
||||
worker Worker
|
||||
@@ -18,15 +17,18 @@ type (
|
||||
)
|
||||
|
||||
// NewQueue returns a Queue.
|
||||
func NewQueue(w Worker) *Queue {
|
||||
func NewQueue(w Worker, workerNum int) *Queue {
|
||||
q := &Queue{
|
||||
workerCount: runtime.NumCPU(),
|
||||
queueCount: runtime.NumCPU() << 1,
|
||||
routineGroup: newRoutineGroup(),
|
||||
quit: make(chan struct{}),
|
||||
worker: w,
|
||||
}
|
||||
|
||||
if workerNum > 0 {
|
||||
q.workerCount = workerNum
|
||||
}
|
||||
|
||||
return q
|
||||
}
|
||||
|
||||
@@ -45,9 +47,9 @@ func (q *Queue) Start() {
|
||||
q.startWorker()
|
||||
}
|
||||
|
||||
// Stop stops q.
|
||||
func (q *Queue) Stop() {
|
||||
q.worker.Stop()
|
||||
// Shutdown stops all queues.
|
||||
func (q *Queue) Shutdown() {
|
||||
q.worker.Shutdown()
|
||||
close(q.quit)
|
||||
}
|
||||
|
||||
@@ -56,9 +58,9 @@ func (q *Queue) Wait() {
|
||||
q.routineGroup.Wait()
|
||||
}
|
||||
|
||||
// Enqueue queue all job
|
||||
func (q *Queue) Enqueue(job interface{}) error {
|
||||
return q.worker.Enqueue(job)
|
||||
// Queue to queue all job
|
||||
func (q *Queue) Queue(job interface{}) error {
|
||||
return q.worker.Queue(job)
|
||||
}
|
||||
|
||||
func (q *Queue) startWorker() {
|
||||
|
||||
@@ -4,23 +4,29 @@ import (
|
||||
"errors"
|
||||
|
||||
"github.com/appleboy/gorush/gorush"
|
||||
"github.com/appleboy/gorush/queue"
|
||||
)
|
||||
|
||||
var _ queue.Worker = (*Worker)(nil)
|
||||
|
||||
// Worker for simple queue using channel
|
||||
type Worker struct {
|
||||
queueNotification chan gorush.PushNotification
|
||||
}
|
||||
|
||||
// Run start the worker
|
||||
func (s *Worker) Run(_ chan struct{}) {
|
||||
func (s *Worker) Run(_ chan struct{}) error {
|
||||
for notification := range s.queueNotification {
|
||||
gorush.SendNotification(notification)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop worker
|
||||
func (s *Worker) Stop() {
|
||||
// Shutdown worker
|
||||
func (s *Worker) Shutdown() error {
|
||||
close(s.queueNotification)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Capacity for channel
|
||||
@@ -33,8 +39,8 @@ func (s *Worker) Usage() int {
|
||||
return len(s.queueNotification)
|
||||
}
|
||||
|
||||
// Enqueue send notification to queue
|
||||
func (s *Worker) Enqueue(job interface{}) error {
|
||||
// Queue send notification to queue
|
||||
func (s *Worker) Queue(job interface{}) error {
|
||||
select {
|
||||
case s.queueNotification <- job.(gorush.PushNotification):
|
||||
return nil
|
||||
|
||||
@@ -2,9 +2,9 @@ package queue
|
||||
|
||||
// Worker interface
|
||||
type Worker interface {
|
||||
Run(chan struct{})
|
||||
Stop()
|
||||
Enqueue(job interface{}) error
|
||||
Run(chan struct{}) error
|
||||
Shutdown() error
|
||||
Queue(job interface{}) error
|
||||
Capacity() int
|
||||
Usage() int
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user