Skip to content

Commit

Permalink
feat: enhance directory analysis with context cancellation and signal…
Browse files Browse the repository at this point in the history
… handling

- Added context cancellation support to the directory analysis process, allowing for graceful shutdowns during file processing.
- Implemented signal handling for SIGINT and SIGTERM to manage user-initiated cancellations effectively.
- Updated the DirectoryAnalysis function to include a context parameter, ensuring that analysis can be interrupted cleanly.
- Enhanced error handling to provide clearer feedback on cancellation and processing issues.
- Introduced a new watchDirectory function to continuously monitor for new audio files, improving real-time processing capabilities.

These changes improve the responsiveness and robustness of the audio analysis workflow, enhancing user experience during long-running tasks.
  • Loading branch information
tphakala committed Jan 19, 2025
1 parent 5301a46 commit 57d555e
Show file tree
Hide file tree
Showing 2 changed files with 317 additions and 434 deletions.
39 changes: 37 additions & 2 deletions cmd/directory/directory.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package directory

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

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -18,13 +21,45 @@ func Command(settings *conf.Settings) *cobra.Command {
Long: "Provide a directory path to analyze all *.wav files within it.",
Args: cobra.ExactArgs(1),
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)

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

// Ensure cleanup on exit
defer func() {
signal.Stop(sigChan)
}()

// The directory to analyze is passed as the first argument
settings.Input.Path = args[0]
return analysis.DirectoryAnalysis(settings)
err := analysis.DirectoryAnalysis(settings, ctx)
if err != nil {
if err == context.Canceled {
return nil
}
return err
}
return nil
},
}

// Set up flags specific to the 'file' command
// Disable printing usage on error
cmd.SilenceUsage = true
cmd.SilenceErrors = true

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

0 comments on commit 57d555e

Please sign in to comment.