Refactor code to allow for centralised configuration fetching and the running of background ticker
This commit is contained in:
parent
b0cbab3e34
commit
d81cade73b
22
src/cif/initialise.go
Normal file
22
src/cif/initialise.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
15
src/cif/runner.go
Normal file
15
src/cif/runner.go
Normal file
@ -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)
|
||||
}
|
@ -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/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() {
|
||||
|
14
src/main.go
14
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
|
||||
|
Loading…
Reference in New Issue
Block a user