better make negative and logging for action timings

This commit is contained in:
Radu Ioan Fericean
2012-09-08 15:14:27 +03:00
parent 74c3fb9d5c
commit 448db86825
6 changed files with 20 additions and 7 deletions

View File

@@ -414,8 +414,8 @@ func main() {
timespans.Logger.Info("Starting CGRateS scheduler.")
go func() {
sched := scheduler.NewScheduler()
sched.LoadActionTimings(getter)
go reloadSchedulerSingnalHandler(sched, getter)
sched.LoadActionTimings(getter)
sched.Loop()
}()
}

View File

@@ -99,7 +99,6 @@ func registerToBalancer() {
func reloadSchedulerSingnalHandler(sched *scheduler.Scheduler, getter timespans.DataStorage) {
timespans.Logger.Info("Handling HUP signal...")
for {
timespans.Logger.Debug("here again")
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGHUP)
sig := <-c

View File

@@ -78,7 +78,7 @@ func (s *Scheduler) LoadActionTimings(storage timespans.DataStorage) {
isAsap = at.CheckForASAP()
toBeSaved = toBeSaved || isAsap
if at.IsOneTimeRun() {
timespans.Logger.Info(fmt.Sprintf("Time for one time action on %v", at))
timespans.Logger.Info(fmt.Sprintf("Time for one time action on %v", key))
go at.Execute()
// do not append it to the newAts list to be saved
} else {

View File

@@ -128,8 +128,10 @@ func resetCountersAction(ub *UserBalance, a *Action) (err error) {
}
func genericMakeNegative(a *Action) {
a.Units = -a.Units
if a.MinuteBucket != nil {
if a.Units > 0 { // only apply if not allready negative
a.Units = -a.Units
}
if a.MinuteBucket != nil && a.MinuteBucket.Seconds > 0 {
a.MinuteBucket.Seconds = -a.MinuteBucket.Seconds
}
}

View File

@@ -56,7 +56,7 @@ func (at *ActionTiming) GetNextStartTime() (t time.Time) {
now := time.Now()
y, m, d := now.Date()
z, _ := now.Zone()
if i.StartTime != "" {
if i.StartTime != "" && i.StartTime != ASAP {
l := fmt.Sprintf("%d-%d-%d %s %s", y, m, d, i.StartTime, z)
var err error
t, err = time.Parse(FORMAT, l)
@@ -219,7 +219,7 @@ func (at *ActionTiming) Execute() (err error) {
}
for _, ub := range at.getUserBalances() {
AccLock.Guard(ub.Id, func() (float64, error) {
go Logger.Info(fmt.Sprintf("Executing %v: %v", ub.Id, a))
Logger.Info(fmt.Sprintf("Executing %v on %v", a.ActionType, ub.Id))
err = actionFunction(ub, a)
storageGetter.SetUserBalance(ub)
return 0, nil

View File

@@ -816,6 +816,18 @@ func TestActionTimingLogging(t *testing.T) {
}
}
func TestActionMakeNegative(t *testing.T) {
a := &Action{Units: 10}
genericMakeNegative(a)
if a.Units > 0 {
t.Error("Failed to make negative: ", a)
}
genericMakeNegative(a)
if a.Units > 0 {
t.Error("Failed to preserve negative: ", a)
}
}
/********************************** Benchmarks ********************************/
func BenchmarkUUID(b *testing.B) {