generated from devnw/oss-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
217 lines (175 loc) · 4.4 KB
/
cmd.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright © 2019 Developer Network, LLC
//
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.
package cmd
import (
"context"
"flag"
"fmt"
"net"
"net/url"
"os"
"os/signal"
"syscall"
"time"
"atomizer.io/amqp"
"atomizer.io/engine"
"devnw.com/alog"
"github.com/pkg/errors"
)
const (
// connectionStringENV is the connection string for the message queue,
// in this case this is specific to rabbitmq
connectionStringENV string = "CONNECTIONSTRING"
// QUEUE is the queue for atom messages to be passed across in the
// message queue
queueENV string = "QUEUE"
)
// Initialize reads flags in from the command line and stands up the atomizer
func Initialize() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
// Setup interrupt monitoring for the agent
go func() {
defer cancel()
select {
case <-ctx.Done():
return
case <-sigs:
alog.Println("Closing Atomizer Agent")
os.Exit(1)
}
}()
err := alog.Global(
ctx,
"ATOMIZER AGENT",
alog.DEFAULTTIMEFORMAT,
time.UTC,
alog.DEFAULTBUFFER,
alog.Standards()...,
)
if err != nil {
fmt.Println("unable to overwrite the global logger")
}
// Parse the command line flags or environment variables
cstring, queue := flags()
if err != nil {
fmt.Println("error while pulling environment variables | " + err.Error())
os.Exit(1)
}
u, err := url.Parse(cstring)
if err != nil {
fmt.Println("error parsing connection string")
os.Exit(1)
}
if err = waitForHost(ctx, u.Host); err != nil {
fmt.Println(err)
os.Exit(1)
}
var conductor engine.Conductor
var attempts int
for attempts < 5 && conductor == nil {
// Create the amqp conductor for the agent
conductor, err = amqp.Connect(ctx, cstring, queue)
if err != nil || conductor == nil {
if attempts >= 5 {
fmt.Println(
"error while initializing amqp | " +
err.Error(),
)
os.Exit(1)
}
attempts++
alog.Printf(
"connection attempt %d to amqp failed, retrying",
attempts,
)
time.Sleep(time.Second * 5)
}
}
alog.Println("connection to amqp established")
// Register the conductor into the atomizer library after initializing the
// connection to the message queue
err = engine.Register(conductor)
if err != nil {
fmt.Println("error registering amqp conductor | " + err.Error())
os.Exit(1)
}
// setup the alog event subscription
events := make(chan interface{})
defer close(events)
alog.Printc(ctx, events)
// Create a copy of the atomizer
a, err := engine.Atomize(ctx, events)
if err != nil {
fmt.Println("error while initializing atomizer.| " + err.Error())
os.Exit(1)
}
// Execute the processing on the atomizer
err = a.Exec()
if err != nil {
fmt.Println("error while executing atomizer.| " + err.Error())
os.Exit(1)
}
alog.Println("Online")
// Block until the processing is interrupted
a.Wait()
time.Sleep(time.Millisecond * 50)
// Get the alog wait method to work with the internal channels
alog.Wait(true)
}
// waitForHost waits for the port of the conductor to become available
func waitForHost(ctx context.Context, host string) error {
ctx, cancel := context.WithTimeout(ctx, time.Second*60)
defer cancel()
for {
select {
case <-ctx.Done():
return errors.New("connection timeout exceeded")
default:
_, err := net.Dial("tcp", host)
if err == nil {
alog.Printf("host [%s] detected", host)
return nil
}
alog.Printf("waiting for host [%s]", host)
time.Sleep(time.Second * 5)
}
}
}
func flags() (conductor, queue string) {
c := flag.String(
"conn",
"amqp://guest:guest@localhost:5672/",
"connection string used for rabbit mq",
)
help := flag.Bool(
"h",
false,
`signals to the agent to use environment
variables for configurations`,
)
q := flag.String("queue", "atomizer", "queue is the queue for atom messages to be passed across in the message queue")
flag.Parse()
if *help {
fmt.Println("Usage Information")
flag.PrintDefaults()
os.Exit(0)
}
return environment(*c, *q)
}
// environment pulls the environment variables as defined in the constants
// section and overwrites the passed flag values
func environment(cflag, qflag string) (c, q string) {
c = os.Getenv(connectionStringENV)
if c == "" {
c = cflag
}
q = os.Getenv(queueENV)
if q == "" {
q = qflag
}
return c, q
}