Crates.io | env-watcher |
lib.rs | env-watcher |
version | 0.1.0 |
source | src |
created_at | 2021-12-16 00:42:21.15138 |
updated_at | 2021-12-16 00:42:21.15138 |
description | A simple library for viewing environment variables with a subscription to change any variables. |
homepage | |
repository | https://github.com/SinmoWay/env-watcher |
max_upload_size | |
id | 498906 |
size | 49,196 |
Implementation of viewing variables in real time. In case of changing variables, get a snapshot of data or a change event (Delete/Edit).
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.
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()
This module provides 3 macros to simplify your work.
init_env_watch!
- basic storage initializationsub_env!
- subscription with snapshot return and channel for event managementsub_env_snapshot!
- basic snapshot implementationWe have 3 subscription options in total.
See Changelog
MIT License