diff --git a/apier/v1/accounts.go b/apier/v1/accounts.go index aca99a981..aec37365d 100644 --- a/apier/v1/accounts.go +++ b/apier/v1/accounts.go @@ -64,7 +64,7 @@ func (self *ApierV1) GetAccountActionPlan(attrs AttrAcntAction, reply *[]*Accoun type AttrRemActionTiming struct { ActionPlanId string // Id identifying the ActionTimings profile ActionTimingId string // Internal CGR id identifying particular ActionTiming, *all for all user related ActionTimings to be canceled - Tenant string // Tenant he account belongs to + Tenant string // Tenant the account belongs to Account string // Account name ReloadScheduler bool // If set it will reload the scheduler after adding } @@ -90,6 +90,9 @@ func (self *ApierV1) RemActionTiming(attrs AttrRemActionTiming, reply *string) e if err := self.RatingDb.SetActionPlans(attrs.ActionPlanId, ats); err != nil { return 0, err } + if len(ats) > 0 { // update cache + self.RatingDb.CacheRatingPrefixValues(map[string][]string{utils.ACTION_PLAN_PREFIX: []string{attrs.ActionPlanId}}) + } return 0, nil }, 0, utils.ACTION_PLAN_PREFIX) if err != nil { diff --git a/apier/v1/apier_local_test.go b/apier/v1/apier_local_test.go index 6f76e83f4..3b804fbf0 100644 --- a/apier/v1/apier_local_test.go +++ b/apier/v1/apier_local_test.go @@ -1155,32 +1155,32 @@ func TestApierGetAccount(t *testing.T) { } var reply *engine.Account attrs := &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1001"} - if err := rater.Call("ApierV1.GetAccount", attrs, &reply); err != nil { + if err := rater.Call("ApierV2.GetAccount", attrs, &reply); err != nil { t.Error("Got error on ApierV1.GetAccount: ", err.Error()) } else if reply.BalanceMap[utils.MONETARY].GetTotalValue() != 11.5 { // We expect 11.5 since we have added in the previous test 1.5 t.Errorf("Calling ApierV1.GetBalance expected: 11.5, received: %f", reply.BalanceMap[utils.MONETARY].GetTotalValue()) } attrs = &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "dan"} - if err := rater.Call("ApierV1.GetAccount", attrs, &reply); err != nil { + if err := rater.Call("ApierV2.GetAccount", attrs, &reply); err != nil { t.Error("Got error on ApierV1.GetAccount: ", err.Error()) } else if reply.BalanceMap[utils.MONETARY].GetTotalValue() != 1.5 { t.Errorf("Calling ApierV1.GetAccount expected: 1.5, received: %f", reply.BalanceMap[utils.MONETARY].GetTotalValue()) } // The one we have topped up though executeAction attrs = &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "dan2"} - if err := rater.Call("ApierV1.GetAccount", attrs, &reply); err != nil { + if err := rater.Call("ApierV2.GetAccount", attrs, &reply); err != nil { t.Error("Got error on ApierV1.GetAccount: ", err.Error()) } else if reply.BalanceMap[utils.MONETARY].GetTotalValue() != 11.5 { t.Errorf("Calling ApierV1.GetAccount expected: 10, received: %f", reply.BalanceMap[utils.MONETARY].GetTotalValue()) } attrs = &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "dan3"} - if err := rater.Call("ApierV1.GetAccount", attrs, &reply); err != nil { + if err := rater.Call("ApierV2.GetAccount", attrs, &reply); err != nil { t.Error("Got error on ApierV1.GetAccount: ", err.Error()) } else if reply.BalanceMap[utils.MONETARY].GetTotalValue() != 3.6 { t.Errorf("Calling ApierV1.GetAccount expected: 3.6, received: %f", reply.BalanceMap[utils.MONETARY].GetTotalValue()) } attrs = &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "dan6"} - if err := rater.Call("ApierV1.GetAccount", attrs, &reply); err != nil { + if err := rater.Call("ApierV2.GetAccount", attrs, &reply); err != nil { t.Error("Got error on ApierV1.GetAccount: ", err.Error()) } else if reply.BalanceMap[utils.MONETARY].GetTotalValue() != 1 { t.Errorf("Calling ApierV1.GetAccount expected: 1, received: %f", reply.BalanceMap[utils.MONETARY].GetTotalValue()) @@ -1290,7 +1290,7 @@ func TestApierGetAccountAfterLoad(t *testing.T) { } var reply *engine.Account attrs := &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1001"} - if err := rater.Call("ApierV1.GetAccount", attrs, &reply); err != nil { + if err := rater.Call("ApierV2.GetAccount", attrs, &reply); err != nil { t.Error("Got error on ApierV1.GetAccount: ", err.Error()) } else if reply.BalanceMap[utils.MONETARY].GetTotalValue() != 11 { t.Errorf("Calling ApierV1.GetBalance expected: 11, received: %f", reply.BalanceMap[utils.MONETARY].GetTotalValue()) diff --git a/engine/action_plan.go b/engine/action_plan.go index 09a59da92..2a084f983 100644 --- a/engine/action_plan.go +++ b/engine/action_plan.go @@ -330,27 +330,27 @@ func (at *ActionPlan) String_DISABLED() string { } // Helper to remove ActionPlan members based on specific filters, empty data means no always match -func RemActionPlan(ats ActionPlans, actionTimingId, balanceId string) ActionPlans { +func RemActionPlan(ats ActionPlans, actionTimingId, accountId string) ActionPlans { for idx, at := range ats { if len(actionTimingId) != 0 && at.Uuid != actionTimingId { // No Match for ActionPlanId, no need to move further continue } - if len(balanceId) == 0 { // No account defined, considered match for complete removal + if len(accountId) == 0 { // No account defined, considered match for complete removal if len(ats) == 1 { // Removing last item, by init empty return make([]*ActionPlan, 0) } ats[idx], ats = ats[len(ats)-1], ats[:len(ats)-1] continue } - for iBlnc, blncId := range at.AccountIds { - if blncId == balanceId { + for iAcc, accId := range at.AccountIds { + if accId == accountId { if len(at.AccountIds) == 1 { // Only one balance, remove complete at if len(ats) == 1 { // Removing last item, by init empty return make([]*ActionPlan, 0) } ats[idx], ats = ats[len(ats)-1], ats[:len(ats)-1] } else { - at.AccountIds[iBlnc], at.AccountIds = at.AccountIds[len(at.AccountIds)-1], at.AccountIds[:len(at.AccountIds)-1] + at.AccountIds[iAcc], at.AccountIds = at.AccountIds[len(at.AccountIds)-1], at.AccountIds[:len(at.AccountIds)-1] } // only remove the first one matching break diff --git a/engine/storage_map.go b/engine/storage_map.go index 772cfa2ba..b83787650 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -699,6 +699,7 @@ func (ms *MapStorage) SetActionPlans(key string, ats ActionPlans) (err error) { if len(ats) == 0 { // delete the key delete(ms.dict, utils.ACTION_PLAN_PREFIX+key) + cache2go.RemKey(utils.ACTION_PLAN_PREFIX + key) return } result, err := ms.ms.Marshal(&ats) @@ -740,6 +741,11 @@ func (ms *MapStorage) GetDerivedChargers(key string, skipCache bool) (dcs utils. } func (ms *MapStorage) SetDerivedChargers(key string, dcs utils.DerivedChargers) error { + if len(dcs) == 0 { + delete(ms.dict, utils.DERIVEDCHARGERS_PREFIX+key) + cache2go.RemKey(utils.DERIVEDCHARGERS_PREFIX + key) + return nil + } result, err := ms.ms.Marshal(dcs) ms.dict[utils.DERIVEDCHARGERS_PREFIX+key] = result return err diff --git a/engine/storage_mongo.go b/engine/storage_mongo.go index dcaa9fa2c..6a30e3685 100644 --- a/engine/storage_mongo.go +++ b/engine/storage_mongo.go @@ -1077,6 +1077,14 @@ func (ms *MongoStorage) GetActionPlans(key string, skipCache bool) (ats ActionPl } func (ms *MongoStorage) SetActionPlans(key string, ats ActionPlans) error { + if len(ats) == 0 { + cache2go.RemKey(utils.ACTION_PLAN_PREFIX + key) + err := ms.db.C(colApl).Remove(bson.M{"key": key}) + if err != mgo.ErrNotFound { + return err + } + return nil + } _, err := ms.db.C(colApl).Upsert(bson.M{"key": key}, &struct { Key string Value ActionPlans @@ -1121,8 +1129,8 @@ func (ms *MongoStorage) GetDerivedChargers(key string, skipCache bool) (dcs util func (ms *MongoStorage) SetDerivedChargers(key string, dcs utils.DerivedChargers) (err error) { if len(dcs) == 0 { - err = ms.db.C(colDcs).Remove(bson.M{"key": key}) cache2go.RemKey(utils.DERIVEDCHARGERS_PREFIX + key) + err = ms.db.C(colDcs).Remove(bson.M{"key": key}) if err != mgo.ErrNotFound { return err } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index deb383538..5ad738564 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -873,6 +873,7 @@ func (rs *RedisStorage) SetActionPlans(key string, ats ActionPlans) (err error) if len(ats) == 0 { // delete the key _, err = rs.db.Del(utils.ACTION_PLAN_PREFIX + key) + cache2go.RemKey(utils.ACTION_PLAN_PREFIX + key) return err } result, err := rs.ms.Marshal(&ats) @@ -918,7 +919,7 @@ func (rs *RedisStorage) GetDerivedChargers(key string, skipCache bool) (dcs util func (rs *RedisStorage) SetDerivedChargers(key string, dcs utils.DerivedChargers) (err error) { if len(dcs) == 0 { _, err = rs.db.Del(utils.DERIVEDCHARGERS_PREFIX + key) - // FIXME: Does cache need cleanup too? + cache2go.RemKey(utils.DERIVEDCHARGERS_PREFIX + key) return err } marshaled, err := rs.ms.Marshal(dcs)