lib/config_skeleton.rb in config_skeleton-0.3.0 vs lib/config_skeleton.rb in config_skeleton-0.3.1

- old
+ new

@@ -21,11 +21,12 @@ # 1. Declare all the environment variables you care about, with the # ServiceSkeleton declaration methods `string`, `integer`, etc. # # 1. Implement service-specific config generation and reloading code, by # overriding the private methods #config_file, #config_data, and #reload_server -# (and also potentially #config_ok? and #sleep_duration). +# (and also potentially #config_ok?, #sleep_duration, #before_regenerate_config, and +# #after_regenerate_config). # See the documentation for those methods for what they need to do. # # 1. Setup any file watchers you want with .watch and #watch. # # 1. Instantiate your new class, passing in an environment hash, and then call @@ -360,10 +361,24 @@ # def config_data raise NotImplementedError, "config_data must be implemented in subclass." end + # Run code before the config is regenerated and the config_file + # is written. + # + # @note this can optionally be implemented by subclasses. + # + def before_regenerate_config(force_reload); end + + # Run code after the config is regenerated and if the regeneration + # was forced the new config has been cycled in. + # + # @note this can optionally be implemented by subclasses. + # + def after_regenerate_config(force_reload); end + # Verify that the currently running config is acceptable. # # In the event that a generated config is "bad", it may be possible to detect # that the server hasn't accepted the new config, and if so, the config can # be rolled back to a known-good state and the `<prefix>_config_ok` metric @@ -441,10 +456,12 @@ # received, for instance), set `force_reload: true` and we'll be really insistent. # # @return [void] # def regenerate_config(force_reload: false) + before_regenerate_config(force_reload) + logger.debug(logloc) { "force? #{force_reload.inspect}" } tmpfile = Tempfile.new(service_name, File.dirname(config_file)) logger.debug(logloc) { "Tempfile is #{tmpfile.path}" } unless (new_config = instrumented_config_data).nil? File.write(tmpfile.path, new_config) @@ -464,9 +481,11 @@ if force_reload || diff.to_s != "" cycle_config(tmpfile.path) end end + + after_regenerate_config(force_reload) ensure metrics.last_change_timestamp.set({}, File.stat(config_file).mtime.to_f) tmpfile.close rescue nil tmpfile.unlink rescue nil end