diff --git a/packages/shutdown/order.go b/packages/shutdown/order.go index 199c0350179c1fb0723905572acb6c06c94c6183..9815548ba56b0b9a38cc39b858fae6c28692c995 100644 --- a/packages/shutdown/order.go +++ b/packages/shutdown/order.go @@ -1,7 +1,8 @@ package shutdown const ( - ShutdownPriorityTangle = iota + ShutdownPriorityDatabase = iota + ShutdownPriorityTangle ShutdownPriorityRemoteLog ShutdownPriorityAnalysis ShutdownPriorityMetrics diff --git a/pluginmgr/core/plugins.go b/pluginmgr/core/plugins.go index 6f4a510081dbf04160e1441f60b5522b706cba58..c7faaaf75c525471eb3df8a5847cba45c66aa455 100644 --- a/pluginmgr/core/plugins.go +++ b/pluginmgr/core/plugins.go @@ -5,6 +5,7 @@ import ( "github.com/iotaledger/goshimmer/plugins/banner" "github.com/iotaledger/goshimmer/plugins/cli" "github.com/iotaledger/goshimmer/plugins/config" + "github.com/iotaledger/goshimmer/plugins/database" "github.com/iotaledger/goshimmer/plugins/gossip" "github.com/iotaledger/goshimmer/plugins/gracefulshutdown" "github.com/iotaledger/goshimmer/plugins/logger" @@ -20,6 +21,7 @@ var PLUGINS = node.Plugins( logger.PLUGIN, cli.PLUGIN, portcheck.PLUGIN, + database.PLUGIN, autopeering.PLUGIN, tangle.PLUGIN, gossip.PLUGIN, diff --git a/plugins/database/plugin.go b/plugins/database/plugin.go new file mode 100644 index 0000000000000000000000000000000000000000..3762d8f0d5cb5772cc80fdf82fe69f248ec5e69a --- /dev/null +++ b/plugins/database/plugin.go @@ -0,0 +1,45 @@ +// database is a plugin that manages the badger database (e.g. garbage collection). +package database + +import ( + "time" + + "github.com/iotaledger/hive.go/timeutil" + + "github.com/iotaledger/goshimmer/packages/database" + "github.com/iotaledger/goshimmer/packages/shutdown" + + "github.com/iotaledger/hive.go/daemon" + "github.com/iotaledger/hive.go/logger" + "github.com/iotaledger/hive.go/node" +) + +const ( + PLUGIN_NAME = "Database" +) + +var ( + PLUGIN = node.NewPlugin(PLUGIN_NAME, node.Enabled, configure, run) + log *logger.Logger +) + +func configure(plugin *node.Plugin) { + log = logger.NewLogger(PLUGIN_NAME) + + _ = database.GetBadgerInstance() +} + +func run(plugin *node.Plugin) { + daemon.BackgroundWorker(PLUGIN_NAME+"_GC", func(shutdownSignal <-chan struct{}) { + timeutil.Ticker(func() { + database.CleanupBadgerInstance(log) + }, 5*time.Minute, shutdownSignal) + }, shutdown.ShutdownPriorityBadgerGarbageCollection) + + daemon.BackgroundWorker(PLUGIN_NAME, func(shutdownSignal <-chan struct{}) { + <-shutdownSignal + log.Infof("Syncing database to disk...") + database.GetBadgerInstance().Close() + log.Infof("Syncing database to disk... done") + }, shutdown.ShutdownPriorityDatabase) +}