mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-20 22:58:44 +05:00
fix for removing account from action plan
This commit is contained in:
@@ -559,20 +559,28 @@ func removeAccountAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Ac
|
||||
utils.Logger.Err(fmt.Sprintf("Could not get action plans: %s: %v", accID, err))
|
||||
return err
|
||||
}
|
||||
for key, ap := range allAPs {
|
||||
if _, exists := ap.AccountIDs[accID]; !exists {
|
||||
_, err := Guardian.Guard(func() (interface{}, error) {
|
||||
var dirtyAps []string
|
||||
_, err = Guardian.Guard(func() (interface{}, error) {
|
||||
for key, ap := range allAPs {
|
||||
if _, exists := ap.AccountIDs[accID]; !exists {
|
||||
// save action plan
|
||||
ratingStorage.SetActionPlan(key, ap)
|
||||
// cache
|
||||
ratingStorage.CacheRatingPrefixValues(map[string][]string{utils.ACTION_PLAN_PREFIX: []string{utils.ACTION_PLAN_PREFIX + key}})
|
||||
return 0, nil
|
||||
}, 0, utils.ACTION_PLAN_PREFIX)
|
||||
if err != nil {
|
||||
return err
|
||||
delete(ap.AccountIDs, key)
|
||||
ratingStorage.SetActionPlan(key, ap, true)
|
||||
dirtyAps = append(dirtyAps, utils.ACTION_PLAN_PREFIX+key)
|
||||
}
|
||||
}
|
||||
if len(dirtyAps) > 0 {
|
||||
// cache
|
||||
ratingStorage.CacheRatingPrefixValues(map[string][]string{
|
||||
utils.ACTION_PLAN_PREFIX: dirtyAps})
|
||||
}
|
||||
return 0, nil
|
||||
|
||||
}, 0, utils.ACTION_PLAN_PREFIX)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO: no scheduler reload?
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ type RatingStorage interface {
|
||||
GetActionTriggers(string) (ActionTriggers, error)
|
||||
SetActionTriggers(string, ActionTriggers) error
|
||||
GetActionPlan(string, bool) (*ActionPlan, error)
|
||||
SetActionPlan(string, *ActionPlan) error
|
||||
SetActionPlan(string, *ActionPlan, bool) error
|
||||
GetAllActionPlans() (map[string]*ActionPlan, error)
|
||||
PushTask(*Task) error
|
||||
PopTask() (*Task, error)
|
||||
|
||||
@@ -664,20 +664,22 @@ func (ms *MapStorage) GetActionPlan(key string, skipCache bool) (ats *ActionPlan
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MapStorage) SetActionPlan(key string, ats *ActionPlan) (err error) {
|
||||
func (ms *MapStorage) SetActionPlan(key string, ats *ActionPlan, overwrite bool) (err error) {
|
||||
if len(ats.ActionTimings) == 0 {
|
||||
// delete the key
|
||||
delete(ms.dict, utils.ACTION_PLAN_PREFIX+key)
|
||||
cache2go.RemKey(utils.ACTION_PLAN_PREFIX + key)
|
||||
return
|
||||
}
|
||||
// get existing action plan to merge the account ids
|
||||
if existingAts, _ := ms.GetActionPlan(key, true); existingAts != nil {
|
||||
if ats.AccountIDs == nil && len(existingAts.AccountIDs) > 0 {
|
||||
ats.AccountIDs = make(utils.StringMap)
|
||||
}
|
||||
for accID := range existingAts.AccountIDs {
|
||||
ats.AccountIDs[accID] = true
|
||||
if !overwrite {
|
||||
// get existing action plan to merge the account ids
|
||||
if existingAts, _ := ms.GetActionPlan(key, true); existingAts != nil {
|
||||
if ats.AccountIDs == nil && len(existingAts.AccountIDs) > 0 {
|
||||
ats.AccountIDs = make(utils.StringMap)
|
||||
}
|
||||
for accID := range existingAts.AccountIDs {
|
||||
ats.AccountIDs[accID] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
result, err := ms.ms.Marshal(&ats)
|
||||
|
||||
@@ -1156,7 +1156,7 @@ func (ms *MongoStorage) GetActionPlan(key string, skipCache bool) (ats *ActionPl
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) SetActionPlan(key string, ats *ActionPlan) error {
|
||||
func (ms *MongoStorage) SetActionPlan(key string, ats *ActionPlan, overwrite bool) error {
|
||||
// clean dots from account ids map
|
||||
if len(ats.ActionTimings) == 0 {
|
||||
cache2go.RemKey(utils.ACTION_PLAN_PREFIX + key)
|
||||
@@ -1166,13 +1166,15 @@ func (ms *MongoStorage) SetActionPlan(key string, ats *ActionPlan) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// get existing action plan to merge the account ids
|
||||
if existingAts, _ := ms.GetActionPlan(key, true); existingAts != nil {
|
||||
if ats.AccountIDs == nil && len(existingAts.AccountIDs) > 0 {
|
||||
ats.AccountIDs = make(utils.StringMap)
|
||||
}
|
||||
for accID := range existingAts.AccountIDs {
|
||||
ats.AccountIDs[accID] = true
|
||||
if !overwrite {
|
||||
// get existing action plan to merge the account ids
|
||||
if existingAts, _ := ms.GetActionPlan(key, true); existingAts != nil {
|
||||
if ats.AccountIDs == nil && len(existingAts.AccountIDs) > 0 {
|
||||
ats.AccountIDs = make(utils.StringMap)
|
||||
}
|
||||
for accID := range existingAts.AccountIDs {
|
||||
ats.AccountIDs[accID] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
result, err := ms.ms.Marshal(ats)
|
||||
|
||||
@@ -920,20 +920,22 @@ func (rs *RedisStorage) GetActionPlan(key string, skipCache bool) (ats *ActionPl
|
||||
return
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) SetActionPlan(key string, ats *ActionPlan) (err error) {
|
||||
func (rs *RedisStorage) SetActionPlan(key string, ats *ActionPlan, overwrite bool) (err error) {
|
||||
if len(ats.ActionTimings) == 0 {
|
||||
// delete the key
|
||||
err = rs.db.Cmd("DEL", utils.ACTION_PLAN_PREFIX+key).Err
|
||||
cache2go.RemKey(utils.ACTION_PLAN_PREFIX + key)
|
||||
return err
|
||||
}
|
||||
// get existing action plan to merge the account ids
|
||||
if existingAts, _ := rs.GetActionPlan(key, true); existingAts != nil {
|
||||
if ats.AccountIDs == nil && len(existingAts.AccountIDs) > 0 {
|
||||
ats.AccountIDs = make(utils.StringMap)
|
||||
}
|
||||
for accID := range existingAts.AccountIDs {
|
||||
ats.AccountIDs[accID] = true
|
||||
if !overwrite {
|
||||
// get existing action plan to merge the account ids
|
||||
if existingAts, _ := rs.GetActionPlan(key, true); existingAts != nil {
|
||||
if ats.AccountIDs == nil && len(existingAts.AccountIDs) > 0 {
|
||||
ats.AccountIDs = make(utils.StringMap)
|
||||
}
|
||||
for accID := range existingAts.AccountIDs {
|
||||
ats.AccountIDs[accID] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -760,7 +760,7 @@ func (tpr *TpReader) LoadAccountActionsFiltered(qriedAA *TpAccountAction) error
|
||||
}
|
||||
}
|
||||
// write action plan
|
||||
err = tpr.ratingStorage.SetActionPlan(accountAction.ActionPlanId, actionPlan)
|
||||
err = tpr.ratingStorage.SetActionPlan(accountAction.ActionPlanId, actionPlan, false)
|
||||
if err != nil {
|
||||
return errors.New(err.Error() + " (SetActionPlan): " + accountAction.ActionPlanId)
|
||||
}
|
||||
@@ -1399,7 +1399,7 @@ func (tpr *TpReader) WriteToDatabase(flush, verbose bool) (err error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
err = tpr.ratingStorage.SetActionPlan(k, ap)
|
||||
err = tpr.ratingStorage.SetActionPlan(k, ap, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user