diff --git a/.dockerignore b/.dockerignore index 7cd1091..72e59fd 100644 --- a/.dockerignore +++ b/.dockerignore @@ -7,6 +7,7 @@ *.dll *.so *.dylib +*.txt # Test binary, built with `go test -c` *.test diff --git a/.gitignore b/.gitignore index 7cd1091..72e59fd 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ *.dll *.so *.dylib +*.txt # Test binary, built with `go test -c` *.test diff --git a/src/main.go b/src/main.go index 02c5fbe..16dc3f4 100644 --- a/src/main.go +++ b/src/main.go @@ -10,5 +10,6 @@ import ( func main() { log.Msg.Info("Initialised OwlBoard MQ Client " + helpers.Version) dbAccess.PrintFromDbPackage() + defer messaging.Disconnect(messaging.Client) messaging.Listen() } diff --git a/src/messaging/client.go b/src/messaging/client.go index 771bd7a..f1801e6 100644 --- a/src/messaging/client.go +++ b/src/messaging/client.go @@ -14,6 +14,7 @@ type nrodCredential struct { pass string } +// Fetches credentials from environment variables and exits if none provided. func getCredentials() nrodCredential { var nrod_user string = os.Getenv("OWL_LDB_CORPUSUSER") var nrod_pass string = os.Getenv("OWL_LDB_CORPUSPASS") @@ -27,8 +28,9 @@ func getCredentials() nrodCredential { } } -var client = dial() +var Client = dial() +// Connects the STOMP file to the Network Rail MQ Server func dial() *stomp.Conn { var credentials nrodCredential = getCredentials() conn, err := stomp.Dial("tcp", "publicdatafeeds.networkrail.co.uk:61618", @@ -38,19 +40,26 @@ func dial() *stomp.Conn { ) if err != nil { log.Msg.Fatal("Unable to connect to STOMP Client", zap.String("err", err.Error())) + conn.MustDisconnect() } - //defer disconnect(conn) // This exits as soon as the function has returned + log.Msg.Info("Initialised STOMP Client") return conn } -func disconnect(conn *stomp.Conn) { +// Handles graceful disconnection of the STOMP client, falls back to +// a force disconnect if this fails. +func Disconnect(conn *stomp.Conn) { if conn != nil { err := conn.Disconnect() + log.Msg.Warn("Disconnected STOMP Client") if err != nil { conn.MustDisconnect() + log.Msg.Error("STOMP Disconnection failed, forced disconnect") } + return } + log.Msg.Error("STOMP Disconnect failed, next connection attempt may fail") } // Register against the MQ Server and log each message for testing purposes diff --git a/src/messaging/vstp.go b/src/messaging/vstp.go index 09099d7..a4dd0c8 100644 --- a/src/messaging/vstp.go +++ b/src/messaging/vstp.go @@ -2,6 +2,8 @@ package messaging import ( "fmt" + "io/ioutil" + "time" "git.fjla.uk/owlboard/mq-client/log" @@ -9,19 +11,45 @@ import ( ) func Listen() { - sub, err := client.Subscribe("/topic/VSTP_ALL", stomp.AckAuto) + sub, err := Client.Subscribe("/topic/VSTP_ALL", stomp.AckAuto) if err != nil { log.Msg.Fatal("Unable to start subscription: " + err.Error()) } + log.Msg.Info("Subscription to VSTP topic successful") - msg := <-sub.C - if msg.Err != nil { - fmt.Println(msg.Err) - } - handle(msg) + go func() { + log.Msg.Debug("Message handler routine started") + for { + msg := <-sub.C + if msg.Err != nil { + fmt.Println(msg.Err) + } + handle(msg) + } + }() + + select {} } +var count uint64 = 0 + func handle(msg *stomp.Message) { log.Msg.Info("STOMP message received") - fmt.Println(msg) + count++ + log.Msg.Info("Message count: " + fmt.Sprint(count)) + fmt.Println(string(msg.Body)) + saveToFile(string(msg.Body)) +} + +func saveToFile(msg string) { + timestamp := time.Now().Format("2006-01-02T15:04:05") + path := fmt.Sprintf("%s-msg.txt", timestamp) + + err := ioutil.WriteFile(path, []byte(msg), 0644) + if err != nil { + log.Msg.Error("Error saving message: " + err.Error()) + return + } + + log.Msg.Info("Saved message to: " + path) }