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 timetableCollection string = "timetable"
|
||||||
const databaseName string = "owlboard"
|
const databaseName string = "owlboard"
|
||||||
|
|
||||||
func init() {
|
func PushVersionToDb() {
|
||||||
version := database.Version{
|
version := database.Version{
|
||||||
Target: "mq-client",
|
Target: "mq-client",
|
||||||
Component: "mq-client",
|
Component: "mq-client",
|
||||||
|
@ -1,62 +1,43 @@
|
|||||||
package dbAccess
|
package dbAccess
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
||||||
"git.fjla.uk/owlboard/timetable-mgr/log"
|
"git.fjla.uk/owlboard/timetable-mgr/log"
|
||||||
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Generate DB Url
|
// Provide the DB Connection to other functions
|
||||||
var dbUri string = getDbUri()
|
var MongoClient (*mongo.Client)
|
||||||
|
|
||||||
func getDbUri() string {
|
func getDbUri(cfg *helpers.Configuration) string {
|
||||||
log.Msg.Debug("Fetching DB Access details")
|
var uri = "mongodb://" + cfg.DbUser + ":" + cfg.DbPass + "@" + cfg.DbHost + ":" + cfg.DbPort
|
||||||
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
|
|
||||||
return uri
|
return uri
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provide the DB Connection to other functions
|
|
||||||
var MongoClient (*mongo.Client) = initDataAccess()
|
|
||||||
|
|
||||||
// Configure bsonOpts
|
// Configure bsonOpts
|
||||||
var bsonOpts = &options.BSONOptions{
|
var bsonOpts = &options.BSONOptions{
|
||||||
UseJSONStructTags: true,
|
UseJSONStructTags: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialise the DB Connection
|
// 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)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
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 {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
log.Msg.Fatal("Error connecting to database: " + err.Error())
|
log.Msg.Fatal("Error connecting to database: " + err.Error())
|
||||||
} else {
|
} else {
|
||||||
log.Msg.Info("Database connection successful")
|
log.Msg.Info("Database connection successful")
|
||||||
}
|
}
|
||||||
return client
|
MongoClient = client
|
||||||
}
|
}
|
||||||
|
|
||||||
func CloseMongoClient() {
|
func CloseMongoClient() {
|
||||||
|
14
src/main.go
14
src/main.go
@ -6,6 +6,7 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"git.fjla.uk/owlboard/timetable-mgr/cif"
|
||||||
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
|
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
|
||||||
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
||||||
"git.fjla.uk/owlboard/timetable-mgr/log"
|
"git.fjla.uk/owlboard/timetable-mgr/log"
|
||||||
@ -24,14 +25,23 @@ func main() {
|
|||||||
|
|
||||||
log.Msg.Info("Initialised OwlBoard timetable-mgr " + helpers.Version)
|
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)
|
defer cleanup(cfg)
|
||||||
|
|
||||||
|
// Start CIF Task Ticker
|
||||||
|
cif.InitBackgroundTask(cfg)
|
||||||
|
|
||||||
if cfg.VstpOn {
|
if cfg.VstpOn {
|
||||||
messaging.StompInit(cfg)
|
messaging.StompInit(cfg)
|
||||||
go handleSignals(cfg)
|
|
||||||
|
|
||||||
vstp.Subscribe()
|
vstp.Subscribe()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
select {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Traps SIGINT and SIGTERM signals and ensures cleanup() is run
|
// Traps SIGINT and SIGTERM signals and ensures cleanup() is run
|
||||||
|
Loading…
Reference in New Issue
Block a user