From 31db60b89a76d67ba7e74196b5d21128fc1f3ec1 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Tue, 26 Nov 2024 01:54:47 -0600 Subject: [PATCH] Enable config caching default 5 minutes (when testing set CONFIG_CACHE_TTL higher so issues are obvious) lnms config:clear now clears both Laravel and LibreNMS config caches. LibreNMS config cache will automatically regenerate on next application load. --- app/ConfigRepository.php | 16 +++++++++++-- app/Console/Commands/ClearConfigCommand.php | 26 +++++++++++++++++++++ app/Console/Commands/DevicePoll.php | 3 +++ config/librenms.php | 2 +- discovery.php | 2 ++ lang/en/commands.php | 3 +++ scripts/collect-snmp-data.php | 2 ++ scripts/save-test-data.php | 2 ++ tests/bootstrap.php | 2 ++ 9 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 app/Console/Commands/ClearConfigCommand.php diff --git a/app/ConfigRepository.php b/app/ConfigRepository.php index 73f35ff2514e..41c528be3c14 100644 --- a/app/ConfigRepository.php +++ b/app/ConfigRepository.php @@ -239,6 +239,8 @@ public function persist($key, $value): bool // delete any children (there should not be any unless it is legacy) \App\Models\Config::query()->where('config_name', 'like', "$key.%")->delete(); + $this->invalidateCache(); // config has been changed, it will need to be reloaded + return true; } catch (Exception $e) { if (class_exists(Log::class)) { @@ -316,6 +318,16 @@ public function getAll(): array return $this->config; } + /** + * Invalidate config cache (but don't reload) + * Next time the config is loaded, it will loaded fresh + * Because this is currently hardcoded to file cache, it will only clear the cache on this node + */ + public function invalidateCache(): void + { + Cache::driver('file')->forget('librenms-config'); + } + /** * merge the database config with the global config, * global config overrides db @@ -456,8 +468,8 @@ private function loadPostUserConfigDefaults(): void if (! $this->has('snmp.unescape')) { $this->persist('snmp.unescape', version_compare((new Version($this))->netSnmp(), '5.8.0', '<')); } - if (! self::has('reporting.usage')) { - self::persist('reporting.usage', (bool) Callback::get('enabled')); + if (! $this->has('reporting.usage')) { + $this->persist('reporting.usage', (bool) Callback::get('enabled')); } // populate legacy DB credentials, just in case something external uses them. Maybe remove this later diff --git a/app/Console/Commands/ClearConfigCommand.php b/app/Console/Commands/ClearConfigCommand.php new file mode 100644 index 000000000000..291ad2bfcd71 --- /dev/null +++ b/app/Console/Commands/ClearConfigCommand.php @@ -0,0 +1,26 @@ +setLaravel($this->laravel); + $configClearCommand->run(new ArrayInput([]), $this->output); + + LibrenmsConfig::invalidateCache(); + } +} diff --git a/app/Console/Commands/DevicePoll.php b/app/Console/Commands/DevicePoll.php index 5d38cbee5438..935b29cf9da0 100644 --- a/app/Console/Commands/DevicePoll.php +++ b/app/Console/Commands/DevicePoll.php @@ -4,6 +4,7 @@ use App\Console\LnmsCommand; use App\Events\DevicePolled; +use App\Facades\LibrenmsConfig; use App\Jobs\PollDevice; use App\Models\Device; use App\Polling\Measure\MeasurementManager; @@ -56,6 +57,8 @@ public function handle(MeasurementManager $measurements): int if ($this->getOutput()->isVerbose()) { Log::debug(Version::get()->header()); \LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache + LibrenmsConfig::invalidateCache(); + LibrenmsConfig::reload(); } $module_overrides = Module::parseUserOverrides(explode(',', $this->option('modules') ?? '')); diff --git a/config/librenms.php b/config/librenms.php index 3cff19b25e3f..621c96ec194b 100644 --- a/config/librenms.php +++ b/config/librenms.php @@ -61,5 +61,5 @@ | Amount of seconds to allow the config to be cached. 0 means no cache. */ - 'config_cache_ttl' => env('CONFIG_CACHE_TTL', 0), + 'config_cache_ttl' => env('CONFIG_CACHE_TTL', 300), ]; diff --git a/discovery.php b/discovery.php index 9056c589761d..ab3180b47507 100755 --- a/discovery.php +++ b/discovery.php @@ -70,6 +70,8 @@ echo "DEBUG!\n"; Debug::setVerbose(isset($options['v'])); \LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache + LibrenmsConfig::invalidateCache(); + LibrenmsConfig::reload(); } if (! $where) { diff --git a/lang/en/commands.php b/lang/en/commands.php index 1db457d6df4f..a0d47791680e 100644 --- a/lang/en/commands.php +++ b/lang/en/commands.php @@ -1,6 +1,9 @@ [ + 'description' => 'Clear config cache. This will allow any changes that have been made since the last full config load to be reflected in the current config.', + ], 'config:get' => [ 'description' => 'Get configuration value', 'arguments' => [ diff --git a/scripts/collect-snmp-data.php b/scripts/collect-snmp-data.php index 144c2d475ef3..6836fe0e2e84 100755 --- a/scripts/collect-snmp-data.php +++ b/scripts/collect-snmp-data.php @@ -127,6 +127,8 @@ echo 'Capturing Data: '; \LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache + LibrenmsConfig::invalidateCache(); + LibrenmsConfig::reload(); $capture->captureFromDevice($device['device_id'], $prefer_new_snmprec, $full); echo "\nVerify these file(s) do not contain any private data before sharing!\n"; } catch (InvalidModuleException $e) { diff --git a/scripts/save-test-data.php b/scripts/save-test-data.php index d4aff00caf4d..0b08223c3f43 100755 --- a/scripts/save-test-data.php +++ b/scripts/save-test-data.php @@ -144,6 +144,8 @@ echo PHP_EOL; \LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache + LibrenmsConfig::invalidateCache(); + LibrenmsConfig::reload(); $tester = new ModuleTestHelper($modules, $target_os, $target_variant); if (! $no_save && ! empty($output_file)) { $tester->setJsonSavePath($output_file); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index b1d47f28f3ea..43d687fee42d 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -84,5 +84,7 @@ } \LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache +LibrenmsConfig::invalidateCache(); +LibrenmsConfig::reload(); app()->terminate(); // destroy the bootstrap Laravel application