Skip to content

Commit

Permalink
fix: iqama configuration not loading
Browse files Browse the repository at this point in the history
This fix makes the monolith reload the config every minute instead of
on-restart.

It's also a prepartion for adding a database, where if the end-user changes
the record time it will take it into consideration right away.
  • Loading branch information
serafdev committed Sep 30, 2024
1 parent 3ecc6f5 commit ddea464
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 78 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ env:

jobs:
docker-push-qa:
needs: release
runs-on: ubuntu-latest

permissions:
contents: read
packages: write
Expand Down
67 changes: 36 additions & 31 deletions cmd/monolith/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,39 +61,50 @@ func main() {
if config.Features.Record {
host := os.Getenv("MDROID_OBS_WEBSOCKET_HOST")
password := os.Getenv("MDROID_OBS_WEBSOCKET_PASSWORD")
data := rec2.NewRecordConfigDataS()

obsClient := startServerWithRetry(host, password, data)

// Calculate the duration until midnight
now := time.Now()
night := time.Date(now.Year(), now.Month(), now.Day()+1, 2, 0, 0, 0, now.Location())
duration := night.Sub(now)
go func() {
for {
data := rec2.NewRecordConfigDataS()
obsClient, err := rec2.StartRecServer(host, password, data)
if err != nil {
fmt.Printf("could not reach or authenticate to OBS, retrying in 1 minute...\n")
time.Sleep(1 * time.Minute)
continue
}

// Create a timer that waits until midnight
timer := time.NewTimer(duration)
<-timer.C // This blocks until the timer fires
// Check recording status every minute
for {
data = rec2.NewRecordConfigDataS()
shouldRecord := rec2.SupposedToBeRecording(data)
isRecording, err := obsClient.IsRecording()
if err != nil {
fmt.Printf("couldn't check if OBS is recording: %v\n", err)
continue
}

// Now that it's midnight, start a ticker that ticks every 24 hours
ticker := time.NewTicker(24 * time.Hour)
if shouldRecord && !isRecording {
fmt.Println("should be recording")
err := obsClient.StartRecording()
if err != nil {
fmt.Printf("couldn't start recording: %v\n", err)
}
} else if !shouldRecord && isRecording {
fmt.Println("should not be recording")
err := obsClient.StopRecording()
if err != nil {
fmt.Printf("couldn't stop recording: %v\n", err)
}
}
time.Sleep(1 * time.Minute)

// Call rec.StartRecServer every time the ticker ticks
go func() {
for range ticker.C {
err := obsClient.Disconnect()
if err != nil {
return
}
obsClient = startServerWithRetry(host, password, data)
}
}()

}

out:
for {
select {
// discord msgs dispatcher
case msg := <-msgs:
fmt.Printf("%v, operation received from discord: %s\n", time.Now(), msg)
if strings.HasPrefix(msg, "rec-") {
Expand All @@ -106,13 +117,10 @@ out:
}
}
case <-stop:
// simplest way to wait for the nested go routines to clean up
// takes < 2 ms but better be safe
time.Sleep(10 * time.Second)
break out
}
}

}

func bot() {
Expand All @@ -121,17 +129,14 @@ func bot() {

logger, _ := zap.NewProduction()
discordBot := discord.NewDiscordBot(logger, guildID, botToken, true)
discordBot.StartBot()
}

func startServerWithRetry(host string, password string, data *rec2.RecordConfigDataS) *rec2.Recorder {
for {
obs, err := rec2.StartRecServer(host, password, data)
err := discordBot.StartBot()
if err != nil {
fmt.Printf("could not reach or authenticate to OBS, retrying in 1 minutes...\n")
time.Sleep(1 * time.Minute)
logger.Error("Failed to start Discord bot, retrying in 10 minutes", zap.Error(err))
time.Sleep(10 * time.Minute)
} else {
return obs
break
}
}
}
12 changes: 7 additions & 5 deletions pkg/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

type Bot interface {
StartBot()
StartBot() error
}

type bot struct {
Expand All @@ -41,20 +41,21 @@ func NewDiscordBot(logger *zap.Logger, adminGuildID, botToken string, isPublic b
}

// StartBot starts the bot
func (d *bot) StartBot() {
d.run(true, nil, nil)
func (d *bot) StartBot() error {
return d.run(true, nil, nil)
}

// run starts the bot and listens for incoming commands
func (d *bot) run(removeCommands bool, obs *rec.Recorder, notifyChan chan string) {
func (d *bot) run(removeCommands bool, obs *rec.Recorder, notifyChan chan string) error {
var err error

d.addInteractionCreateHandlers()
d.addReadyHandlers()

err = d.session.Open()
if err != nil {
d.logger.Fatal("Cannot open the session", zap.Error(err))
d.logger.Error("Cannot open the session", zap.Error(err))
return err
}

d.logger.Info("Starting Discord Bot",
Expand Down Expand Up @@ -83,6 +84,7 @@ func (d *bot) run(removeCommands bool, obs *rec.Recorder, notifyChan chan string
<-stop
time.Sleep(10 * time.Second) // Graceful shutdown, giving some time for the bot to respond to the commands

return nil
}

func (d *bot) addInteractionCreateHandlers() {
Expand Down
1 change: 0 additions & 1 deletion pkg/iqama/v2/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func NewIqamaCSV(masjid string) Iqama {
func (i *IqamaCSV) GetTodayTimes() (*IqamaDailyTimes, error) {
// Get today's date
today := time.Now()

times, err := i.iqamaForDate(today)
handleErr(err)
return &times, nil
Expand Down
40 changes: 1 addition & 39 deletions pkg/rec/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,15 @@ package rec

import (
"fmt"
"time"
)

func StartRecServer(host, password string, data *RecordConfigDataS) (*Recorder, error) {
client, err := New(host, password)
if err != nil {
fmt.Println("could not initiate client")
return nil, nil
return nil, err
}

fmt.Println("Starting OBS recording control routine")
go func() {
for {

isRecording, err := client.IsRecording()
if err != nil {
fmt.Printf("couldn't check if OBS is recording: %v\n", err)
}

shouldRecord := SupposedToBeRecording(data)

// Start recording if supposed to be recording but currently not recording
if shouldRecord && !isRecording {
fmt.Println("should be recording")
err := client.StartRecording()
if err != nil {
fmt.Printf("couldn't start recording: %v\n", err)
}
}

//var recordTimeLimit float64
//{
// recordTimeLimit = 2 * 60 * 60 * 1000
//}

// Stop recording if not supposed to be recording but currently recording
if !shouldRecord && isRecording { // && (client.RecordTime() > recordTimeLimit) {
fmt.Println("should not be recording")
err := client.StopRecording()
if err != nil {
fmt.Printf("couldn't stop recording: %v\n", err)
}
}

time.Sleep(1 * time.Minute)
}
}()

return client, nil
}

0 comments on commit ddea464

Please sign in to comment.