diff --git a/apier/v1/scheduler.go b/apier/v1/scheduler.go index ec02807a5..5fbcb7591 100644 --- a/apier/v1/scheduler.go +++ b/apier/v1/scheduler.go @@ -110,22 +110,11 @@ type ScheduledActions struct { } func (self *ApierV1) GetScheduledActions(attrs AttrsGetScheduledActions, reply *[]*ScheduledActions) error { - schedActions := make([]*ScheduledActions, 0) if self.Sched == nil { return errors.New("SCHEDULER_NOT_ENABLED") } + var schedActions []*ScheduledActions scheduledActions := self.Sched.GetQueue() - var min, max int - if attrs.Paginator.Offset != nil { - min = *attrs.Paginator.Offset - } - if attrs.Paginator.Limit != nil { - max = *attrs.Paginator.Limit - } - if max > len(scheduledActions) { - max = len(scheduledActions) - } - scheduledActions = scheduledActions[min : min+max] for _, qActions := range scheduledActions { sas := &ScheduledActions{ActionsId: qActions.ActionsId, ActionPlanId: qActions.Id, ActionPlanUuid: qActions.Uuid} if attrs.SearchTerm != "" && @@ -140,28 +129,40 @@ func (self *ApierV1) GetScheduledActions(attrs AttrsGetScheduledActions, reply * if !attrs.TimeEnd.IsZero() && (sas.NextRunTime.After(attrs.TimeEnd) || sas.NextRunTime.Equal(attrs.TimeEnd)) { continue } - acntFiltersMatch := false - for _, acntKey := range qActions.AccountIds { - tenantMatched := len(attrs.Tenant) == 0 - accountMatched := len(attrs.Account) == 0 - dta, _ := utils.NewTAFromAccountKey(acntKey) - sas.Accounts = append(sas.Accounts, dta) - // One member matching - if !tenantMatched && attrs.Tenant == dta.Tenant { - tenantMatched = true + // filter on account + if attrs.Tenant != "" || attrs.Account != "" { + found := false + for _, accID := range qActions.AccountIds { + split := strings.Split(accID, utils.CONCATENATED_KEY_SEP) + if len(split) != 2 { + continue // malformed account id + } + if attrs.Tenant != "" && attrs.Tenant != split[0] { + continue + } + if attrs.Account != "" && attrs.Account != split[1] { + continue + } + found = true + break } - if !accountMatched && attrs.Account == dta.Account { - accountMatched = true - } - if tenantMatched && accountMatched { - acntFiltersMatch = true + if !found { + continue } } - if !acntFiltersMatch { - continue - } + // we have a winner schedActions = append(schedActions, sas) } + if attrs.Paginator.Offset != nil { + if *attrs.Paginator.Offset <= len(schedActions) { + schedActions = schedActions[*attrs.Paginator.Offset:] + } + } + if attrs.Paginator.Limit != nil { + if *attrs.Paginator.Limit <= len(schedActions) { + schedActions = schedActions[:*attrs.Paginator.Limit] + } + } *reply = schedActions return nil } diff --git a/cmd/cgr-engine/rater.go b/cmd/cgr-engine/rater.go index f5a5a655f..b03594864 100644 --- a/cmd/cgr-engine/rater.go +++ b/cmd/cgr-engine/rater.go @@ -45,7 +45,7 @@ func startRater(internalRaterChan chan *engine.Responder, internalBalancerChan c server *utils.Server, ratingDb engine.RatingStorage, accountDb engine.AccountingStorage, loadDb engine.LoadStorage, cdrDb engine.CdrStorage, logDb engine.LogStorage, stopHandled *bool, exitChan chan bool) { - waitTasks := make([]chan struct{}, 0) + var waitTasks []chan struct{} //Cache load cacheTaskChan := make(chan struct{}) diff --git a/console/scheduler_queue.go b/console/scheduler_queue.go new file mode 100644 index 000000000..a409c3a68 --- /dev/null +++ b/console/scheduler_queue.go @@ -0,0 +1,63 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2012-2015 ITsysCOM + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package console + +import "github.com/cgrates/cgrates/apier/v1" + +func init() { + c := &CmdGetScheduledActions{ + name: "scheduler_queue", + rpcMethod: "ApierV1.GetScheduledActions", + rpcParams: &v1.AttrsGetScheduledActions{}, + } + commands[c.Name()] = c + c.CommandExecuter = &CommandExecuter{c} +} + +// Commander implementation +type CmdGetScheduledActions struct { + name string + rpcMethod string + rpcParams *v1.AttrsGetScheduledActions + *CommandExecuter +} + +func (self *CmdGetScheduledActions) Name() string { + return self.name +} + +func (self *CmdGetScheduledActions) RpcMethod() string { + return self.rpcMethod +} + +func (self *CmdGetScheduledActions) RpcParams(reset bool) interface{} { + if reset || self.rpcParams == nil { + self.rpcParams = &v1.AttrsGetScheduledActions{} + } + return self.rpcParams +} + +func (self *CmdGetScheduledActions) PostprocessRpcParams() error { + return nil +} + +func (self *CmdGetScheduledActions) RpcResult() interface{} { + s := v1.ScheduledActions{} + return &s +} diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index db9728bb0..dfae55c04 100644 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -66,7 +66,7 @@ func (s *Scheduler) Loop() { } else { s.Unlock() d := a0.GetNextStartTime(now).Sub(now) - //utils.Logger.Info(fmt.Sprintf("Timer set to wait for %v", d)) + utils.Logger.Info(fmt.Sprintf("Time to next action (%s): %v", a0.Id, d)) s.timer = time.NewTimer(d) select { case <-s.timer.C: @@ -84,27 +84,30 @@ func (s *Scheduler) LoadActionPlans(storage engine.RatingStorage) { if err != nil && err != utils.ErrNotFound { utils.Logger.Warning(fmt.Sprintf("Cannot get action plans: %v", err)) } + utils.Logger.Info(fmt.Sprintf(" processing %d action plans", len(actionPlans))) // recreate the queue s.Lock() s.queue = engine.ActionPlanPriotityList{} for key, aps := range actionPlans { toBeSaved := false isAsap := false - newApls := make([]*engine.ActionPlan, 0) // will remove the one time runs from the database + var newApls []*engine.ActionPlan // will remove the one time runs from the database for _, ap := range aps { if ap.Timing == nil { utils.Logger.Warning(fmt.Sprintf(" Nil timing on action plan: %+v, discarding!", ap)) continue } + if len(ap.AccountIds) == 0 { // no accounts just ignore + continue + } isAsap = ap.IsASAP() toBeSaved = toBeSaved || isAsap if isAsap { - if len(ap.AccountIds) > 0 { - utils.Logger.Info(fmt.Sprintf("Time for one time action on %v", key)) - } + utils.Logger.Info(fmt.Sprintf("Time for one time action on %v", key)) ap.Execute() ap.AccountIds = make([]string, 0) } else { + now := time.Now() if ap.GetNextStartTime(now).Before(now) { // the task is obsolete, do not add it to the queue @@ -124,6 +127,7 @@ func (s *Scheduler) LoadActionPlans(storage engine.RatingStorage) { } } sort.Sort(s.queue) + utils.Logger.Info(fmt.Sprintf(" queued %d action plans", len(s.queue))) s.Unlock() }