-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathworker_task.go
49 lines (41 loc) · 956 Bytes
/
worker_task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package queue
import (
"context"
"errors"
)
var _ Worker = (*taskWorker)(nil)
// just for unit testing, don't use it.
type taskWorker struct {
messages chan QueuedMessage
}
func (w *taskWorker) Run(task QueuedMessage) error {
if v, ok := task.(Job); ok {
if v.Task != nil {
_ = v.Task(context.Background())
}
}
return nil
}
func (w *taskWorker) Shutdown() error {
close(w.messages)
return nil
}
func (w *taskWorker) Queue(job QueuedMessage) error {
select {
case w.messages <- job:
return nil
default:
return errors.New("max capacity reached")
}
}
func (w *taskWorker) Request() (QueuedMessage, error) {
select {
case task := <-w.messages:
return task, nil
default:
return nil, errors.New("no message in queue")
}
}
func (w *taskWorker) Capacity() int { return cap(w.messages) }
func (w *taskWorker) Usage() int { return len(w.messages) }
func (w *taskWorker) BusyWorkers() uint64 { return uint64(0) }