From 039a3b40c38afd98d02c7545ebf7421b0b10398a Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Wed, 27 Jun 2012 13:44:51 +0300 Subject: [PATCH] handling sig hup signal for scheduler --- cmd/cgr-scheduler/cgr-scheduler.go | 33 ++++++++++++++++++++++++------ timespans/actions.go | 2 -- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/cmd/cgr-scheduler/cgr-scheduler.go b/cmd/cgr-scheduler/cgr-scheduler.go index a3c23f51e..42909df03 100644 --- a/cmd/cgr-scheduler/cgr-scheduler.go +++ b/cmd/cgr-scheduler/cgr-scheduler.go @@ -19,11 +19,14 @@ along with this program. If not, see package main import ( - "log" - "github.com/cgrates/cgrates/timespans" "flag" - "time" + "github.com/cgrates/cgrates/timespans" + "log" + "os" "sort" + "time" + "os/signal" + "syscall" ) var ( @@ -72,17 +75,35 @@ func (s scheduler) loop() { } } -func main() { - flag.Parse() +// Listens for the HUP system signal and gracefuly reloads the timers from database. +func stopSingnalHandler() { + log.Print("Handling HUP signal...") + c := make(chan os.Signal) + signal.Notify(c, syscall.SIGHUP) + sig := <-c + + log.Printf("Caught signal %v, reloading action timings.\n", sig) + loadActionTimings() +} + +func loadActionTimings() (actionTimings []*timespans.ActionTiming, err error) { storage, err := timespans.NewRedisStorage(*redisserver, *redisdb) + defer storage.Close() if err != nil { log.Fatalf("Could not open database connection: %v", err) } - actionTimings, err := storage.GetAllActionTimings() + actionTimings, err = storage.GetAllActionTimings() + return +} + +func main() { + flag.Parse() + actionTimings, err := loadActionTimings() if err != nil { log.Fatalf("Cannot get action timings:", err) } s := scheduler{} s.queue = append(s.queue, actionTimings...) + go stopSingnalHandler() s.loop() } diff --git a/timespans/actions.go b/timespans/actions.go index 58a7db8c2..6f1fd6935 100644 --- a/timespans/actions.go +++ b/timespans/actions.go @@ -186,7 +186,6 @@ MONTHS: // reset the monthday if i.MonthDays != nil { t = time.Date(t.Year(), t.Month(), i.MonthDays[0], t.Hour(), t.Minute(), t.Second(), 0, t.Location()) - log.Print("here: ", i.MonthDays) } } } else { // today was not found in the list, x is the first greater day @@ -194,7 +193,6 @@ MONTHS: // reset the monthday if i.MonthDays != nil { t = time.Date(t.Year(), t.Month(), i.MonthDays[0], t.Hour(), t.Minute(), t.Second(), 0, t.Location()) - log.Print("here1: ", i.MonthDays) } } }