Skip to content

Commit 8e454ec

Browse files
committed
refactor: replace lazy-static
Signed-off-by: Jan Zachmann <50990105+JanZachmann@users.noreply.github.com>
1 parent 87a1f5e commit 8e454ec

File tree

7 files changed

+87
-115
lines changed

7 files changed

+87
-115
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ futures = { version = "0.3", default-features = false }
2626
futures-util = { version = "0.3", default-features = false }
2727
glob = { version = "0.3", default-features = false }
2828
inotify = { version = "0.11", default-features = false, features = ["stream"] }
29-
lazy_static = { version = "1.5", default-features = false }
3029
log = { version = "0.4", default-features = false }
3130
log-panics = { version = "2", default-features = false }
3231
modemmanager = { git = "https://github.com/omnect/modemmanager.git", tag = "0.3.4", default-features = false, optional = true }

src/twin/modem_info.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::twin::feature::{self, *};
22
use anyhow::{Context, Result, bail};
33
use azure_iot_sdk::client::IotMessage;
4-
use lazy_static::lazy_static;
54
use serde_json::json;
65
use std::{env, time::Duration};
76
use tokio::sync::mpsc::Sender;
@@ -300,10 +299,13 @@ mod inner {
300299

301300
impl ModemInfo {
302301
pub fn new() -> Result<Self> {
303-
if 0 < *REFRESH_MODEM_INFO_INTERVAL_SECS {
304-
feature::notify_interval::<Self>(Duration::from_secs(
305-
*REFRESH_MODEM_INFO_INTERVAL_SECS,
306-
))?;
302+
let refresh_interval = env::var("REFRESH_MODEM_INFO_INTERVAL_SECS")
303+
.unwrap_or("600".to_string())
304+
.parse::<u64>()
305+
.context("cannot parse REFRESH_MODEM_INFO_INTERVAL_SECS env var")?;
306+
307+
if 0 < refresh_interval {
308+
feature::notify_interval::<Self>(Duration::from_secs(refresh_interval))?;
307309
}
308310

309311
Ok(ModemInfo {
@@ -337,16 +339,6 @@ pub use inner::ModemInfo;
337339
const MODEM_INFO_VERSION: u8 = 1;
338340
const ID: &str = "modem_info";
339341

340-
lazy_static! {
341-
static ref REFRESH_MODEM_INFO_INTERVAL_SECS: u64 = {
342-
const REFRESH_MODEM_INFO_INTERVAL_SECS_DEFAULT: &str = "600";
343-
env::var("REFRESH_MODEM_INFO_INTERVAL_SECS")
344-
.unwrap_or(REFRESH_MODEM_INFO_INTERVAL_SECS_DEFAULT.to_string())
345-
.parse::<u64>()
346-
.expect("cannot parse REFRESH_MODEM_INFO_INTERVAL_SECS env var")
347-
};
348-
}
349-
350342
impl Feature for ModemInfo {
351343
fn name(&self) -> String {
352344
ID.to_string()

src/twin/network.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,12 @@ use crate::{
55
};
66
use anyhow::{Context, Result, bail};
77
use azure_iot_sdk::client::IotMessage;
8-
use lazy_static::lazy_static;
98
use log::{debug, error, info, warn};
109
use serde::Serialize;
1110
use serde_json::json;
1211
use std::{env, time::Duration};
1312
use tokio::sync::mpsc::Sender;
1413

15-
lazy_static! {
16-
static ref REFRESH_NETWORK_STATUS_INTERVAL_SECS: u64 = {
17-
const REFRESH_NETWORK_STATUS_INTERVAL_SECS_DEFAULT: &str = "60";
18-
env::var("REFRESH_NETWORK_STATUS_INTERVAL_SECS")
19-
.unwrap_or(REFRESH_NETWORK_STATUS_INTERVAL_SECS_DEFAULT.to_string())
20-
.parse::<u64>()
21-
.expect("cannot parse REFRESH_NETWORK_STATUS_INTERVAL_SECS env var")
22-
};
23-
}
24-
2514
static NETWORK_SERVICE: &str = "systemd-networkd.service";
2615

2716
#[derive(PartialEq, Serialize)]
@@ -100,10 +89,13 @@ impl Network {
10089
const ID: &'static str = "network_status";
10190

10291
pub fn new() -> Result<Self> {
103-
if 0 < *REFRESH_NETWORK_STATUS_INTERVAL_SECS {
104-
feature::notify_interval::<Self>(Duration::from_secs(
105-
*REFRESH_NETWORK_STATUS_INTERVAL_SECS,
106-
))?;
92+
let refresh_interval = env::var("REFRESH_NETWORK_STATUS_INTERVAL_SECS")
93+
.unwrap_or("60".to_string())
94+
.parse::<u64>()
95+
.context("cannot parse REFRESH_NETWORK_STATUS_INTERVAL_SECS env var")?;
96+
97+
if 0 < refresh_interval {
98+
feature::notify_interval::<Self>(Duration::from_secs(refresh_interval))?;
10799
}
108100

109101
Ok(Network {

src/twin/provisioning_config.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
use crate::twin::feature::{self, *};
22
use anyhow::{Context, Result, bail, ensure};
33
use azure_iot_sdk::client::IotMessage;
4-
use lazy_static::lazy_static;
54
use log::{debug, info, warn};
65
use serde::Serialize;
76
use serde_json::json;
87
use std::{env, path::Path, time::Duration};
98
use time::format_description::well_known::Rfc3339;
109
use tokio::sync::mpsc::Sender;
1110

12-
lazy_static! {
13-
static ref REFRESH_EST_EXPIRY_INTERVAL_SECS: u64 = {
14-
const REFRESH_EST_EXPIRY_INTERVAL_SECS_DEFAULT: &str = "180";
15-
env::var("REFRESH_EST_EXPIRY_INTERVAL_SECS")
16-
.unwrap_or(REFRESH_EST_EXPIRY_INTERVAL_SECS_DEFAULT.to_string())
17-
.parse::<u64>()
18-
.expect("cannot parse REFRESH_EST_EXPIRY_INTERVAL_SECS env var")
19-
};
20-
}
21-
2211
#[derive(Debug, Serialize)]
2312
#[serde(rename_all = "snake_case")]
2413
enum Source {
@@ -219,7 +208,12 @@ impl ProvisioningConfig {
219208
_ => bail!("provisioning_config: invalid provisioning configuration found"),
220209
};
221210

222-
if 0 < *REFRESH_EST_EXPIRY_INTERVAL_SECS
211+
let refresh_interval = env::var("REFRESH_EST_EXPIRY_INTERVAL_SECS")
212+
.unwrap_or("180".to_string())
213+
.parse::<u64>()
214+
.context("cannot parse REFRESH_EST_EXPIRY_INTERVAL_SECS env var")?;
215+
216+
if 0 < refresh_interval
223217
&& matches!(
224218
method,
225219
Method::X509(X509 {
@@ -228,9 +222,7 @@ impl ProvisioningConfig {
228222
})
229223
)
230224
{
231-
feature::notify_interval::<Self>(Duration::from_secs(
232-
*REFRESH_EST_EXPIRY_INTERVAL_SECS,
233-
))?;
225+
feature::notify_interval::<Self>(Duration::from_secs(refresh_interval))?;
234226
}
235227

236228
let this = ProvisioningConfig {

src/twin/system_info.rs

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::{
77
use anyhow::{Context, Result, bail};
88
use azure_iot_sdk::client::{IotHubClient, IotMessage};
99
use inotify::WatchMask;
10-
use lazy_static::lazy_static;
1110
use log::{info, warn};
1211
use serde::{Deserialize, Serialize, Serializer, ser::Error};
1312
use serde_json::json;
@@ -22,17 +21,10 @@ use time::format_description::well_known::Rfc3339;
2221
use tokio::sync::mpsc;
2322

2423
static BOOTLOADER_UPDATED_FILE: &str = "/run/omnect-device-service/omnect_bootloader_updated";
25-
26-
// ToDo
27-
lazy_static! {
28-
static ref REFRESH_SYSTEM_INFO_INTERVAL_SECS: u64 = {
29-
const REFRESH_SYSTEM_INFO_INTERVAL_SECS_DEFAULT: &str = "60";
30-
env::var("REFRESH_SYSTEM_INFO_INTERVAL_SECS")
31-
.unwrap_or(REFRESH_SYSTEM_INFO_INTERVAL_SECS_DEFAULT.to_string())
32-
.parse::<u64>()
33-
.expect("cannot parse REFRESH_SYSTEM_INFO_INTERVAL_SECS env var")
34-
};
35-
}
24+
#[cfg(not(feature = "mock"))]
25+
static TIMESYNC_FILE: &str = "/run/systemd/timesync/synchronized";
26+
#[cfg(feature = "mock")]
27+
static TIMESYNC_FILE: &str = "/tmp/synchronized";
3628

3729
#[derive(Default, Serialize)]
3830
struct Label {
@@ -73,7 +65,7 @@ enum MetricValue {
7365
}
7466

7567
impl MetricValue {
76-
fn to_metric(self) -> Metric {
68+
fn metric(self) -> Metric {
7769
let (name, value, labels) = match self {
7870
MetricValue::CpuUsage(value) => ("cpu_usage".to_owned(), value, Label::default()),
7971
MetricValue::MemoryUsed(value) => ("memory_used".to_owned(), value, Label::default()),
@@ -130,15 +122,6 @@ where
130122
s.serialize_str(&time_stamp)
131123
}
132124

133-
// ToDo rm lazy_staticw
134-
lazy_static! {
135-
static ref TIMESYNC_FILE: &'static Path = if cfg!(feature = "mock") {
136-
Path::new("/tmp/synchronized")
137-
} else {
138-
Path::new("/run/systemd/timesync/synchronized")
139-
};
140-
}
141-
142125
#[derive(Clone, Debug, Deserialize, PartialEq)]
143126
pub struct FleetIdCommand {
144127
pub fleet_id: String,
@@ -253,12 +236,18 @@ impl SystemInfo {
253236
RootPartition::current()?.as_str()
254237
);
255238

256-
feature::add_watch::<Self>(&TIMESYNC_FILE, WatchMask::CREATE | WatchMask::ONESHOT)?;
239+
feature::add_watch::<Self>(
240+
Path::new(TIMESYNC_FILE),
241+
WatchMask::CREATE | WatchMask::ONESHOT,
242+
)?;
257243

258-
if 0 < *REFRESH_SYSTEM_INFO_INTERVAL_SECS {
259-
feature::notify_interval::<Self>(Duration::from_secs(
260-
*REFRESH_SYSTEM_INFO_INTERVAL_SECS,
261-
))?;
244+
let refresh_interval = env::var("REFRESH_SYSTEM_INFO_INTERVAL_SECS")
245+
.unwrap_or("60".to_string())
246+
.parse::<u64>()
247+
.context("cannot parse REFRESH_SYSTEM_INFO_INTERVAL_SECS env var")?;
248+
249+
if 0 < refresh_interval {
250+
feature::notify_interval::<Self>(Duration::from_secs(refresh_interval))?;
262251
}
263252

264253
Ok(SystemInfo {
@@ -332,12 +321,14 @@ impl SystemInfo {
332321
return Ok(());
333322
};
334323

335-
let Ok(mut time_stamp) = TIME_STAMP.write() else {
336-
bail!("metrics: failed to lock TIME_STAMP")
337-
};
338-
*time_stamp = time::OffsetDateTime::now_utc()
339-
.format(&Rfc3339)
340-
.context("metrics: failed to get and format timestamp")?;
324+
{
325+
let Ok(mut time_stamp) = TIME_STAMP.write() else {
326+
bail!("metrics: failed to lock TIME_STAMP")
327+
};
328+
*time_stamp = time::OffsetDateTime::now_utc()
329+
.format(&Rfc3339)
330+
.context("metrics: failed to get and format timestamp")?;
331+
}
341332

342333
self.hardware_info.components.refresh(true);
343334
self.hardware_info.system.refresh_cpu_usage();
@@ -356,16 +347,16 @@ impl SystemInfo {
356347
.unwrap_or((0, 0));
357348

358349
let mut metrics = vec![
359-
MetricValue::CpuUsage(self.hardware_info.system.global_cpu_usage() as f64).to_metric(),
360-
MetricValue::MemoryUsed(self.hardware_info.system.used_memory() as f64).to_metric(),
361-
MetricValue::MemoryTotal(self.hardware_info.system.total_memory() as f64).to_metric(),
362-
MetricValue::DiskUsed(disk_used as f64).to_metric(),
363-
MetricValue::DiskTotal(disk_total as f64).to_metric(),
350+
MetricValue::CpuUsage(self.hardware_info.system.global_cpu_usage() as f64).metric(),
351+
MetricValue::MemoryUsed(self.hardware_info.system.used_memory() as f64).metric(),
352+
MetricValue::MemoryTotal(self.hardware_info.system.total_memory() as f64).metric(),
353+
MetricValue::DiskUsed(disk_used as f64).metric(),
354+
MetricValue::DiskTotal(disk_total as f64).metric(),
364355
];
365356

366357
self.hardware_info.components.iter().for_each(|c| {
367358
if let Some(t) = c.temperature() {
368-
metrics.push(MetricValue::Temp(t.into(), c.label().to_string()).to_metric())
359+
metrics.push(MetricValue::Temp(t.into(), c.label().to_string()).metric())
369360
};
370361
});
371362

0 commit comments

Comments
 (0)