Skip to content

Commit

Permalink
rpc: adding new rpc command lnmetrics-cache
Browse files Browse the repository at this point in the history
Changelog-Added: adding new rpc command `lnmetrics-cache`

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Apr 1, 2022
1 parent e2cd456 commit 40b94fe
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
2 changes: 1 addition & 1 deletion internal/plugin/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type MetricOneRpcMethod struct {
plugin *MetricsPlugin `json:"-"`
}

func (rpc *MetricOneRpcMethod) Name() string {
func (instance *MetricOneRpcMethod) Name() string {
return "metric_one"
}

Expand Down
34 changes: 17 additions & 17 deletions internal/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,54 +72,54 @@ func (plugin *MetricsPlugin) RegisterMethods() error {
}

//nolint
func (instance *MetricsPlugin) callUpdateOnMetric(metric Metric, msg *Msg) {
if err := metric.UpdateWithMsg(msg, instance.Rpc); err != nil {
func (plugin *MetricsPlugin) callUpdateOnMetric(metric Metric, msg *Msg) {
if err := metric.UpdateWithMsg(msg, plugin.Rpc); err != nil {
log.GetInstance().Error(fmt.Sprintf("Error during update metrics event: %s", err))
}
}

// Call on stop operation on the node when the caller are shoutdown it self.
func (instance *MetricsPlugin) callOnStopOnMetrics(metric Metric, msg *Msg) {
err := metric.OnClose(msg, instance.Rpc)
func (plugin *MetricsPlugin) callOnStopOnMetrics(metric Metric, msg *Msg) {
err := metric.OnStop(msg, plugin.Rpc)
if err != nil {
log.GetInstance().Error(err)
}
}

// Update the metrics without any information received by the caller
func (instance *MetricsPlugin) callUpdateOnMetricNoMsg(metric Metric) {
func (plugin *MetricsPlugin) callUpdateOnMetricNoMsg(metric Metric) {
log.GetInstance().Debug("Calling Update on metrics")
err := metric.Update(instance.Rpc)
err := metric.Update(plugin.Rpc)
if err != nil {
log.GetInstance().Error(fmt.Sprintf("Error %s", err))
}
}

func (instance *MetricsPlugin) updateAndUploadMetric(metric Metric) {
func (plugin *MetricsPlugin) updateAndUploadMetric(metric Metric) {
log.GetInstance().Info("Calling update and upload metric")
instance.callUpdateOnMetricNoMsg(metric)
if err := metric.UploadOnRepo(instance.Server, instance.Rpc); err != nil {
plugin.callUpdateOnMetricNoMsg(metric)
if err := metric.UploadOnRepo(plugin.Server, plugin.Rpc); err != nil {
log.GetInstance().Error(fmt.Sprintf("Error %s", err))
}
}

// Register internal recurrent methods
func (instance *MetricsPlugin) RegisterRecurrentEvt(after string) {
func (plugin *MetricsPlugin) RegisterRecurrentEvt(after string) {
log.GetInstance().Info(fmt.Sprintf("Register recurrent event each %s", after))
instance.Cron = cron.New()
plugin.Cron = cron.New()
// FIXME: Discover what is the first value
_, err := instance.Cron.AddFunc(after, func() {
_, err := plugin.Cron.AddFunc(after, func() {
log.GetInstance().Info("Update and Uploading metrics")
for _, metric := range instance.Metrics {
go instance.updateAndUploadMetric(metric)
for _, metric := range plugin.Metrics {
go plugin.updateAndUploadMetric(metric)
}
})
if err != nil {
log.GetInstance().Error(fmt.Sprintf("Error during registering recurrent event: %s", err))
}
}

func (instance *MetricsPlugin) RegisterOneTimeEvt(after string) {
func (plugin *MetricsPlugin) RegisterOneTimeEvt(after string) {
log.GetInstance().Info(fmt.Sprintf("Register one time event after %s", after))
duration, err := time.ParseDuration(after)
if err != nil {
Expand All @@ -129,7 +129,7 @@ func (instance *MetricsPlugin) RegisterOneTimeEvt(after string) {
time.AfterFunc(duration, func() {
log.GetInstance().Debug("Calling on time function function")
// TODO: Should C-Lightning send a on init event like notification?
for _, metric := range instance.Metrics {
for _, metric := range plugin.Metrics {
go func(instance *MetricsPlugin, metric Metric) {
err := metric.OnInit(instance.Rpc)
if err != nil {
Expand All @@ -141,7 +141,7 @@ func (instance *MetricsPlugin) RegisterOneTimeEvt(after string) {
log.GetInstance().Error(fmt.Sprintf("Error: %s", err))
}

}(instance, metric)
}(plugin, metric)
}
})
}
46 changes: 41 additions & 5 deletions internal/plugin/plugin_rpc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package plugin

import (
"fmt"
"github.com/LNOpenMetrics/go-lnmetrics.reporter/internal/cache"
"github.com/LNOpenMetrics/lnmetrics.utils/log"
sysinfo "github.com/elastic/go-sysinfo"
"github.com/vincenzopalazzo/glightning/jrpc2"
)
Expand All @@ -23,16 +26,16 @@ type info struct {
ProxyEnabled bool
}

func (instance PluginRpcMethod) Name() string {
return "lnmetrics-info"
}

func NewPluginRpcMethod(pluginMetrics *MetricsPlugin) *PluginRpcMethod {
return &PluginRpcMethod{
metricsPlugin: pluginMetrics,
}
}

func (instance PluginRpcMethod) Name() string {
return "lnmetrics-info"
}

func (instance *PluginRpcMethod) New() interface{} {
return instance
}
Expand All @@ -45,7 +48,7 @@ func (instance *PluginRpcMethod) Call() (jrpc2.Result, error) {
goInfo := sysinfo.Go()
return info{
Name: "go-lnmetrics.reporter",
Version: "v0.0.4-rc7",
Version: "v0.0.5-rc1",
LangVersion: goInfo.Version,
Architecture: goInfo.Arch,
MaxProcs: goInfo.MaxProcs,
Expand All @@ -54,3 +57,36 @@ func (instance *PluginRpcMethod) Call() (jrpc2.Result, error) {
ProxyEnabled: instance.metricsPlugin.WithProxy,
}, nil
}

// CleanCacheRPC RPC call to clean up the plugin cache.
type CleanCacheRPC struct {
Cmd string
}

func NewCleanCacheRPC() *CleanCacheRPC {
return &CleanCacheRPC{}
}

func (instance *CleanCacheRPC) New() *CleanCacheRPC {
return instance
}

func (instance CleanCacheRPC) Name() string {
return "lnmetrics-cache"
}

func (instance *CleanCacheRPC) Call() (jrpc2.Result, error) {

if instance.Cmd != "clean" {
return nil, fmt.Errorf("command %s node found", instance.Cmd)
}

if err := cache.GetInstance().CleanCache(); err != nil {
log.GetInstance().Errorf("CleanCacheRPC rpc call return the following error during the %s cmd: %s", instance.Cmd, err)
return nil, fmt.Errorf("clean cache operation return the following error: %s", err)
}
response := struct {
Result string `json:"result"`
}{Result: "cleaning operation succeeded"}
return response, nil
}

0 comments on commit 40b94fe

Please sign in to comment.