Skip to content

Commit

Permalink
Skip updates for non-GPX based workouts
Browse files Browse the repository at this point in the history
There is nothing to update there (for now)...
Also generically catch any panic in the background loop and recover from
it with a message.

Signed-off-by: Jo Vandeginste <Jo.Vandeginste@kuleuven.be>
  • Loading branch information
jovandeginste committed Jul 31, 2024
1 parent 59f066b commit 9fd9480
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
24 changes: 17 additions & 7 deletions pkg/app/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,27 @@ const (
)

func (a *App) BackgroundWorker() {
for {
a.bgLoop()
time.Sleep(WorkerDelay)
}
}

func (a *App) bgLoop() {
l := a.logger.With("module", "worker")

for {
l.Info("Worker started...")
defer func() {
if r := recover(); r != nil {
l.Error(fmt.Sprintf("Panic in bgLoop: %#v", r))
}
}()

a.updateWorkout(l)
a.autoImports(l)
l.Info("Worker started...")

l.Info("Worker finished...")
time.Sleep(WorkerDelay)
}
a.updateWorkout(l)
a.autoImports(l)

l.Info("Worker finished...")
}

func (a *App) autoImports(l *slog.Logger) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/database/workouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ func (w *Workout) Save(db *gorm.DB) error {
}

func (w *Workout) AsGPX() (*gpx.GPX, error) {
if w.GPX == nil {
return nil, errors.New("workout has no GPX")
}

return converters.Parse(w.GPX.Filename, w.GPX.Content)
}

Expand All @@ -250,6 +254,11 @@ func (w *Workout) setData(data *MapData) {
}

func (w *Workout) UpdateData(db *gorm.DB) error {
if w.GPX == nil {
// We only update data from (stored) GPX data
return nil
}

gpxContent, err := w.AsGPX()
if err != nil {
return err
Expand Down

0 comments on commit 9fd9480

Please sign in to comment.