chore: support single queue and multiple worker (#589)

This commit is contained in:
Bo-Yi Wu
2021-07-16 12:10:34 +08:00
committed by GitHub
parent 0658f18430
commit ab8b1991ab
34 changed files with 1020 additions and 881 deletions

59
queue/simple/simple.go Normal file
View 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),
}
}