diff --git a/apier/v1/dispatcher.go b/apier/v1/dispatcher.go index 3b72672e3..fde3857a1 100755 --- a/apier/v1/dispatcher.go +++ b/apier/v1/dispatcher.go @@ -638,3 +638,22 @@ func (dS *DispatcherGuardianSv1) RemoteUnlock(attr *dispatchers.AttrRemoteUnlock func (dS *DispatcherGuardianSv1) Ping(args *dispatchers.CGREvWithApiKey, reply *string) error { return dS.dS.GuardianSv1Ping(args, reply) } + +func NewDispatcherSchedulerSv1(dps *dispatchers.DispatcherService) *DispatcherSchedulerSv1 { + return &DispatcherSchedulerSv1{dS: dps} +} + +// Exports RPC from SchedulerSv1 +type DispatcherSchedulerSv1 struct { + dS *dispatchers.DispatcherService +} + +// Reload reloads scheduler instructions +func (dS *DispatcherSchedulerSv1) Reload(attr *dispatchers.StringkWithApiKey, reply *string) (err error) { + return dS.dS.SchedulerSv1Reload(attr, reply) +} + +// Ping used to detreminate if component is active +func (dS *DispatcherSchedulerSv1) Ping(args *dispatchers.CGREvWithApiKey, reply *string) error { + return dS.dS.SchedulerSv1Ping(args, reply) +} diff --git a/apier/v1/schedulers.go b/apier/v1/schedulers.go index c4a8215ce..e1eddbbc2 100644 --- a/apier/v1/schedulers.go +++ b/apier/v1/schedulers.go @@ -18,7 +18,10 @@ along with this program. If not, see package v1 -import "github.com/cgrates/cgrates/servmanager" +import ( + "github.com/cgrates/cgrates/servmanager" + "github.com/cgrates/cgrates/utils" +) func NewSchedulerSv1(schdS *servmanager.SchedulerS) *SchedulerSv1 { return &SchedulerSv1{schdS: schdS} @@ -33,3 +36,8 @@ type SchedulerSv1 struct { func (schdSv1 *SchedulerSv1) Reload(arg string, reply *string) error { return schdSv1.schdS.V1Reload(arg, reply) } + +func (schdSv1 *SchedulerSv1) Ping(ign *utils.CGREvent, reply *string) error { + *reply = utils.Pong + return nil +} diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index 6f89e95bb..471de43bc 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -1007,6 +1007,9 @@ func startDispatcherService(internalDispatcherSChan chan *dispatchers.Dispatcher server.RpcRegisterName(utils.GuardianSv1, v1.NewDispatcherGuardianSv1(dspS)) + server.RpcRegisterName(utils.SchedulerSv1, + v1.NewDispatcherSchedulerSv1(dspS)) + internalDispatcherSChan <- dspS } diff --git a/console/ping.go b/console/ping.go index fcd88fa1e..b0b941b3e 100644 --- a/console/ping.go +++ b/console/ping.go @@ -70,6 +70,8 @@ func (self *CmdApierPing) RpcMethod() string { return utils.DispatcherSv1Ping case utils.AnalyzerSLow: return utils.AnalyzerSv1Ping + case utils.SchedulerSLow: + return utils.SchedulerSv1Ping default: } return self.rpcMethod diff --git a/data/tariffplans/dispatchers/Attributes.csv b/data/tariffplans/dispatchers/Attributes.csv index ffde8ae0a..49c77a5ab 100644 --- a/data/tariffplans/dispatchers/Attributes.csv +++ b/data/tariffplans/dispatchers/Attributes.csv @@ -11,3 +11,4 @@ cgrates.org,ATTR_API_SES_AUTH,*auth,*string:~APIKey:ses12345,,,APIMethods,Sessio cgrates.org,ATTR_API_RSP_AUTH,*auth,*string:~APIKey:rsp12345,,,APIMethods,Responder.Status&Responder.GetTimeout&Responder.Shutdown&Responder.Ping,false,20 cgrates.org,ATTR_API_CHC_AUTH,*auth,*string:~APIKey:chc12345,,,APIMethods,CacheSv1.Ping&CacheSv1.GetCacheStats&CacheSv1.LoadCache&CacheSv1.PrecacheStatus&CacheSv1.GetItemIDs&CacheSv1.HasItem&CacheSv1.GetItemExpiryTime&CacheSv1.ReloadCache&CacheSv1.RemoveItem&CacheSv1.FlushCache&CacheSv1.Clear,false,20 cgrates.org,ATTR_API_GRD_AUTH,*auth,*string:~APIKey:grd12345,,,APIMethods,GuardianSv1.Ping&GuardianSv1.RemoteLock&GuardianSv1.RemoteUnlock,false,20 +cgrates.org,ATTR_API_SCHD_AUTH,*auth,*string:~APIKey:sched12345,,,APIMethods,SchedulerSv1.Ping,false,20 diff --git a/dispatchers/scheduler.go b/dispatchers/scheduler.go new file mode 100644 index 000000000..3129830e4 --- /dev/null +++ b/dispatchers/scheduler.go @@ -0,0 +1,49 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +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 dispatchers + +import ( + "time" + + "github.com/cgrates/cgrates/utils" +) + +func (dS *DispatcherService) SchedulerSv1Ping(args *CGREvWithApiKey, reply *string) (err error) { + if dS.attrS != nil { + if err = dS.authorize(utils.SchedulerSv1Ping, + args.CGREvent.Tenant, + args.APIKey, args.CGREvent.Time); err != nil { + return + } + } + return dS.Dispatch(&args.CGREvent, utils.MetaScheduler, args.RouteID, + utils.SchedulerSv1Ping, args.CGREvent, reply) +} + +func (dS *DispatcherService) SchedulerSv1Reload(args *StringkWithApiKey, reply *string) (err error) { + if dS.attrS != nil { + if err = dS.authorize(utils.SchedulerSv1Ping, + args.TenantArg.Tenant, + args.APIKey, utils.TimePointer(time.Now())); err != nil { + return + } + } + return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaScheduler, + args.RouteID, utils.SchedulerSv1Reload, args.Arg, reply) +} diff --git a/dispatchers/scheduler_it_test.go b/dispatchers/scheduler_it_test.go new file mode 100644 index 000000000..7e3990df8 --- /dev/null +++ b/dispatchers/scheduler_it_test.go @@ -0,0 +1,57 @@ +// +build integration + +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +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 dispatchers + +import ( + "testing" + + "github.com/cgrates/cgrates/utils" +) + +var sTestsDspSched = []func(t *testing.T){ + testDspSchedPing, +} + +//Test start here +func TestDspSchedulerSTMySQL(t *testing.T) { + testDsp(t, sTestsDspSched, "TestDspSchedulerSTMySQL", "all", "all2", "attributes", "dispatchers", "tutorial", "oldtutorial", "dispatchers") +} + +func testDspSchedPing(t *testing.T) { + var reply string + if err := allEngine.RCP.Call(utils.SchedulerSv1Ping, new(utils.CGREvent), &reply); err != nil { + t.Error(err) + } else if reply != utils.Pong { + t.Errorf("Received: %s", reply) + } + if err := dispEngine.RCP.Call(utils.SchedulerSv1Ping, &CGREvWithApiKey{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + }, + DispatcherResource: DispatcherResource{ + APIKey: "sched12345", + }, + }, &reply); err != nil { + t.Error(err) + } else if reply != utils.Pong { + t.Errorf("Received: %s", reply) + } +} diff --git a/dispatchers/utils.go b/dispatchers/utils.go index 0eb633461..afb0f3d0e 100755 --- a/dispatchers/utils.go +++ b/dispatchers/utils.go @@ -167,6 +167,12 @@ type AttrRemoteUnlockWithApiKey struct { RefID string } +type StringkWithApiKey struct { + DispatcherResource + utils.TenantArg + Arg string +} + func ParseStringMap(s string) utils.StringMap { if s == utils.ZERO { return make(utils.StringMap) diff --git a/utils/consts.go b/utils/consts.go index 08ba4bcc5..e60bef1ce 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -611,6 +611,7 @@ const ( ThresholdsLow = "thresholds" DispatcherSLow = "dispatchers" AnalyzerSLow = "analyzers" + SchedulerSLow = "schedulers" LoaderSLow = "loaders" ) @@ -863,7 +864,8 @@ const ( // Scheduler const ( - SchedulerPing = "Scheduler.Ping" + SchedulerSv1 = "SchedulerSv1" + SchedulerSv1Ping = "SchedulerSv1.Ping" SchedulerSv1Reload = "SchedulerSv1.Reload" )