diff --git a/cmd/crawler/main.go b/cmd/crawler/main.go index c2202b8..df34e7f 100644 --- a/cmd/crawler/main.go +++ b/cmd/crawler/main.go @@ -9,10 +9,8 @@ import ( "github/pippellia-btc/crawler/pkg/redb" "log" "os" - "os/signal" "runtime" "sync" - "syscall" "time" "github.com/nbd-wtf/go-nostr" @@ -23,7 +21,7 @@ import ( func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - go handleSignals(cancel) + go pipe.HandleSignals(cancel) config, err := config.Load() if err != nil { @@ -102,16 +100,6 @@ func main() { consumers.Wait() } -// handleSignals listens for OS signals and triggers context cancellation. -func handleSignals(cancel context.CancelFunc) { - signals := make(chan os.Signal, 1) - signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) - <-signals - - log.Println(" Signal received. Shutting down...") - cancel() -} - // enqueue things into the specified channel or return an error if full. func enqueue[T any](queue chan T) func(t T) error { return func(t T) error { diff --git a/cmd/sync/main.go b/cmd/sync/main.go index 6ea865a..3c9bdc0 100644 --- a/cmd/sync/main.go +++ b/cmd/sync/main.go @@ -9,10 +9,8 @@ import ( "github/pippellia-btc/crawler/pkg/redb" "log" "os" - "os/signal" "runtime" "sync" - "syscall" "time" "github.com/nbd-wtf/go-nostr" @@ -28,7 +26,7 @@ If Redis and the eventstore are already in sync, run the executable at /cmd/craw func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - go handleSignals(cancel) + go pipe.HandleSignals(cancel) config, err := config.Load() if err != nil { @@ -104,16 +102,6 @@ func main() { wg.Wait() } -// handleSignals listens for OS signals and triggers context cancellation. -func handleSignals(cancel context.CancelFunc) { - signals := make(chan os.Signal, 1) - signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) - <-signals - - log.Println(" Signal received. Shutting down...") - cancel() -} - // enqueue things into the specified channel or return an error if full. func enqueue[T any](queue chan T) func(t T) error { return func(t T) error { diff --git a/pkg/pipe/arbiter.go b/pkg/pipe/arbiter.go index acf8e22..fd667d2 100644 --- a/pkg/pipe/arbiter.go +++ b/pkg/pipe/arbiter.go @@ -8,7 +8,10 @@ import ( "github/pippellia-btc/crawler/pkg/redb" "github/pippellia-btc/crawler/pkg/walks" "log" + "os" + "os/signal" "sync/atomic" + "syscall" "time" ) @@ -210,3 +213,13 @@ func Promote(db redb.RedisDB, node graph.ID) error { return db.Promote(ctx, node) } + +// HandleSignals listens for OS signals and triggers context cancellation. +func HandleSignals(cancel context.CancelFunc) { + signals := make(chan os.Signal, 1) + signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) + <-signals + + log.Println("signal received. shutting down...") + cancel() +}