This repository has been archived by the owner on Feb 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalive.go
66 lines (52 loc) · 1.41 KB
/
alive.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
package lifecycle
import (
"encoding/json"
"fmt"
)
// AliveEvent is a io.choria.lifecycle.v1.alive event
//
// In addition to the usually required fields it requires a Version()
// specified when producing this type of event
type AliveEvent struct {
basicEvent
Version string `json:"version"`
}
func init() {
eventTypes["alive"] = Alive
eventJSONParsers[Alive] = func(j []byte) (Event, error) {
return newAliveEventFromJSON(j)
}
eventFactories[Alive] = func(opts ...Option) Event {
return newAliveEvent(opts...)
}
}
func newAliveEvent(opts ...Option) *AliveEvent {
event := &AliveEvent{basicEvent: newBasicEvent("alive")}
for _, o := range opts {
o(event)
}
return event
}
func newAliveEventFromJSON(j []byte) (*AliveEvent, error) {
event := &AliveEvent{basicEvent: newBasicEvent("alive")}
err := json.Unmarshal(j, event)
if err != nil {
return nil, err
}
switch event.EventProtocol {
case "io.choria.lifecycle.v1.alive":
case "choria:lifecycle:alive:1":
event.EventProtocol = "io.choria.lifecycle.v1.alive"
default:
return nil, fmt.Errorf("invalid protocol '%s'", event.EventProtocol)
}
return event, nil
}
// String is text suitable to display on the console etc
func (e *AliveEvent) String() string {
return fmt.Sprintf("[alive] %s: %s version %s", e.Ident, e.Component(), e.Version)
}
// SetVersion sets the version for the event
func (e *AliveEvent) SetVersion(v string) {
e.Version = v
}