Compare commits
2 Commits
57244c6971
...
d81cade73b
Author | SHA1 | Date |
---|---|---|
Fred Boniface | d81cade73b | |
Fred Boniface | b0cbab3e34 |
|
@ -0,0 +1,22 @@
|
|||
package cif
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
||||
)
|
||||
|
||||
// Start a background task to periodically check and update the timetable from the CIF file feed.
|
||||
|
||||
func InitBackgroundTask(cfg *helpers.Configuration) {
|
||||
go runBackgroundTask(cfg)
|
||||
}
|
||||
|
||||
func runBackgroundTask(cfg *helpers.Configuration) {
|
||||
ticker := time.NewTicker(10 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
cifCheck(cfg)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package cif
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/log"
|
||||
)
|
||||
|
||||
func cifCheck(cfg *helpers.Configuration) {
|
||||
log.Msg.Debug("CIF Task Started")
|
||||
numGoroutines := runtime.NumGoroutine()
|
||||
fmt.Println("Number of goroutines running: ", numGoroutines)
|
||||
}
|
|
@ -4,8 +4,8 @@ import (
|
|||
"context"
|
||||
|
||||
"git.fjla.uk/owlboard/go-types/pkg/database"
|
||||
"git.fjla.uk/owlboard/mq-client/helpers"
|
||||
"git.fjla.uk/owlboard/mq-client/log"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/log"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
@ -13,7 +13,7 @@ import (
|
|||
const timetableCollection string = "timetable"
|
||||
const databaseName string = "owlboard"
|
||||
|
||||
func init() {
|
||||
func PushVersionToDb() {
|
||||
version := database.Version{
|
||||
Target: "mq-client",
|
||||
Component: "mq-client",
|
||||
|
|
|
@ -1,62 +1,43 @@
|
|||
package dbAccess
|
||||
|
||||
import (
|
||||
"git.fjla.uk/owlboard/mq-client/log"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/log"
|
||||
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// Generate DB Url
|
||||
var dbUri string = getDbUri()
|
||||
// Provide the DB Connection to other functions
|
||||
var MongoClient (*mongo.Client)
|
||||
|
||||
func getDbUri() string {
|
||||
log.Msg.Debug("Fetching DB Access details")
|
||||
var dbHost string = os.Getenv("OWL_DB_HOST")
|
||||
if dbHost == "" {
|
||||
dbHost = "localhost"
|
||||
}
|
||||
var dbPort string = os.Getenv("OWL_DB_PORT")
|
||||
if dbPort == "" {
|
||||
dbPort = "27017"
|
||||
}
|
||||
var dbUser string = os.Getenv("OWL_DB_USER")
|
||||
if dbUser == "" {
|
||||
dbUser = "owl"
|
||||
}
|
||||
var dbPass string = os.Getenv("OWL_DB_PASS")
|
||||
if dbPass == "" {
|
||||
dbPass = "twittwoo"
|
||||
}
|
||||
var uri = "mongodb://" + dbUser + ":" + dbPass + "@" + dbHost + ":" + dbPort
|
||||
func getDbUri(cfg *helpers.Configuration) string {
|
||||
var uri = "mongodb://" + cfg.DbUser + ":" + cfg.DbPass + "@" + cfg.DbHost + ":" + cfg.DbPort
|
||||
return uri
|
||||
}
|
||||
|
||||
// Provide the DB Connection to other functions
|
||||
var MongoClient (*mongo.Client) = initDataAccess()
|
||||
|
||||
// Configure bsonOpts
|
||||
var bsonOpts = &options.BSONOptions{
|
||||
UseJSONStructTags: true,
|
||||
}
|
||||
|
||||
// Initialise the DB Connection
|
||||
func initDataAccess() *mongo.Client {
|
||||
func InitDataAccess(cfg *helpers.Configuration) {
|
||||
uri := getDbUri(cfg)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
client, err := mongo.Connect(ctx, options.Client().ApplyURI(dbUri).SetBSONOptions(bsonOpts))
|
||||
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri).SetBSONOptions(bsonOpts))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
log.Msg.Fatal("Error connecting to database: " + err.Error())
|
||||
} else {
|
||||
log.Msg.Info("Database connection successful")
|
||||
}
|
||||
return client
|
||||
MongoClient = client
|
||||
}
|
||||
|
||||
func CloseMongoClient() {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module git.fjla.uk/owlboard/mq-client
|
||||
module git.fjla.uk/owlboard/timetable-mgr
|
||||
|
||||
go 1.19
|
||||
go 1.21
|
||||
|
||||
require (
|
||||
git.fjla.uk/owlboard/go-types v0.0.0-20230727192011-171bd3eafd83
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
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-20230727191457-d15ddc556312 h1:IolAJJTttdcmykOI73Zjfh3V8Gd01l9TrM+OmliM4h0=
|
||||
git.fjla.uk/owlboard/go-types v0.0.0-20230727191457-d15ddc556312/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
|
||||
git.fjla.uk/owlboard/go-types v0.0.0-20230727192011-171bd3eafd83 h1:q+I66M4YVRnKwdyYqcwou7TTniM1uwUSh3Bpa8SDLuM=
|
||||
git.fjla.uk/owlboard/go-types v0.0.0-20230727192011-171bd3eafd83/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
|
||||
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
|
||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
|
@ -26,11 +23,13 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
|||
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
|
||||
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
|
||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
||||
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
|
||||
|
@ -46,6 +45,7 @@ go.mongodb.org/mongo-driver v1.12.0/go.mod h1:AZkxhPnFJUoH7kZlFkVKucV20K387miPfm
|
|||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
|
||||
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
|
||||
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
|
||||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
|
||||
|
@ -97,3 +97,4 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8T
|
|||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
|
||||
"git.fjla.uk/owlboard/mq-client/helpers"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
||||
)
|
||||
|
||||
var Msg *zap.Logger
|
||||
|
|
35
src/main.go
35
src/main.go
|
@ -6,11 +6,12 @@ import (
|
|||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"git.fjla.uk/owlboard/mq-client/dbAccess"
|
||||
"git.fjla.uk/owlboard/mq-client/helpers"
|
||||
"git.fjla.uk/owlboard/mq-client/log"
|
||||
"git.fjla.uk/owlboard/mq-client/messaging"
|
||||
"git.fjla.uk/owlboard/mq-client/vstp"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/cif"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/log"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/messaging"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/vstp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -24,31 +25,45 @@ func main() {
|
|||
|
||||
log.Msg.Info("Initialised OwlBoard timetable-mgr " + helpers.Version)
|
||||
|
||||
defer cleanup()
|
||||
dbAccess.InitDataAccess(cfg)
|
||||
dbAccess.PushVersionToDb()
|
||||
|
||||
go handleSignals()
|
||||
// Handle signals from the OS
|
||||
go handleSignals(cfg)
|
||||
|
||||
defer cleanup(cfg)
|
||||
|
||||
// Start CIF Task Ticker
|
||||
cif.InitBackgroundTask(cfg)
|
||||
|
||||
if cfg.VstpOn {
|
||||
messaging.StompInit(cfg)
|
||||
vstp.Subscribe()
|
||||
}
|
||||
|
||||
select {}
|
||||
}
|
||||
|
||||
// Traps SIGINT and SIGTERM signals and ensures cleanup() is run
|
||||
func handleSignals() {
|
||||
func handleSignals(cfg *helpers.Configuration) {
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
sig := <-sigChan
|
||||
log.Msg.Warn("Signal received: " + sig.String())
|
||||
cleanup()
|
||||
cleanup(cfg)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Cleans up open connections ready for a clean exit of the program
|
||||
func cleanup() {
|
||||
func cleanup(cfg *helpers.Configuration) {
|
||||
log.Msg.Debug("Cleaning up open connections")
|
||||
if cfg.VstpOn {
|
||||
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()
|
||||
|
|
|
@ -1,41 +1,23 @@
|
|||
package messaging
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"git.fjla.uk/owlboard/mq-client/log"
|
||||
|
||||
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/log"
|
||||
"github.com/go-stomp/stomp/v3"
|
||||
)
|
||||
|
||||
type nrodCredential struct {
|
||||
user string
|
||||
pass string
|
||||
}
|
||||
var Client *stomp.Conn
|
||||
|
||||
// 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")
|
||||
if nrod_user == "" || nrod_pass == "" {
|
||||
log.Msg.Fatal("No NROD Credentials provided")
|
||||
}
|
||||
log.Msg.Debug("NROD Credentials loaded for user: " + nrod_user)
|
||||
return nrodCredential{
|
||||
user: nrod_user,
|
||||
pass: nrod_pass,
|
||||
}
|
||||
func StompInit(cfg *helpers.Configuration) {
|
||||
Client = dial(cfg.NrodUser, cfg.NrodPass)
|
||||
}
|
||||
|
||||
var Client = dial()
|
||||
|
||||
// Connects the STOMP file to the Network Rail MQ Server
|
||||
func dial() *stomp.Conn {
|
||||
var credentials nrodCredential = getCredentials()
|
||||
func dial(user, pass string) *stomp.Conn {
|
||||
conn, err := stomp.Dial("tcp", "publicdatafeeds.networkrail.co.uk:61618",
|
||||
stomp.ConnOpt.Login(credentials.user, credentials.pass),
|
||||
stomp.ConnOpt.Login(user, pass),
|
||||
stomp.ConnOpt.HeartBeat(15000, 15000),
|
||||
stomp.ConnOpt.Header("client-id", credentials.user+"-mq-client"),
|
||||
stomp.ConnOpt.Header("client-id", user+"-mq-client"),
|
||||
)
|
||||
if err != nil {
|
||||
log.Msg.Fatal("Unable to connect to STOMP Client: " + err.Error())
|
||||
|
|
|
@ -4,8 +4,8 @@ import (
|
|||
"fmt"
|
||||
|
||||
"git.fjla.uk/owlboard/go-types/pkg/database"
|
||||
"git.fjla.uk/owlboard/mq-client/dbAccess"
|
||||
"git.fjla.uk/owlboard/mq-client/log"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/log"
|
||||
)
|
||||
|
||||
// Decide, based on the DB Formatted message type, what action needs taking
|
||||
|
|
|
@ -3,7 +3,7 @@ package vstp
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"git.fjla.uk/owlboard/mq-client/log"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/log"
|
||||
"github.com/go-stomp/stomp/v3"
|
||||
)
|
||||
|
||||
|
|
|
@ -9,8 +9,8 @@ import (
|
|||
|
||||
"git.fjla.uk/owlboard/go-types/pkg/database"
|
||||
"git.fjla.uk/owlboard/go-types/pkg/upstreamApi"
|
||||
"git.fjla.uk/owlboard/mq-client/helpers"
|
||||
"git.fjla.uk/owlboard/mq-client/log"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/log"
|
||||
)
|
||||
|
||||
// Unmarshals the JSON data and runs it through the formatData() function and returns the data in a DB ready Struct
|
||||
|
|
|
@ -3,8 +3,8 @@ package vstp
|
|||
import (
|
||||
"time"
|
||||
|
||||
"git.fjla.uk/owlboard/mq-client/log"
|
||||
"git.fjla.uk/owlboard/mq-client/messaging"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/log"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/messaging"
|
||||
"github.com/go-stomp/stomp/v3"
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in New Issue