Skip to content

Commit

Permalink
feat: enhance audio processing with context cancellation and improved…
Browse files Browse the repository at this point in the history
… error handling

- Added context cancellation support to audio file analysis, allowing for graceful shutdowns during processing.
- Implemented signal handling for user-initiated cancellations, ensuring clean exits without errors.
- Enhanced error handling in file and directory analysis, providing clearer feedback on issues encountered during processing.
- Updated the WAV and FLAC file validation logic to improve robustness and accuracy in file readiness checks.

These changes improve the user experience by allowing for more responsive and controlled audio processing, especially in long-running tasks.
  • Loading branch information
tphakala committed Jan 19, 2025
1 parent 32c6eb2 commit 04ff386
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 54 deletions.
31 changes: 30 additions & 1 deletion cmd/file/file.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package file

import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -18,12 +22,37 @@ func Command(settings *conf.Settings) *cobra.Command {
Long: `Analyze a single audio file for bird calls and songs.`,
Args: cobra.ExactArgs(1), // the command expects exactly one argument
RunE: func(cmd *cobra.Command, args []string) error {
// Create a context that can be cancelled
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Set up signal handling
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP)

// Handle shutdown in a separate goroutine
go func() {
sig := <-sigChan
fmt.Print("\n") // Add newline before the interrupt message
log.Printf("Received signal %v, initiating graceful shutdown...", sig)
cancel()
}()

// Input file path is the first argument
settings.Input.Path = args[0]
return analysis.FileAnalysis(settings)
err := analysis.FileAnalysis(settings, ctx)
if err == context.Canceled {
// Return nil for user-initiated cancellation
return nil
}
return err
},
}

// Disable printing usage on error
cmd.SilenceUsage = true
cmd.SilenceErrors = true

// Set up flags specific to the 'file' command
if err := setupFlags(cmd, settings); err != nil {
fmt.Printf("error setting up flags: %v\n", err)
Expand Down
Loading

0 comments on commit 04ff386

Please sign in to comment.