From d81cade73b3cd2b827364339bd2fc44b9c4b78b0 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Mon, 25 Mar 2024 12:21:59 +0000 Subject: [PATCH] Refactor code to allow for centralised configuration fetching and the running of background ticker --- src/cif/initialise.go | 22 ++++++++++++++++++++++ src/cif/runner.go | 15 +++++++++++++++ src/dbAccess/access.go | 2 +- src/dbAccess/client.go | 37 +++++++++---------------------------- src/main.go | 14 ++++++++++++-- 5 files changed, 59 insertions(+), 31 deletions(-) create mode 100644 src/cif/initialise.go create mode 100644 src/cif/runner.go diff --git a/src/cif/initialise.go b/src/cif/initialise.go new file mode 100644 index 0000000..8258b04 --- /dev/null +++ b/src/cif/initialise.go @@ -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) + } +} diff --git a/src/cif/runner.go b/src/cif/runner.go new file mode 100644 index 0000000..7efea7b --- /dev/null +++ b/src/cif/runner.go @@ -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) +} diff --git a/src/dbAccess/access.go b/src/dbAccess/access.go index 3dbeab0..3fab5a9 100644 --- a/src/dbAccess/access.go +++ b/src/dbAccess/access.go @@ -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", diff --git a/src/dbAccess/client.go b/src/dbAccess/client.go index bcf1edb..7307f44 100644 --- a/src/dbAccess/client.go +++ b/src/dbAccess/client.go @@ -1,62 +1,43 @@ package dbAccess import ( + "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() { diff --git a/src/main.go b/src/main.go index fff9197..c31b1a7 100644 --- a/src/main.go +++ b/src/main.go @@ -6,6 +6,7 @@ import ( "os/signal" "syscall" + "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" @@ -24,14 +25,23 @@ func main() { log.Msg.Info("Initialised OwlBoard timetable-mgr " + helpers.Version) + dbAccess.InitDataAccess(cfg) + dbAccess.PushVersionToDb() + + // Handle signals from the OS + go handleSignals(cfg) + defer cleanup(cfg) + // Start CIF Task Ticker + cif.InitBackgroundTask(cfg) + if cfg.VstpOn { messaging.StompInit(cfg) - go handleSignals(cfg) - vstp.Subscribe() } + + select {} } // Traps SIGINT and SIGTERM signals and ensures cleanup() is run