extracted handle signals

This commit is contained in:
pippellia-btc
2025-06-10 16:44:53 +02:00
parent b856f03605
commit b60eb9d10b
3 changed files with 15 additions and 26 deletions

View File

@@ -9,10 +9,8 @@ import (
"github/pippellia-btc/crawler/pkg/redb" "github/pippellia-btc/crawler/pkg/redb"
"log" "log"
"os" "os"
"os/signal"
"runtime" "runtime"
"sync" "sync"
"syscall"
"time" "time"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
@@ -23,7 +21,7 @@ import (
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
go handleSignals(cancel) go pipe.HandleSignals(cancel)
config, err := config.Load() config, err := config.Load()
if err != nil { if err != nil {
@@ -102,16 +100,6 @@ func main() {
consumers.Wait() 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. // enqueue things into the specified channel or return an error if full.
func enqueue[T any](queue chan T) func(t T) error { func enqueue[T any](queue chan T) func(t T) error {
return func(t T) error { return func(t T) error {

View File

@@ -9,10 +9,8 @@ import (
"github/pippellia-btc/crawler/pkg/redb" "github/pippellia-btc/crawler/pkg/redb"
"log" "log"
"os" "os"
"os/signal"
"runtime" "runtime"
"sync" "sync"
"syscall"
"time" "time"
"github.com/nbd-wtf/go-nostr" "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() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
go handleSignals(cancel) go pipe.HandleSignals(cancel)
config, err := config.Load() config, err := config.Load()
if err != nil { if err != nil {
@@ -104,16 +102,6 @@ func main() {
wg.Wait() 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. // enqueue things into the specified channel or return an error if full.
func enqueue[T any](queue chan T) func(t T) error { func enqueue[T any](queue chan T) func(t T) error {
return func(t T) error { return func(t T) error {

View File

@@ -8,7 +8,10 @@ import (
"github/pippellia-btc/crawler/pkg/redb" "github/pippellia-btc/crawler/pkg/redb"
"github/pippellia-btc/crawler/pkg/walks" "github/pippellia-btc/crawler/pkg/walks"
"log" "log"
"os"
"os/signal"
"sync/atomic" "sync/atomic"
"syscall"
"time" "time"
) )
@@ -210,3 +213,13 @@ func Promote(db redb.RedisDB, node graph.ID) error {
return db.Promote(ctx, node) 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()
}