chore: support single queue and multiple worker (#589)
This commit is contained in:
59
queue/simple/simple.go
Normal file
59
queue/simple/simple.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package simple
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/appleboy/gorush/config"
|
||||
"github.com/appleboy/gorush/gorush"
|
||||
)
|
||||
|
||||
// Worker for simple queue using channel
|
||||
type Worker struct {
|
||||
cfg config.ConfYaml
|
||||
queueNotification chan gorush.PushNotification
|
||||
}
|
||||
|
||||
// Run start the worker
|
||||
func (s *Worker) Run(_ chan struct{}) {
|
||||
for notification := range s.queueNotification {
|
||||
gorush.SendNotification(s.cfg, notification)
|
||||
}
|
||||
}
|
||||
|
||||
// Stop worker
|
||||
func (s *Worker) Stop() {
|
||||
close(s.queueNotification)
|
||||
}
|
||||
|
||||
// Capacity for channel
|
||||
func (s *Worker) Capacity() int {
|
||||
return cap(s.queueNotification)
|
||||
}
|
||||
|
||||
// Usage for count of channel usage
|
||||
func (s *Worker) Usage() int {
|
||||
return len(s.queueNotification)
|
||||
}
|
||||
|
||||
// Enqueue send notification to queue
|
||||
func (s *Worker) Enqueue(job interface{}) error {
|
||||
select {
|
||||
case s.queueNotification <- job.(gorush.PushNotification):
|
||||
return nil
|
||||
default:
|
||||
return errors.New("max capacity reached")
|
||||
}
|
||||
}
|
||||
|
||||
// Config update current config
|
||||
func (s *Worker) Config(cfg config.ConfYaml) {
|
||||
s.cfg = cfg
|
||||
}
|
||||
|
||||
// NewWorker for struct
|
||||
func NewWorker(cfg config.ConfYaml) *Worker {
|
||||
return &Worker{
|
||||
cfg: cfg,
|
||||
queueNotification: make(chan gorush.PushNotification, cfg.Core.QueueNum),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user