mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-16 21:59:53 +05:00
even better one time run
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user