Skip to content

Commit

Permalink
Add caching_delay flag to cgr-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
arberkatellari authored and danbogos committed Jan 6, 2024
1 parent a536148 commit d5d873b
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 4 deletions.
15 changes: 14 additions & 1 deletion cmd/cgr-loader/cgr-loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"log"
"os"
"strings"
"time"

"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
Expand Down Expand Up @@ -96,7 +97,8 @@ var (

cachingArg = cgrLoaderFlags.String(utils.CachingArgCgr, utils.EmptyString,
"Caching strategy used when loading TP")
tpid = cgrLoaderFlags.String(utils.TpIDCfg, dfltCfg.LoaderCgrCfg().TpID,
cachingDlay = cgrLoaderFlags.Duration(utils.CachingDlayCfg, 0, "Adds delay before cache reload")
tpid = cgrLoaderFlags.String(utils.TpIDCfg, dfltCfg.LoaderCgrCfg().TpID,
"The tariff plan ID from the database")
dataPath = cgrLoaderFlags.String(utils.PathCfg, dfltCfg.LoaderCgrCfg().DataPath,
"The path to folder containing the data files")
Expand Down Expand Up @@ -294,6 +296,9 @@ func loadConfig() (ldrCfg *config.CGRConfig) {
if *cachingArg != utils.EmptyString {
ldrCfg.GeneralCfg().DefaultCaching = *cachingArg
}
if *cachingDlay != 0 {
ldrCfg.GeneralCfg().CachingDelay = *cachingDlay
}
return
}

Expand Down Expand Up @@ -409,6 +414,14 @@ func main() {
}
}

// delay if needed before cache reload
if ldrCfg.GeneralCfg().CachingDelay != 0 {
if *verbose {
log.Printf("Delaying %v", ldrCfg.GeneralCfg().CachingDelay)
}
time.Sleep(ldrCfg.GeneralCfg().CachingDelay)
}

// reload cache
if err = tpReader.ReloadCache(ldrCfg.GeneralCfg().DefaultCaching, *verbose, map[string]any{
utils.OptsAPIKey: *apiKey,
Expand Down
1 change: 1 addition & 0 deletions config/config_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const CGRATES_CFG_JSON = `
"default_tenant": "cgrates.org", // default tenant to consider when missing from requests
"default_timezone": "Local", // default timezone for timestamps where not specified <""|UTC|Local|$IANA_TZ_DB>
"default_caching": "*reload", // default actions to do when caching items
"caching_delay": "0s", // add delay before cache reload
"min_call_duration": "0s", // only authorize calls with allowed duration higher than this
"max_call_duration": "3h", // maximum call duration a prepaid call can last
"connect_attempts": 5, // initial server connect attempts
Expand Down
1 change: 1 addition & 0 deletions config/config_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestDfGeneralJsonCfg(t *testing.T) {
Default_category: utils.StringPointer("call"),
Default_tenant: utils.StringPointer("cgrates.org"),
Default_caching: utils.StringPointer(utils.MetaReload),
Caching_delay: utils.StringPointer("0s"),
Default_timezone: utils.StringPointer("Local"),
Connect_attempts: utils.IntPointer(5),
Reconnects: utils.IntPointer(-1),
Expand Down
7 changes: 4 additions & 3 deletions config/config_test.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions config/generalcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type GeneralCfg struct {
DefaultTenant string // set default tenant
DefaultTimezone string // default timezone for timestamps where not specified <""|UTC|Local|$IANA_TZ_DB>
DefaultCaching string
CachingDelay time.Duration // use to add delay before cache reload
ConnectAttempts int // number of initial connection attempts before giving up
Reconnects int // number of reconnect attempts in case of connection lost <-1 for infinite | nb>
MaxReconnectInterval time.Duration // time to wait in between reconnect attempts
Expand Down Expand Up @@ -124,6 +125,11 @@ func (gencfg *GeneralCfg) loadFromJSONCfg(jsnGeneralCfg *GeneralJsonCfg) (err er
if jsnGeneralCfg.Default_caching != nil {
gencfg.DefaultCaching = *jsnGeneralCfg.Default_caching
}
if jsnGeneralCfg.Caching_delay != nil {
if gencfg.CachingDelay, err = utils.ParseDurationWithNanosecs(*jsnGeneralCfg.Caching_delay); err != nil {
return err
}
}
if jsnGeneralCfg.Locking_timeout != nil {
if gencfg.LockingTimeout, err = utils.ParseDurationWithNanosecs(*jsnGeneralCfg.Locking_timeout); err != nil {
return err
Expand Down Expand Up @@ -161,6 +167,7 @@ func (gencfg *GeneralCfg) AsMapInterface() (initialMP map[string]any) {
utils.DefaultTenantCfg: gencfg.DefaultTenant,
utils.DefaultTimezoneCfg: gencfg.DefaultTimezone,
utils.DefaultCachingCfg: gencfg.DefaultCaching,
utils.CachingDlayCfg: gencfg.CachingDelay,
utils.ConnectAttemptsCfg: gencfg.ConnectAttempts,
utils.ReconnectsCfg: gencfg.Reconnects,
utils.MaxReconnectIntervalCfg: "0",
Expand Down
2 changes: 2 additions & 0 deletions config/generalcfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func TestGeneralCfgAsMapInterface(t *testing.T) {
utils.DefaultTenantCfg: "cgrates.org",
utils.DefaultTimezoneCfg: "Local",
utils.DefaultCachingCfg: "*reload",
utils.CachingDlayCfg: 0 * time.Second,
utils.ConnectAttemptsCfg: 5,
utils.ReconnectsCfg: -1,
utils.MaxReconnectIntervalCfg: "1s",
Expand Down Expand Up @@ -209,6 +210,7 @@ func TestGeneralCfgAsMapInterface1(t *testing.T) {
utils.DefaultTenantCfg: "cgrates.org",
utils.DefaultTimezoneCfg: "Local",
utils.DefaultCachingCfg: "*reload",
utils.CachingDlayCfg: 0 * time.Second,
utils.ConnectAttemptsCfg: 5,
utils.ReconnectsCfg: -1,
utils.MaxReconnectIntervalCfg: "0",
Expand Down
1 change: 1 addition & 0 deletions config/libconfig_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type GeneralJsonCfg struct {
Default_tenant *string
Default_timezone *string
Default_caching *string
Caching_delay *string
Connect_attempts *int
Reconnects *int
Max_reconnect_interval *string
Expand Down
1 change: 1 addition & 0 deletions utils/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -1978,6 +1978,7 @@ const (
DefaultTenantCfg = "default_tenant"
DefaultTimezoneCfg = "default_timezone"
DefaultCachingCfg = "default_caching"
CachingDlayCfg = "caching_delay"
ConnectAttemptsCfg = "connect_attempts"
ReconnectsCfg = "reconnects"
MaxReconnectIntervalCfg = "max_reconnect_interval"
Expand Down

0 comments on commit d5d873b

Please sign in to comment.