/
toto.go
103 lines (90 loc) · 3.51 KB
/
toto.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
/*
Toto-build, the stupid Go continuous build server.
Toto-build is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Toto-build is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package main
import (
"encoding/json"
"flag"
"github.com/nsqio/go-nsq"
"github.com/vil-coyote-acme/toto-build-common/broker"
"github.com/vil-coyote-acme/toto-build-common/message"
"toto-build-agent/api/messaging"
"toto-build-agent/build"
"github.com/vil-coyote-acme/toto-build-common/logs"
)
var (
nsqLookUpHost string
nsqLookUpPort string
brokerAddr string
brokerPort string
embeddedBroker *broker.Broker
listener *messaging.Listener
toWorkChan chan message.ToWork
reportChan chan message.Report
logger *logs.Logger
)
// todo get the log level as an input
func main() {
logger = logs.NewLogger("[TOTO-BUILD main] : ", logs.NewConsoleAppender(logs.INFO))
logger.Info("application is starting")
flag.StringVar(&brokerAddr, "broker-addr", "127.0.0.1", "address of the broker. Should be accessible from scheduler")
flag.StringVar(&brokerPort, "broker-port", "4150", "port of the broker. Should be accessible from scheduler")
flag.StringVar(&nsqLookUpHost, "lookup-addrr", "127.0.0.1", "address of the lookup service. Used by toto-build to get topic used for communications")
flag.StringVar(&nsqLookUpPort, "lookup-port", "4161", "port of the lookup service.")
flag.Parse()
}
// start listening for toWork
// will start an embeded broker, a report producer and a toWork listener
func startListening() {
// first start the broker
logger.Info("Starting listening for jobs")
embeddedBroker = broker.NewBroker()
embeddedBroker.BrokerAddr = brokerAddr
embeddedBroker.BrokerPort = brokerPort
embeddedBroker.StartBroker()
// start the report producer
logger.Info("Starting the report producer")
reportChan = make(chan message.Report, 20)
producerConf := messaging.NewProducerConfig()
producerConf.NsqAddr = brokerAddr + ":" + brokerPort
producer := messaging.NewProducer(producerConf)
producer.Start(reportChan)
// start the work listener
logger.Info("Starting the work listener")
listenerConf := messaging.NewListenerConfig()
listenerConf.LookupAddr = []string{nsqLookUpHost + ":" + nsqLookUpPort}
listener = messaging.NewListener(listenerConf)
// must create the topic BEFORE listing on it
sayHello(listenerConf.Topic)
toWorkChan = listener.Start()
// and then start executing incoming job
logger.Info("Starting execution incoming Jobs")
build.ExecuteJob(toWorkChan, reportChan)
logger.Info("Agent setup done")
}
func graceFullShutDown() {
logger.Info("Starting stopping grecefully")
embeddedBroker.Stop()
listener.Stop()
close(toWorkChan)
close(reportChan)
logger.Info("Stopped grecefully")
}
// will publish the first report : a hello one
func sayHello(topic string) {
config := nsq.NewConfig()
p, _ := nsq.NewProducer(brokerAddr + ":" + brokerPort, config)
body, _ := json.Marshal(message.ToWork{int64(1), message.HELLO, "HELLO", "", ""})
p.Publish("jobs", body)
}