From 18dc87754bd3c93a6d77518b67e1774f49395815 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Fri, 21 Jul 2023 12:12:16 +0100 Subject: [PATCH] Message parsing now complete but not optimised --- src/go.sum | 32 -------------------------------- src/helpers/config.go | 8 +------- src/log/log.go | 16 +++++++++++++++- src/main.go | 34 +++++++++++++++++++++++++++++++--- src/messaging/client.go | 3 +-- src/vstp/actions.go | 22 ++++++++++++++++++++++ src/vstp/handler.go | 1 + src/vstp/subscribe.go | 10 +++++++++- 8 files changed, 80 insertions(+), 46 deletions(-) diff --git a/src/go.sum b/src/go.sum index cf38e19..81ca724 100644 --- a/src/go.sum +++ b/src/go.sum @@ -1,35 +1,3 @@ -git.fjla.uk/owlboard/go-types v0.0.0-20230717113641-a1216e6e5106 h1:1sPFYr4/gEdWGWJhB4/Y7jJYemBSZBXcWtBm7XiZqZk= -git.fjla.uk/owlboard/go-types v0.0.0-20230717113641-a1216e6e5106/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= -git.fjla.uk/owlboard/go-types v0.0.0-20230718113936-e8f6c7115eb9 h1:UHdSkwt7zSwU5cA8bCNW8Z+eyNHAXYZUgbzyOhvUKqQ= -git.fjla.uk/owlboard/go-types v0.0.0-20230718113936-e8f6c7115eb9/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= -git.fjla.uk/owlboard/go-types v0.0.0-20230719183133-0c31803e4c27 h1:1tA4ikudlUPo4MAmzBzU59um9OsiUbPbPxHBJ2FJkDw= -git.fjla.uk/owlboard/go-types v0.0.0-20230719183133-0c31803e4c27/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= -git.fjla.uk/owlboard/go-types v0.0.0-20230719183907-ef710419e906 h1:eB0Ed0BrJ2w6akyK3Jf/ZAFAdlLM/E1jXHVC8QNPtu4= -git.fjla.uk/owlboard/go-types v0.0.0-20230719183907-ef710419e906/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= -git.fjla.uk/owlboard/go-types v0.0.0-20230719185315-e492e8ad6ae7 h1:g+ovnwGpx24KqMzrFA7L6s+JKR2MWzcO51iMXdq7N50= -git.fjla.uk/owlboard/go-types v0.0.0-20230719185315-e492e8ad6ae7/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= -git.fjla.uk/owlboard/go-types v0.0.0-20230719192727-845a41f8572b h1:QKHrP3kjvypPO/YVrHdTNV68w6I7MNYyEy5zcD2Jk/A= -git.fjla.uk/owlboard/go-types v0.0.0-20230719192727-845a41f8572b/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= -git.fjla.uk/owlboard/go-types v0.0.0-20230720095555-ad24002a6acc h1:7s+svu/FOU/qwLLThtwvOV2CqID8sZmVyd15UldgBR8= -git.fjla.uk/owlboard/go-types v0.0.0-20230720095555-ad24002a6acc/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= -git.fjla.uk/owlboard/go-types v0.0.0-20230720100930-1cbfb1d074d2 h1:FOLnHCAL/2fnjC/TLpQkt4WSMzijcDrtyf0YdiyZIbo= -git.fjla.uk/owlboard/go-types v0.0.0-20230720100930-1cbfb1d074d2/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= -git.fjla.uk/owlboard/go-types v0.0.0-20230720185107-816f023a60b5 h1:oNEoHfifZc/F/z+4T8wJ8Ky68vzt/OxL7E0iFhLoh9s= -git.fjla.uk/owlboard/go-types v0.0.0-20230720185107-816f023a60b5/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= -git.fjla.uk/owlboard/go-types v0.0.0-20230720190235-ff722287f36d h1:NIVo66/uvUXV5lvfdLHLyFRXMGXk/iG+IemDU1Iu5JM= -git.fjla.uk/owlboard/go-types v0.0.0-20230720190235-ff722287f36d/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= -git.fjla.uk/owlboard/go-types v0.0.0-20230720194730-1085e35a688e h1:KhnFsS2lumpPwhgVQKTGWXiIOd2KCh0phVSH4/JATAE= -git.fjla.uk/owlboard/go-types v0.0.0-20230720194730-1085e35a688e/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= -git.fjla.uk/owlboard/go-types v0.0.0-20230720195742-5fda10022220 h1:XsxDYTm5s2O9WQPDtl7LvvtlrKnmaKCZCvZpXl6xaDs= -git.fjla.uk/owlboard/go-types v0.0.0-20230720195742-5fda10022220/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= -git.fjla.uk/owlboard/go-types v0.0.0-20230720201551-a582c3495e07 h1:jGnHCl5UZi9y65Uqy2nW65UD4zJEBTWB9+10qHlbCTg= -git.fjla.uk/owlboard/go-types v0.0.0-20230720201551-a582c3495e07/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= -git.fjla.uk/owlboard/go-types v0.0.0-20230720202112-4df6f0456ea4 h1:HeHukiRnDzXNDp/NaqI2n9WWChoglx1yRS620rgvj6o= -git.fjla.uk/owlboard/go-types v0.0.0-20230720202112-4df6f0456ea4/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= -git.fjla.uk/owlboard/go-types v0.0.0-20230720203835-cf2b2d98e86a h1:JvDUM2sIawyyLKhIOgciAj0PpVWziYlXb+RA2IgphMg= -git.fjla.uk/owlboard/go-types v0.0.0-20230720203835-cf2b2d98e86a/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= -git.fjla.uk/owlboard/go-types v0.0.0-20230721082815-634948b5448c h1:KGxtm/m+uPo2xmZIKebUxmMQnb72hY6+GQ7ZRez/iy8= -git.fjla.uk/owlboard/go-types v0.0.0-20230721082815-634948b5448c/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= git.fjla.uk/owlboard/go-types v0.0.0-20230721082911-9a574276d572 h1:shnlNyIV1jG+xQsg5zCt2fEjiDzCQQeDTjTFuKZa97c= git.fjla.uk/owlboard/go-types v0.0.0-20230721082911-9a574276d572/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= diff --git a/src/helpers/config.go b/src/helpers/config.go index f9695c2..7dda533 100644 --- a/src/helpers/config.go +++ b/src/helpers/config.go @@ -1,10 +1,7 @@ package helpers import ( - "fmt" "os" - - "git.fjla.uk/owlboard/mq-client/log" ) // Version Constants @@ -13,9 +10,7 @@ const versionSuffix string = "beta" const Version string = versionNum + "-" + versionSuffix // Environment Variables -var runtime string = getRuntime() -var NrUser string = "nil" -var NrPass string = "" +var Runtime string = getRuntime() // Functions func getRuntime() string { @@ -23,6 +18,5 @@ func getRuntime() string { if runtimeEnv == "" { runtimeEnv = "unknown" } - log.Msg.Debug(fmt.Sprintf("Runtime mode: %s", runtimeEnv)) return runtimeEnv } diff --git a/src/log/log.go b/src/log/log.go index 9814fd8..d120a1e 100644 --- a/src/log/log.go +++ b/src/log/log.go @@ -3,6 +3,8 @@ package log import ( "go.uber.org/zap" "go.uber.org/zap/zapcore" + + "git.fjla.uk/owlboard/mq-client/helpers" ) var Msg *zap.Logger @@ -14,8 +16,20 @@ func init() { config := zap.NewDevelopmentConfig() config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder // Adds color to log levels - Msg, err = config.Build() + // Determine the log level based on the runtime mode + logLevel := zapcore.DebugLevel + if helpers.Runtime == "production" { + logLevel = zapcore.InfoLevel + } + + // Set the log level + config.Level = zap.NewAtomicLevelAt(logLevel) + + Msg, err = config.Build() // Potential source of the error if err != nil { panic("Failed to initialize logger: " + err.Error()) } + + // Log the selected log level (optional, can be helpful for debugging) + Msg.Info("Log level set to: " + logLevel.String()) } diff --git a/src/main.go b/src/main.go index 026aec9..95d679b 100644 --- a/src/main.go +++ b/src/main.go @@ -1,6 +1,10 @@ package main import ( + "os" + "os/signal" + "syscall" + "git.fjla.uk/owlboard/mq-client/dbAccess" "git.fjla.uk/owlboard/mq-client/helpers" "git.fjla.uk/owlboard/mq-client/log" @@ -10,14 +14,38 @@ import ( func main() { log.Msg.Info("Initialised OwlBoard MQ Client " + helpers.Version) + defer cleanup() - dbAccess.PrintFromDbPackage() + + go handleSignals() + vstp.Subscribe() } +// Traps SIGINT and SIGTERM signals and ensures cleanup() is run +func handleSignals() { + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) + + sig := <-sigChan + log.Msg.Warn("Signal received: " + sig.String()) + cleanup() + os.Exit(1) +} + +// Cleans up open connections ready for a clean exit of the program func cleanup() { log.Msg.Debug("Cleaning up open connections") - defer messaging.Disconnect(messaging.Client) - defer dbAccess.CloseMongoClient() + if messaging.Client != nil { + log.Msg.Info("Closing STOMP Client") + messaging.Disconnect(messaging.Client) + } + if dbAccess.MongoClient != nil { + log.Msg.Info("Closing MongoDB Client") + dbAccess.CloseMongoClient() + } log.Msg.Info("Program ready to exit") + if log.Msg != nil { + log.Msg.Sync() + } } diff --git a/src/messaging/client.go b/src/messaging/client.go index af45ecd..40ce487 100644 --- a/src/messaging/client.go +++ b/src/messaging/client.go @@ -4,7 +4,6 @@ import ( "os" "git.fjla.uk/owlboard/mq-client/log" - "go.uber.org/zap" "github.com/go-stomp/stomp/v3" ) @@ -39,7 +38,7 @@ func dial() *stomp.Conn { stomp.ConnOpt.Header("client-id", credentials.user+"-mq-client"), ) if err != nil { - log.Msg.Fatal("Unable to connect to STOMP Client", zap.String("err", err.Error())) + log.Msg.Fatal("Unable to connect to STOMP Client: " + err.Error()) conn.MustDisconnect() } diff --git a/src/vstp/actions.go b/src/vstp/actions.go index 8e78241..5dc023e 100644 --- a/src/vstp/actions.go +++ b/src/vstp/actions.go @@ -1,4 +1,26 @@ package vstp +import ( + "git.fjla.uk/owlboard/go-types/pkg/database" + "git.fjla.uk/owlboard/mq-client/log" +) + // Decide, based on the DB Formatted message type, what action needs taking // then either insert, or delete from the database as required +func processEntryType(entry database.Service) { + if entry.TransactionType == "Create" { + createEntry(entry) + } else if entry.TransactionType == "Delete" { + deleteEntry(entry) + } else { + log.Msg.Error("Unknown TransactionType: " + entry.TransactionType) + } +} + +func createEntry(entry database.Service) { + log.Msg.Info("Entry Creation requested for: " + entry.TrainUid + " - " + entry.Headcode) +} + +func deleteEntry(entry database.Service) { + log.Msg.Info("Entry DELETE requested for: " + entry.TrainUid + " - " + entry.Headcode) +} diff --git a/src/vstp/handler.go b/src/vstp/handler.go index b2960c3..0314184 100644 --- a/src/vstp/handler.go +++ b/src/vstp/handler.go @@ -17,6 +17,7 @@ func handle(msg *stomp.Message) { count++ log.Msg.Info("Message count: " + fmt.Sprint(count)) schedule := unmarshalData(string(msg.Body)) + processEntryType(schedule) saveToFile(schedule, "transformed") } diff --git a/src/vstp/subscribe.go b/src/vstp/subscribe.go index e53fcbd..da1c687 100644 --- a/src/vstp/subscribe.go +++ b/src/vstp/subscribe.go @@ -1,6 +1,8 @@ package vstp import ( + "time" + "git.fjla.uk/owlboard/mq-client/log" "git.fjla.uk/owlboard/mq-client/messaging" "github.com/go-stomp/stomp/v3" @@ -15,7 +17,13 @@ func Subscribe() { go func() { log.Msg.Debug("GOROUTINE: VSTP Message Handler Started") - defer log.Msg.Warn("GOROUTINE: VSTP Message Handler Stopped") + defer func() { + if r := recover(); r != nil { + log.Msg.Warn("GOROUTINE: VSTP Message Handler Stopped") + time.Sleep(time.Second * 10) + log.Msg.Fatal("GOROUTINE: VSTP Message Handler Failed") + } + }() for { msg := <-sub.C if msg.Err != nil {