2021-07-16 04:10:34 +00:00
|
|
|
package simple
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/appleboy/gorush/gorush"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Worker for simple queue using channel
|
|
|
|
type Worker struct {
|
|
|
|
queueNotification chan gorush.PushNotification
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run start the worker
|
|
|
|
func (s *Worker) Run(_ chan struct{}) {
|
|
|
|
for notification := range s.queueNotification {
|
2021-07-16 08:30:01 +00:00
|
|
|
gorush.SendNotification(notification)
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWorker for struct
|
2021-07-16 08:30:01 +00:00
|
|
|
func NewWorker(num int) *Worker {
|
2021-07-16 04:10:34 +00:00
|
|
|
return &Worker{
|
2021-07-16 08:30:01 +00:00
|
|
|
queueNotification: make(chan gorush.PushNotification, num),
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
|
|
|
}
|