forked from RichardKnop/machinery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eager.go
77 lines (62 loc) · 2.05 KB
/
eager.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package brokers
import (
"encoding/json"
"errors"
"fmt"
"github.com/RichardKnop/machinery/v1/signatures"
)
// EagerBroker represents an "eager" in-memory broker
type EagerBroker struct {
worker TaskProcessor
}
// NewEagerBroker creates new EagerBroker instance
func NewEagerBroker() Broker {
return new(EagerBroker)
}
// EagerMode interface with methods specific for this broker
type EagerMode interface {
AssignWorker(p TaskProcessor)
}
// SetRegisteredTaskNames sets registered task names
func (eagerBroker *EagerBroker) SetRegisteredTaskNames(names []string) {
// do nothing
}
// IsTaskRegistered returns true if the task is registered with this broker
func (eagerBroker *EagerBroker) IsTaskRegistered(name string) bool {
return true
}
// StartConsuming enters a loop and waits for incoming messages
func (eagerBroker *EagerBroker) StartConsuming(consumerTag string, p TaskProcessor) (bool, error) {
return true, nil
}
// StopConsuming quits the loop
func (eagerBroker *EagerBroker) StopConsuming() {
// do nothing
}
// Publish places a new message on the default queue
func (eagerBroker *EagerBroker) Publish(task *signatures.TaskSignature) error {
if eagerBroker.worker == nil {
return errors.New("worker is not assigned in eager-mode")
}
// faking the behavior to marshal input into json
// and unmarshal it back
message, err := json.Marshal(task)
if err != nil {
return fmt.Errorf("json marshaling failed: %v", err)
}
signature := new(signatures.TaskSignature)
err = json.Unmarshal(message, &signature)
if err != nil {
return fmt.Errorf("json unmarshaling failed: %v", err)
}
// blocking call to the task directly
return eagerBroker.worker.Process(signature)
}
// GetPendingTasks returns a slice of task.Signatures waiting in the queue
func (eagerBroker *EagerBroker) GetPendingTasks(queue string) ([]*signatures.TaskSignature, error) {
return []*signatures.TaskSignature{}, errors.New("Not implemented")
}
// AssignWorker assigns a worker to the eager broker
func (eagerBroker *EagerBroker) AssignWorker(w TaskProcessor) {
eagerBroker.worker = w
}