Message parsing now complete but not optimised

This commit is contained in:
Fred Boniface 2023-07-21 12:12:16 +01:00
parent 9fa0f7d61f
commit 18dc87754b
8 changed files with 80 additions and 46 deletions

View File

@ -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 h1:shnlNyIV1jG+xQsg5zCt2fEjiDzCQQeDTjTFuKZa97c=
git.fjla.uk/owlboard/go-types v0.0.0-20230721082911-9a574276d572/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= 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= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=

View File

@ -1,10 +1,7 @@
package helpers package helpers
import ( import (
"fmt"
"os" "os"
"git.fjla.uk/owlboard/mq-client/log"
) )
// Version Constants // Version Constants
@ -13,9 +10,7 @@ const versionSuffix string = "beta"
const Version string = versionNum + "-" + versionSuffix const Version string = versionNum + "-" + versionSuffix
// Environment Variables // Environment Variables
var runtime string = getRuntime() var Runtime string = getRuntime()
var NrUser string = "nil"
var NrPass string = ""
// Functions // Functions
func getRuntime() string { func getRuntime() string {
@ -23,6 +18,5 @@ func getRuntime() string {
if runtimeEnv == "" { if runtimeEnv == "" {
runtimeEnv = "unknown" runtimeEnv = "unknown"
} }
log.Msg.Debug(fmt.Sprintf("Runtime mode: %s", runtimeEnv))
return runtimeEnv return runtimeEnv
} }

View File

@ -3,6 +3,8 @@ package log
import ( import (
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
"git.fjla.uk/owlboard/mq-client/helpers"
) )
var Msg *zap.Logger var Msg *zap.Logger
@ -14,8 +16,20 @@ func init() {
config := zap.NewDevelopmentConfig() config := zap.NewDevelopmentConfig()
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder // Adds color to log levels 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 { if err != nil {
panic("Failed to initialize logger: " + err.Error()) 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())
} }

View File

@ -1,6 +1,10 @@
package main package main
import ( import (
"os"
"os/signal"
"syscall"
"git.fjla.uk/owlboard/mq-client/dbAccess" "git.fjla.uk/owlboard/mq-client/dbAccess"
"git.fjla.uk/owlboard/mq-client/helpers" "git.fjla.uk/owlboard/mq-client/helpers"
"git.fjla.uk/owlboard/mq-client/log" "git.fjla.uk/owlboard/mq-client/log"
@ -10,14 +14,38 @@ import (
func main() { func main() {
log.Msg.Info("Initialised OwlBoard MQ Client " + helpers.Version) log.Msg.Info("Initialised OwlBoard MQ Client " + helpers.Version)
defer cleanup() defer cleanup()
dbAccess.PrintFromDbPackage()
go handleSignals()
vstp.Subscribe() 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() { func cleanup() {
log.Msg.Debug("Cleaning up open connections") log.Msg.Debug("Cleaning up open connections")
defer messaging.Disconnect(messaging.Client) if messaging.Client != nil {
defer dbAccess.CloseMongoClient() 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") log.Msg.Info("Program ready to exit")
if log.Msg != nil {
log.Msg.Sync()
}
} }

View File

@ -4,7 +4,6 @@ import (
"os" "os"
"git.fjla.uk/owlboard/mq-client/log" "git.fjla.uk/owlboard/mq-client/log"
"go.uber.org/zap"
"github.com/go-stomp/stomp/v3" "github.com/go-stomp/stomp/v3"
) )
@ -39,7 +38,7 @@ func dial() *stomp.Conn {
stomp.ConnOpt.Header("client-id", credentials.user+"-mq-client"), stomp.ConnOpt.Header("client-id", credentials.user+"-mq-client"),
) )
if err != nil { 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() conn.MustDisconnect()
} }

View File

@ -1,4 +1,26 @@
package vstp 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 // Decide, based on the DB Formatted message type, what action needs taking
// then either insert, or delete from the database as required // 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)
}

View File

@ -17,6 +17,7 @@ func handle(msg *stomp.Message) {
count++ count++
log.Msg.Info("Message count: " + fmt.Sprint(count)) log.Msg.Info("Message count: " + fmt.Sprint(count))
schedule := unmarshalData(string(msg.Body)) schedule := unmarshalData(string(msg.Body))
processEntryType(schedule)
saveToFile(schedule, "transformed") saveToFile(schedule, "transformed")
} }

View File

@ -1,6 +1,8 @@
package vstp package vstp
import ( import (
"time"
"git.fjla.uk/owlboard/mq-client/log" "git.fjla.uk/owlboard/mq-client/log"
"git.fjla.uk/owlboard/mq-client/messaging" "git.fjla.uk/owlboard/mq-client/messaging"
"github.com/go-stomp/stomp/v3" "github.com/go-stomp/stomp/v3"
@ -15,7 +17,13 @@ func Subscribe() {
go func() { go func() {
log.Msg.Debug("GOROUTINE: VSTP Message Handler Started") 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 { for {
msg := <-sub.C msg := <-sub.C
if msg.Err != nil { if msg.Err != nil {