Skip to content

Commit 242e661

Browse files
cosmetic, fmt
1 parent b438176 commit 242e661

File tree

5 files changed

+286
-283
lines changed

5 files changed

+286
-283
lines changed

docker/container_monitor_setup.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package main
2+
23
import (
3-
"github.com/flexconstructor/go-container-monitor"
4-
"log"
54
"flag"
5+
"github.com/flexconstructor/go-container-monitor"
66
"github.com/sevlyar/go-daemon"
7+
"log"
78
"os"
8-
"syscall"
99
"strings"
10+
"syscall"
1011
)
1112

1213
// Application for collect information about media server system.
@@ -64,16 +65,16 @@ func main() {
6465

6566
log.Println("- - - - - - - - - - - - - - -")
6667
log.Println("system monitor daemon started")
67-
redis_url:=""
68+
redis_url := ""
6869
for _, e := range os.Environ() {
6970
pair := strings.Split(e, "=")
70-
if(pair[0] == "REDIS_URL"){
71+
if pair[0] == "REDIS_URL" {
7172
redis_url = pair[1]
7273
break
7374
}
7475
}
7576

76-
listener = container_monitor.NewRedisListener(redis_url,"",0)
77+
listener = container_monitor.NewRedisListener(redis_url, "", 0)
7778
go listener.Listen()
7879
defer listener.Close()
7980

monitor.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
type ContainerMonitor struct {
1212
close_channel chan bool // Channel for close signal.
1313
info_factory *SystemInfoFactory // System info factory.
14-
testID string
14+
testID string
1515
}
1616

1717
// Returns new ContainerMonitor instance.
@@ -22,26 +22,25 @@ func newContainerMonitor(client *redis.Client, test_id string) *ContainerMonitor
2222
return &ContainerMonitor{
2323
close_channel: make(chan bool),
2424
info_factory: NewSystemInfoFactory(client),
25-
testID:test_id,
25+
testID: test_id,
2626
}
2727
}
2828

2929
// Runs the container monitor.
3030
// Just starts listen of unix socket.
3131
func (m *ContainerMonitor) Run() {
32-
for{
33-
select{
34-
case <- time.After(time.Second *2):
35-
m.info_factory.UpdateSystemInfo(m.testID)
36-
case <- m.close_channel:
37-
return
32+
for {
33+
select {
34+
case <-time.After(time.Second * 2):
35+
m.info_factory.UpdateSystemInfo(m.testID)
36+
case <-m.close_channel:
37+
return
3838
}
3939
}
4040
}
4141

42-
4342
// Close redis connection.
4443
func (m *ContainerMonitor) Stop() {
45-
log.Println("stop test: %s",m.testID)
44+
log.Println("stop test: %s", m.testID)
4645
m.close_channel <- true
4746
}

redis_listener.go

+38-37
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package container_monitor
2+
23
import (
34
"gopkg.in/redis.v4"
45
"log"
56
)
6-
type RedisListener struct{
7-
Client *redis.Client // Redis client
8-
monitor *ContainerMonitor // Container monitor.
7+
8+
type RedisListener struct {
9+
Client *redis.Client // Redis client
10+
monitor *ContainerMonitor // Container monitor.
911
}
1012

1113
// Returns new instance of Redis listener.
1214
// props: r_url string Redis server URL.
1315
// password string Redis server password.
1416
// db int Redis server Data Base ID.
15-
func NewRedisListener(r_url string, password string, db int)(*RedisListener){
17+
func NewRedisListener(r_url string, password string, db int) *RedisListener {
1618
return &RedisListener{
1719
Client: redis.NewClient(&redis.Options{
1820
Addr: r_url,
@@ -23,69 +25,70 @@ func NewRedisListener(r_url string, password string, db int)(*RedisListener){
2325
}
2426

2527
// Listens Redis pub/sub channel.
26-
func (l *RedisListener)Listen(){
28+
func (l *RedisListener) Listen() {
2729
defer l.Client.Close()
28-
pubsub,err:= l.Client.Subscribe(STRESS_TEST_CHANNEL)
29-
if(err != nil){
30-
log.Printf("CLIENT SUBSCRIBE ERROR: %s",err.Error())
30+
pubsub, err := l.Client.Subscribe(STRESS_TEST_CHANNEL)
31+
if err != nil {
32+
log.Printf("CLIENT SUBSCRIBE ERROR: %s", err.Error())
3133
return
3234
}
3335
defer pubsub.Unsubscribe(STRESS_TEST_CHANNEL)
34-
for{
35-
err:=l.ping()
36-
if(err != nil){
37-
log.Printf("CLIENT PING ERROR: %s",err.Error())
36+
for {
37+
err := l.ping()
38+
if err != nil {
39+
log.Printf("CLIENT PING ERROR: %s", err.Error())
3840
return
3941
}
40-
mess,err:= pubsub.ReceiveMessage()
41-
if(err == nil){
42+
mess, err := pubsub.ReceiveMessage()
43+
if err == nil {
4244
l.readRedisMessage(mess)
4345
}
4446
}
4547
}
4648

4749
// Calls redis pub/sub channel.
48-
func (l *RedisListener)Call(test_id string, command string){
49-
mess:= newRedisMessage(command, test_id)
50-
message_string,err:= marshalRedisMessage(mess)
51-
if(err != nil){
52-
log.Printf("can not create start redis message %s",err.Error())
50+
func (l *RedisListener) Call(test_id string, command string) {
51+
mess := newRedisMessage(command, test_id)
52+
message_string, err := marshalRedisMessage(mess)
53+
if err != nil {
54+
log.Printf("can not create start redis message %s", err.Error())
5355
return
5456
}
55-
l.Client.Publish(STRESS_TEST_CHANNEL,message_string)
57+
l.Client.Publish(STRESS_TEST_CHANNEL, message_string)
5658
}
5759

5860
// Closes listener
59-
func (l *RedisListener)Close(){
61+
func (l *RedisListener) Close() {
6062
l.Client.Close()
6163
}
6264

6365
// Reads Redis pub/sub messages.
64-
func(l *RedisListener)readRedisMessage(mess *redis.Message){
65-
message,err:=unmarshalRedisMessage(mess.Payload)
66-
if(err != nil){
67-
log.Printf("Can not unmarshall redis message: %s",err.Error())
66+
func (l *RedisListener) readRedisMessage(mess *redis.Message) {
67+
message, err := unmarshalRedisMessage(mess.Payload)
68+
if err != nil {
69+
log.Printf("Can not unmarshall redis message: %s", err.Error())
6870
return
6971
}
70-
if(message.Command == START_COMMAND) {
72+
if message.Command == START_COMMAND {
7173
l.startTest(message.TestID)
72-
}else{
74+
} else {
7375
l.stopTest(message.TestID)
7476
}
7577
}
7678

7779
// Starts gathering information about the container system.
78-
func (l *RedisListener)startTest(test_id string){
79-
if(l.monitor != nil){
80+
func (l *RedisListener) startTest(test_id string) {
81+
if l.monitor != nil {
8082
log.Println("ERROR: last test not finiched!")
8183
return
8284
}
83-
l.monitor = newContainerMonitor(l.Client,test_id)
85+
l.monitor = newContainerMonitor(l.Client, test_id)
8486
go l.monitor.Run()
8587
}
88+
8689
// Stops gathering information about the container system.
87-
func (l *RedisListener)stopTest(test_id string){
88-
if(l.monitor == nil){
90+
func (l *RedisListener) stopTest(test_id string) {
91+
if l.monitor == nil {
8992
log.Println("ERROR: test not started!")
9093
return
9194
}
@@ -94,12 +97,10 @@ func (l *RedisListener)stopTest(test_id string){
9497
}
9598

9699
// Pings redis pub/sub channel.
97-
func (l *RedisListener)ping()(error){
98-
err:=l.Client.Ping().Err()
99-
if(err != nil){
100+
func (l *RedisListener) ping() error {
101+
err := l.Client.Ping().Err()
102+
if err != nil {
100103
return err
101104
}
102105
return nil
103106
}
104-
105-

redis_message.go

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,42 @@
11
package container_monitor
2+
23
import (
34
"encoding/json"
45
"log"
56
)
7+
68
const (
7-
START_COMMAND="start_test" // Stress test started.
8-
STOP_COMMAND="stop_test" // Stress test stopped.
9-
STRESS_TEST_CHANNEL="stress_test_client" // Redis channel name.
9+
START_COMMAND = "start_test" // Stress test started.
10+
STOP_COMMAND = "stop_test" // Stress test stopped.
11+
STRESS_TEST_CHANNEL = "stress_test_client" // Redis channel name.
1012
)
1113

1214
// Redis pub/sub message
13-
type redisMessage struct{
15+
type redisMessage struct {
1416
Command string
15-
TestID string
17+
TestID string
1618
}
1719

1820
// Returns new instance of Redis pub/sub message.
1921
//
2022
// params: command string Kind of command.
2123
// test_id string Stress test ID.
22-
func newRedisMessage(command string, test_id string)(*redisMessage){
24+
func newRedisMessage(command string, test_id string) *redisMessage {
2325
return &redisMessage{
24-
Command:command,
25-
TestID: test_id,
26+
Command: command,
27+
TestID: test_id,
2628
}
2729
}
2830

2931
// Encodes Redis pub/sub message instance to JSON string.
3032
//
3133
// param: message *redisMessage Instance of Redis pub/sub message.
3234
// return JSON string or error instance.
33-
func marshalRedisMessage(message *redisMessage)(string, error){
34-
message_string,err:= json.Marshal(message)
35-
if(err != nil){
36-
log.Printf("Can not marshal redis message %s",err.Error())
37-
return err.Error(),err
35+
func marshalRedisMessage(message *redisMessage) (string, error) {
36+
message_string, err := json.Marshal(message)
37+
if err != nil {
38+
log.Printf("Can not marshal redis message %s", err.Error())
39+
return err.Error(), err
3840
}
3941
return string(message_string), nil
4042
}
@@ -43,12 +45,12 @@ message_string,err:= json.Marshal(message)
4345
//
4446
// param: message_string string JSON message string.
4547
// return: Instance of *redisMessage or error instance.
46-
func unmarshalRedisMessage(message_string string)(*redisMessage, error){
47-
mess:= &redisMessage{}
48-
err:= json.Unmarshal([]byte(message_string),mess)
49-
if(err != nil){
50-
log.Printf("Can not unmarshal redis message %s",err.Error())
48+
func unmarshalRedisMessage(message_string string) (*redisMessage, error) {
49+
mess := &redisMessage{}
50+
err := json.Unmarshal([]byte(message_string), mess)
51+
if err != nil {
52+
log.Printf("Can not unmarshal redis message %s", err.Error())
5153
return nil, err
5254
}
53-
return mess,nil
54-
}
55+
return mess, nil
56+
}

0 commit comments

Comments
 (0)