-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriber.go
129 lines (105 loc) · 2.61 KB
/
subscriber.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package main
import (
"errors"
"log"
"github.com/nats-io/go-nats"
graph "gopkg.in/r3labs/graph.v2"
)
// subscriber : manages the subscription to all messages, and
// discriminates the ones are processable.
func subscriber(msg *nats.Msg) {
var scheduler Scheduler
m, err := NewMessage(msg.Subject, msg.Data)
if err != nil {
return
}
if m.isSupported() != true {
unsupported(m.subject)
return
}
log.Printf("received: %s", msg.Subject)
scheduler.graph = m.getGraph()
processMessage(&scheduler, m)
if scheduler.Done() {
completed(scheduler.graph)
}
if scheduler.Errored() && !scheduler.Running() {
errored(scheduler.graph, errors.New("service provisioning has failed with an error"))
}
}
// processMessage : get the graph and process the component
func processMessage(scheduler *Scheduler, m *Message) {
component := m.getComponent()
if m.getType() == COMPONENTYPE {
err := storeComponent(component)
if err != nil {
errored(scheduler.graph, err)
}
}
componentsToSchedule, err := scheduler.Receive(component)
if err != nil {
errored(scheduler.graph, err)
}
marshalledGraph, err := scheduler.graph.ToJSON()
if err != nil {
errored(scheduler.graph, err)
}
for _, c := range componentsToSchedule {
// set the service id
gc := c.(*graph.GenericComponent)
(*gc)["service"] = scheduler.graph.ID
// update component on change
err := setChange(c)
if err != nil {
log.Println("could not store change: " + c.GetID())
continue
}
// template and send component
c = template(marshalledGraph, c)
err = send(c)
if err != nil {
errored(scheduler.graph, err)
}
}
}
func storeComponent(c graph.Component) error {
var err error
// update the change
if c.GetAction() != "none" {
err = setChange(c)
if err != nil {
return err
}
}
if c.GetState() != STATUSCOMPLETED {
return nil
}
gc := c.(*graph.GenericComponent)
serviceID := (*gc)["service"]
// update the component
switch c.GetAction() {
case "create", "update", "get":
err = setComponent(c)
case "delete":
err = deleteComponent(c)
case "find":
for _, fc := range getQueryComponents(c) {
gfc := fc.(*graph.GenericComponent)
(*gfc)["service"] = serviceID
err = setComponent(fc)
if err != nil {
return err
}
}
}
return err
}
// upsupported : logs an unsupported subject
func unsupported(subject string) {
if subject != "" {
log.Printf("Unsupported message: %s", subject)
}
}