From 25f74915e0fa1d6417cf6b1c9cf78b02c1f4761d Mon Sep 17 00:00:00 2001 From: DanB Date: Fri, 8 Dec 2017 17:08:26 +0100 Subject: [PATCH] APIerV2.SetAccount with better actions scheduling --- apier/v2/accounts.go | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/apier/v2/accounts.go b/apier/v2/accounts.go index db9504c02..4945e1009 100644 --- a/apier/v2/accounts.go +++ b/apier/v2/accounts.go @@ -105,6 +105,7 @@ func (self *ApierV2) SetAccount(attr AttrSetAccount, reply *string) error { accID := utils.AccountKey(attr.Tenant, attr.Account) dirtyActionPlans := make(map[string]*engine.ActionPlan) var ub *engine.Account + var schedNeedsReload bool _, err := guardian.Guardian.Guard(func() (interface{}, error) { if bal, _ := self.DataManager.DataDB().GetAccount(accID); bal != nil { ub = bal @@ -137,20 +138,12 @@ func (self *ApierV2) SetAccount(attr AttrSetAccount, reply *string) error { } } for _, apID := range *attr.ActionPlanIDs { - if utils.IsSliceMember(acntAPids, apID) && !attr.ActionPlansOverwrite { - continue // Already there - } ap, err := self.DataManager.DataDB().GetActionPlan(apID, false, utils.NonTransactional) if err != nil { return 0, err } - if ap.AccountIDs == nil { - ap.AccountIDs = make(utils.StringMap) - } - ap.AccountIDs[accID] = true - dirtyActionPlans[apID] = ap - acntAPids = append(acntAPids, apID) // create tasks + var schedTasks int // keep count on the number of scheduled tasks so we can compare with actions needed for _, at := range ap.ActionTimings { if at.IsASAP() { t := &engine.Task{ @@ -161,8 +154,25 @@ func (self *ApierV2) SetAccount(attr AttrSetAccount, reply *string) error { if err = self.DataManager.DataDB().PushTask(t); err != nil { return 0, err } + schedTasks++ } } + if schedTasks != 0 && !schedNeedsReload { + schedNeedsReload = true + } + if schedTasks == len(ap.ActionTimings) || // scheduled all actions, no need to add account to AP + utils.IsSliceMember(acntAPids, apID) { + continue // No need to reschedule since already there + } + if ap.AccountIDs == nil { + ap.AccountIDs = make(utils.StringMap) + } + ap.AccountIDs[accID] = true + dirtyActionPlans[apID] = ap + acntAPids = append(acntAPids, apID) + } + if len(dirtyActionPlans) != 0 && !schedNeedsReload { + schedNeedsReload = true } apIDs := make([]string, len(dirtyActionPlans)) i := 0 @@ -212,6 +222,7 @@ func (self *ApierV2) SetAccount(attr AttrSetAccount, reply *string) error { } } } + ub.InitCounters() if attr.AllowNegative != nil { ub.AllowNegative = *attr.AllowNegative @@ -228,7 +239,7 @@ func (self *ApierV2) SetAccount(attr AttrSetAccount, reply *string) error { if err != nil { return utils.NewErrServerError(err) } - if attr.ReloadScheduler && len(dirtyActionPlans) != 0 { + if attr.ReloadScheduler && schedNeedsReload { sched := self.ServManager.GetScheduler() if sched == nil { return errors.New(utils.SchedulerNotRunningCaps)