diff --git a/engine/action_plan.go b/engine/action_plan.go index 74b8278d0..689cafc5a 100644 --- a/engine/action_plan.go +++ b/engine/action_plan.go @@ -296,6 +296,9 @@ func (at *ActionPlan) Execute() (err error) { } func (at *ActionPlan) IsASAP() bool { + if at.Timing == nil { + return false + } return at.Timing.Timing.StartTime == utils.ASAP } diff --git a/engine/tp_reader.go b/engine/tp_reader.go index 53ea336e0..ade32bdba 100644 --- a/engine/tp_reader.go +++ b/engine/tp_reader.go @@ -571,7 +571,7 @@ func (tpr *TpReader) LoadActionPlans() (err error) { if !exists { return fmt.Errorf("[ActionPlans] Could not load the timing for tag: %v", at.TimingId) } - actTmg := &ActionPlan{ + actPln := &ActionPlan{ Uuid: utils.GenUUID(), Id: atId, Weight: at.Weight, @@ -586,7 +586,7 @@ func (tpr *TpReader) LoadActionPlans() (err error) { }, ActionsId: at.ActionsId, } - tpr.actionPlans[atId] = append(tpr.actionPlans[atId], actTmg) + tpr.actionPlans[atId] = append(tpr.actionPlans[atId], actPln) } } @@ -694,7 +694,7 @@ func (tpr *TpReader) LoadAccountActionsFiltered(qriedAA *TpAccountAction) error } else { t = tpr.timings[at.TimingId] // *asap } - actTmg := &ActionPlan{ + actPln := &ActionPlan{ Uuid: utils.GenUUID(), Id: accountAction.ActionPlanId, Weight: at.Weight, @@ -709,7 +709,7 @@ func (tpr *TpReader) LoadAccountActionsFiltered(qriedAA *TpAccountAction) error ActionsId: at.ActionsId, } // collect action ids from timings - actionsIds = append(actionsIds, actTmg.ActionsId) + actionsIds = append(actionsIds, actPln.ActionsId) //add user balance id if no already in found := false for _, ubId := range exitingAccountIds { @@ -719,9 +719,9 @@ func (tpr *TpReader) LoadAccountActionsFiltered(qriedAA *TpAccountAction) error } } if !found { - actTmg.AccountIds = append(exitingAccountIds, id) + actPln.AccountIds = append(exitingAccountIds, id) } - actionTimings = append(actionTimings, actTmg) + actionTimings = append(actionTimings, actPln) } // write action triggers @@ -1503,7 +1503,7 @@ func (tpr *TpReader) GetLoadedIds(categ string) ([]string, error) { i++ } return keys, nil - case utils.ACTION_PREFIX: // actionPlans + case utils.ACTION_PREFIX: keys := make([]string, len(tpr.actions)) i := 0 for k := range tpr.actions { diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index f9395d6a3..27b804693 100644 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -20,6 +20,7 @@ package scheduler import ( "fmt" + "log" "sort" "sync" "time" @@ -87,32 +88,35 @@ func (s *Scheduler) LoadActionPlans(storage engine.RatingStorage) { // recreate the queue s.Lock() s.queue = engine.ActionPlanPriotityList{} - for key, ats := range actionPlans { + for key, aps := range actionPlans { toBeSaved := false isAsap := false newApls := make([]*engine.ActionPlan, 0) // will remove the one time runs from the database - for _, at := range ats { - isAsap = at.IsASAP() + for _, ap := range aps { + log.Printf("AP %+v", ap) + isAsap = ap.IsASAP() toBeSaved = toBeSaved || isAsap if isAsap { - if len(at.AccountIds) > 0 { + if len(ap.AccountIds) > 0 { engine.Logger.Info(fmt.Sprintf("Time for one time action on %v", key)) } - at.Execute() - at.AccountIds = make([]string, 0) - // do not append it to the newApls list to be saved + ap.Execute() + ap.AccountIds = make([]string, 0) } else { now := time.Now() - if at.GetNextStartTime(now).Before(now) { + if ap.GetNextStartTime(now).Before(now) { // the task is obsolete, do not add it to the queue continue } - s.queue = append(s.queue, at) + s.queue = append(s.queue, ap) } // save even asap action plans with empty account id list - newApls = append(newApls, at) + newApls = append(newApls, ap) } if toBeSaved { + for _, ap := range newApls { + log.Printf("NewAP: %+v", ap) + } engine.Guardian.Guard(func() (interface{}, error) { storage.SetActionPlans(key, newApls) return 0, nil