-
Notifications
You must be signed in to change notification settings - Fork 14
/
command.go
77 lines (63 loc) · 1.97 KB
/
command.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 metafora
import "encoding/json"
const (
cmdFreeze = "freeze"
cmdUnfreeze = "unfreeze"
cmdBalance = "balance"
cmdStopTask = "stop_task"
)
// Commands are a way clients can communicate directly with nodes for cluster
// maintenance.
//
// Use the Command functions to generate implementations of this interface.
// Metafora's consumer will discard unknown commands.
type Command interface {
// Name returns the name of the command.
Name() string
// Parameters returns the parameters, if any, the command will be executed
// with.
Parameters() map[string]interface{}
// Marshal turns a command into its wire representation.
Marshal() ([]byte, error)
}
// command is the internal representation of commands used for serialization.
type command struct {
C string `json:"command"`
P map[string]interface{} `json:"parameters,omitempty"`
}
// Name returns the name of the command.
func (c *command) Name() string {
return c.C
}
// Parameters returns the parameters, if any, the command will be executed
// with.
func (c *command) Parameters() map[string]interface{} {
return c.P
}
// Marshal turns a command into its wire representation.
func (c *command) Marshal() ([]byte, error) {
return json.Marshal(c)
}
// Unmarshal parses a command from its wire representation.
func UnmarshalCommand(p []byte) (Command, error) {
c := &command{}
err := json.Unmarshal(p, c)
return c, err
}
// CommandFreeze stops all task watching and balancing.
func CommandFreeze() Command {
return &command{C: cmdFreeze}
}
// CommandUnfreeze resumes task watching and balancing.
func CommandUnfreeze() Command {
return &command{C: cmdUnfreeze}
}
// CommandBalance forces the node's balancer.Balance method to be called even
// if frozen.
func CommandBalance() Command {
return &command{C: cmdBalance}
}
// CommandStopTask forces a node to stop a task even if frozen.
func CommandStopTask(task string) Command {
return &command{C: cmdStopTask, P: map[string]interface{}{"task": task}}
}