Skip to content

Commit

Permalink
fix: Refactor ClipCleanupMonitor function for improved error handling…
Browse files Browse the repository at this point in the history
… and logging
  • Loading branch information
tphakala committed Apr 20, 2024
1 parent 16c0961 commit 5b1bc67
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
25 changes: 17 additions & 8 deletions internal/analysis/realtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,32 +168,41 @@ func closeDataStore(store datastore.Interface) {
}
}

// ClipCleanupMonitor monitors the database and deletes clips that meets the retention policy.
// ClipCleanupMonitor monitors the database and deletes clips that meet the retention policy.
func ClipCleanupMonitor(wg *sync.WaitGroup, settings *conf.Settings, dataStore datastore.Interface, quitChan chan struct{}) {
defer wg.Done()
defer wg.Done() // Ensure that the WaitGroup is marked as done after the function exits

// Creating a ticker that ticks every 1 minute
// Create a ticker that triggers every minute to perform cleanup
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
defer ticker.Stop() // Ensure the ticker is stopped to prevent leaks

for {
select {
case <-quitChan:
// Quit signal received, stop the clip cleanup monitor
// Handle quit signal to stop the monitor
return

case <-ticker.C: // Wait for the next tick
clipsForRemoval, _ := dataStore.GetClipsQualifyingForRemoval(settings.Realtime.Retention.MinEvictionHours, settings.Realtime.Retention.MinClipsPerSpecies)
case <-ticker.C:
// Perform cleanup operation on every tick
clipsForRemoval, err := dataStore.GetClipsQualifyingForRemoval(settings.Realtime.Retention.MinEvictionHours, settings.Realtime.Retention.MinClipsPerSpecies)
if err != nil {
log.Printf("Error retrieving clips for removal: %s\n", err)
continue // Skip this tick's cleanup if there's an error
}

log.Printf("Found %d clips to remove\n", len(clipsForRemoval))

for _, clip := range clipsForRemoval {
// Attempt to remove the clip file from the filesystem
if err := os.Remove(clip.ClipName); err != nil {
log.Printf("Failed to remove %s: %s\n", clip.ClipName, err)
} else {
log.Printf("Removed %s\n", clip.ClipName)
// Only attempt to delete the database record if the file removal was successful
if err := dataStore.DeleteNoteClipPath(clip.ID); err != nil {
log.Printf("Failed to delete clip path for %s: %s\n", clip.ID, err)
}
}
dataStore.DeleteNoteClipPath(clip.ID)
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions internal/datastore/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,18 @@ func (ds *DataStore) GetNoteClipPath(noteID string) (string, error) {

// DeleteNoteClipPath deletes the field representing the path to the audio clip associated with a note.
func (ds *DataStore) DeleteNoteClipPath(noteID string) error {
err := ds.DB.Model(&Note{}).
Where("id = ?", noteID).
Update("clip_name", "").Error
// Validate the input parameter
if noteID == "" {
return fmt.Errorf("invalid note ID: must not be empty")
}

// Update the clip_name field to an empty string for the specified note ID
err := ds.DB.Model(&Note{}).Where("id = ?", noteID).Update("clip_name", "").Error
if err != nil {
return fmt.Errorf("failed to delete clip path: %w", err)
return fmt.Errorf("failed to delete clip path for note ID %s: %w", noteID, err)
}

// Return nil if no errors occurred, indicating successful execution
return nil
}

Expand Down

0 comments on commit 5b1bc67

Please sign in to comment.