From a7033f88c839a58e2bae363f9dd6fc9873f6d1c2 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 10 Aug 2012 18:26:47 +0300 Subject: [PATCH] even better one time run --- cmd/cgr-rater/scheduler.go | 10 ++++++++-- timespans/action_timing.go | 17 ++++++++++++----- timespans/actions_test.go | 21 ++++++++++++++++++++- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/cmd/cgr-rater/scheduler.go b/cmd/cgr-rater/scheduler.go index ca925746c..38a7eb2a7 100644 --- a/cmd/cgr-rater/scheduler.go +++ b/cmd/cgr-rater/scheduler.go @@ -73,9 +73,15 @@ func loadActionTimings(storage timespans.StorageGetter) { sched.queue = timespans.ActionTimingPriotityList{} for key, ats := range actionTimings { toBeSaved := false - for _, at := range ats { + for i, at := range ats { toBeSaved = toBeSaved || at.CheckForASAP() - sched.queue = append(sched.queue, at) + if at.IsOneTimeRun() { + go at.Execute() + // remove it from list + ats = append(ats[:i], ats[i+1:]...) + } else { + sched.queue = append(sched.queue, at) + } } if toBeSaved { storage.SetActionTimings(key, ats) diff --git a/timespans/action_timing.go b/timespans/action_timing.go index bf3cdc3e0..67b944fb2 100644 --- a/timespans/action_timing.go +++ b/timespans/action_timing.go @@ -228,20 +228,27 @@ func (at *ActionTiming) Execute() (err error) { return } +// checks for *asap string as start time and replaces it wit an actual time in the newar future +// returns true if the *asap string was found func (at *ActionTiming) CheckForASAP() bool { if at.Timing.StartTime == ASAP { oneMinute, _ := time.ParseDuration(ASAP_DELAY) - timeToRun := time.Now().Add(oneMinute) - timeTokens := strings.Split(timeToRun.Format(time.Stamp), " ") - at.Timing.Years = Years{timeToRun.Year()} - at.Timing.Months = Months{timeToRun.Month()} - at.Timing.MonthDays = MonthDays{timeToRun.Day()} + timeTokens := strings.Split(time.Now().Add(oneMinute).Format(time.Stamp), " ") at.Timing.StartTime = timeTokens[len(timeTokens)-1] return true } return false } +// returns true if only the starting time was is filled in the Timing field +func (at *ActionTiming) IsOneTimeRun() bool { + return len(at.Timing.Years) == 0 && + len(at.Timing.Months) == 0 && + len(at.Timing.MonthDays) == 0 && + len(at.Timing.WeekDays) == 0 && + len(at.Timing.StartTime) != 0 +} + // Structure to store actions according to weight type ActionTimingPriotityList []*ActionTiming diff --git a/timespans/actions_test.go b/timespans/actions_test.go index df2ee2d9c..b4e327cd4 100644 --- a/timespans/actions_test.go +++ b/timespans/actions_test.go @@ -348,13 +348,32 @@ func TestActionTimingFirstOfTheYear(t *testing.T) { } } -func TestActionTimingIsOneTimeRunNoInterval(t *testing.T) { +func TestActionTimingCheckForASAP(t *testing.T) { at := &ActionTiming{Timing: &Interval{StartTime: ASAP}} if !at.CheckForASAP() { t.Errorf("%v should be asap!", at) } } +func TestActionTimingIsOneTimeRun(t *testing.T) { + at := &ActionTiming{Timing: &Interval{StartTime: ASAP}} + if !at.CheckForASAP() { + t.Errorf("%v should be asap!", at) + } + if !at.IsOneTimeRun() { + t.Errorf("%v should be one time run!", at) + } +} + +func TestActionTimingOneTimeRun(t *testing.T) { + at := &ActionTiming{Timing: &Interval{StartTime: ASAP}} + at.CheckForASAP() + nextRun := at.GetNextStartTime() + if nextRun.IsZero() { + t.Error("next time failed for asap") + } +} + func TestActionTimingLogFunction(t *testing.T) { a := &Action{ ActionType: "LOG",