#env-var #environment #variables #change #snapshot #subscription #viewing

env-watcher

A simple library for viewing environment variables with a subscription to change any variables

1 unstable release

0.1.0 Dec 16, 2021

#561 in Configuration

MIT license

29KB
605 lines

ci

A simple library for viewing environment variables with a subscription to change any variables.

Implementation of viewing variables in real time. In case of changing variables, get a snapshot of data or a change event (Delete/Edit).

Usage

When used locally, you must create an instance of the observer that you will control. Duration - defines the polling time of variables. Default - Duration::from_millis(5 * 100)

let env_watcher = EnvironmentWatcher::new(Duration::from_secs(5));

Next, subscribe to any keys, all options are described in paragraph

let sub_for_all = Subscribe::All;
let (data, rx) = env_watcher.subscribe(sub_for_all)?;

In this case, we received a snapshot of the data at the time of subscription, as well as the channel through which we will receive events.
Suppose we have a running server that should restart if the server.port environment variable changes, let's try to write this!

fn listen_environment() -> Result<(), Error> {
    let env_watcher = EnvironmentWatcher::new(Duration::from_secs(5));
    
    // An analogue of this subscription can be: Subscribe::PatternEnvs(vec!["server.*".to_string()]); 
    let sub_server = Subscribe::Envs(vec!["server.host".to_string(), "server.port".to_string()]); 
    
    let (data, rx) = env_watcher.subscribe(sub_server)?;
    spawn(move || {
        let receiver = rx.clone();
        let data = data.clone();
        loop {
            let event = receiver.recv().unwrap();
            match event {
                ChangeState::Edit(k, v) => {
                    data.insert(k, v);
                    restart_server(&data);
                }
                ChangeState::Delete(k) => {
                    warn("Removed one of the server environment variables. In case of restarting the application, incorrect behavior may be expected!");
                }
            }
        }
    });
    Ok(())
}

fn restart_server(data: &HashMap<String, String>) {
    ...
    println("Restarting server");
}

You can see a more detailed example in the project.

Base implementation for data

EnvironmentData serves as a basic snapshot keeper. In a separate thread, the values are updated if they change in the environment.

EnvironmentData serves as the primary storage for snapshots. On a separate thread, the values are updated if they change in the environment.

You can get a snapshot of the data using the data() method

If a mutable reference is needed then use ref_data()

Derive usage

This module provides 3 macros to simplify your work.

  • init_env_watch! - basic storage initialization
  • sub_env! - subscription with snapshot return and channel for event management
  • sub_env_snapshot! - basic snapshot implementation

Subscribing to environment variables

We have 3 subscription options in total.

  • All - subscribing to all changes to environment variables
  • Envs - subscription for specific keys only
  • PatternEnvs - subscribing only to specific keys using regular expressions thanks to the library

Release History

See Changelog

License

MIT License

Dependencies

~3–35MB
~483K SLoC