summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge_mapper
diff options
context:
space:
mode:
authorPradeepKiruvale <PRADEEPKIRUVALE@gmail.com>2022-01-18 15:30:19 +0530
committerGitHub <noreply@github.com>2022-01-18 15:30:19 +0530
commit26d5adc1d49729d4d2cda067366fc37098811d6d (patch)
tree44fe6fd1fe79750450c2c4f0856a7d82a5fa172a /crates/core/tedge_mapper
parent75f480431b9ff28e8355e38915605a8ce0739c68 (diff)
[664] Refactor mapper flock (#756)
* move flock to main * use full mapper names Co-authored-by: Pradeep Kumar K J <pradeepkumar.kj@sofwareag.com>
Diffstat (limited to 'crates/core/tedge_mapper')
-rw-r--r--crates/core/tedge_mapper/src/main.rs18
-rw-r--r--crates/core/tedge_mapper/src/mapper.rs17
2 files changed, 19 insertions, 16 deletions
diff --git a/crates/core/tedge_mapper/src/main.rs b/crates/core/tedge_mapper/src/main.rs
index d3324ef7..dc4433e4 100644
--- a/crates/core/tedge_mapper/src/main.rs
+++ b/crates/core/tedge_mapper/src/main.rs
@@ -1,8 +1,11 @@
+use std::fmt;
+
use crate::sm_c8y_mapper::mapper::CumulocitySoftwareManagementMapper;
use crate::{
az_mapper::AzureMapper, c8y_mapper::CumulocityMapper, collectd_mapper::mapper::CollectdMapper,
component::TEdgeComponent, error::*,
};
+use flockfile::check_another_instance_is_not_running;
use structopt::*;
use tedge_config::*;
use tedge_utils::paths::home_dir;
@@ -55,14 +58,27 @@ pub enum MapperName {
SmC8y,
}
+impl fmt::Display for MapperName {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ MapperName::Az => write!(f, "{}", "tedge-mapper-az"),
+ MapperName::C8y => write!(f, "{}", "tedge-mapper-c8y"),
+ MapperName::Collectd => write!(f, "{}", "tedge-mapper-collectd"),
+ MapperName::SmC8y => write!(f, "{}", "sm-c8y-mapper"),
+ }
+ }
+}
+
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let mapper = MapperOpt::from_args();
tedge_utils::logging::initialise_tracing_subscriber(mapper.debug);
let component = lookup_component(&mapper.name);
-
let config = tedge_config()?;
+ // Run only one instance of a mapper
+ let _flock = check_another_instance_is_not_running(&mapper.name.to_string())?;
+
component.start(config).await
}
diff --git a/crates/core/tedge_mapper/src/mapper.rs b/crates/core/tedge_mapper/src/mapper.rs
index 2f90e7bb..9ac94b2d 100644
--- a/crates/core/tedge_mapper/src/mapper.rs
+++ b/crates/core/tedge_mapper/src/mapper.rs
@@ -1,7 +1,6 @@
use crate::converter::*;
use crate::error::*;
-use flockfile::{check_another_instance_is_not_running, Flockfile};
use mqtt_client::{Client, MqttClient, MqttClientError};
use tedge_config::{ConfigSettingAccessor, MqttPortSetting, TEdgeConfig};
use tokio::task::JoinHandle;
@@ -12,14 +11,12 @@ pub async fn create_mapper<'a>(
tedge_config: &'a TEdgeConfig,
converter: Box<dyn Converter<Error = ConversionError>>,
) -> Result<Mapper, anyhow::Error> {
- let flock = check_another_instance_is_not_running(app_name)?;
-
info!("{} starting", app_name);
let mqtt_config = mqtt_config(tedge_config)?;
let mqtt_client = Client::connect(app_name, &mqtt_config).await?;
- Ok(Mapper::new(mqtt_client, converter, flock))
+ Ok(Mapper::new(mqtt_client, converter))
}
pub(crate) fn mqtt_config(
@@ -31,20 +28,14 @@ pub(crate) fn mqtt_config(
pub struct Mapper {
client: mqtt_client::Client,
converter: Box<dyn Converter<Error = ConversionError>>,
- _flock: Flockfile,
}
impl Mapper {
pub fn new(
client: mqtt_client::Client,
converter: Box<dyn Converter<Error = ConversionError>>,
- _flock: Flockfile,
) -> Self {
- Self {
- client,
- converter,
- _flock,
- }
+ Self { client, converter }
}
pub(crate) async fn run(&mut self) -> Result<(), MqttClientError> {
@@ -106,16 +97,12 @@ mod tests {
// Given a mapper
let name = "mapper_under_test";
- let flock = check_another_instance_is_not_running(name)
- .expect("Another mapper instance is locking /run/lock/mapper_under_test.lock");
-
let mqtt_config = mqtt_client::Config::default().with_port(broker.port);
let mqtt_client = Client::connect(name, &mqtt_config).await?;
let mut mapper = Mapper {
client: mqtt_client,
converter: Box::new(UppercaseConverter::new()),
- _flock: flock,
};
// Let's run the mapper in the background