service.rs 3.64 KB
Newer Older
1
2
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
Ryan Olson's avatar
Ryan Olson committed
3
4

use derive_getters::Dissolve;
5
6
use std::collections::HashMap;
use std::sync::Mutex;
Ryan Olson's avatar
Ryan Olson committed
7
8
9

use super::*;

10
pub use super::endpoint::EndpointStats;
Ryan Olson's avatar
Ryan Olson committed
11
pub type StatsHandler =
12
    Box<dyn FnMut(String, EndpointStats) -> serde_json::Value + Send + Sync + 'static>;
13
pub type EndpointStatsHandler =
14
    Box<dyn FnMut(EndpointStats) -> serde_json::Value + Send + Sync + 'static>;
15

Neelay Shah's avatar
Neelay Shah committed
16
pub const PROJECT_NAME: &str = "Dynamo";
17

Ryan Olson's avatar
Ryan Olson committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#[derive(Educe, Builder, Dissolve)]
#[educe(Debug)]
#[builder(pattern = "owned", build_fn(private, name = "build_internal"))]
pub struct ServiceConfig {
    #[builder(private)]
    component: Component,

    /// Description
    #[builder(default)]
    description: Option<String>,
}

impl ServiceConfigBuilder {
    /// Create the [`Component`]'s service and store it in the registry.
    pub async fn create(self) -> Result<Component> {
33
        let (component, description) = self.build_internal()?.dissolve();
Ryan Olson's avatar
Ryan Olson committed
34

35
        let version = "0.0.1".to_string();
Ryan Olson's avatar
Ryan Olson committed
36

Ryan Olson's avatar
Ryan Olson committed
37
        let service_name = component.service_name();
38
39
        log::debug!("component: {component}; creating, service_name: {service_name}");

Ryan Olson's avatar
Ryan Olson committed
40
        let description = description.unwrap_or(format!(
41
            "{PROJECT_NAME} component {} in namespace {}",
Ryan Olson's avatar
Ryan Olson committed
42
43
44
            component.name, component.namespace
        ));

45
46
47
48
        let stats_handler_registry: Arc<Mutex<HashMap<String, EndpointStatsHandler>>> =
            Arc::new(Mutex::new(HashMap::new()));

        let stats_handler_registry_clone = stats_handler_registry.clone();
Ryan Olson's avatar
Ryan Olson committed
49

50
51
52
        let mut guard = component.drt.component_registry.inner.lock().await;

        if guard.services.contains_key(&service_name) {
Ryan Olson's avatar
Ryan Olson committed
53
54
55
56
57
            return Err(anyhow::anyhow!("Service already exists"));
        }

        // create service on the secondary runtime
        let builder = component.drt.nats_client.client().service_builder();
58
59

        tracing::debug!("Starting service: {}", service_name);
60
        let service_builder = builder
61
62
63
64
65
66
67
68
            .description(description)
            .stats_handler(move |name, stats| {
                log::trace!("stats_handler: {name}, {stats:?}");
                let mut guard = stats_handler_registry.lock().unwrap();
                match guard.get_mut(&name) {
                    Some(handler) => handler(stats),
                    None => serde_json::Value::Null,
                }
69
70
71
            });
        tracing::debug!("Got builder");
        let service = service_builder
72
73
            .start(service_name.clone(), version)
            .await
Ryan Olson's avatar
Ryan Olson committed
74
75
            .map_err(|e| anyhow::anyhow!("Failed to start service: {e}"))?;

76
77
78
79
80
81
82
83
84
85
86
87
        // new copy of service_name as the previous one is moved into the task above
        let service_name = component.service_name();

        // insert the service into the registry
        guard.services.insert(service_name.clone(), service);

        // insert the stats handler into the registry
        guard
            .stats_handlers
            .insert(service_name, stats_handler_registry_clone);

        // drop the guard to unlock the mutex
Ryan Olson's avatar
Ryan Olson committed
88
89
        drop(guard);

90
91
92
93
94
95
96
97
98
        // Register metrics callback. CRITICAL: Never fail service creation for metrics issues.
        if let Err(err) = component.start_scraping_nats_service_component_metrics() {
            tracing::debug!(
                "Metrics registration failed for '{}': {}",
                component.service_name(),
                err
            );
        }

Ryan Olson's avatar
Ryan Olson committed
99
100
101
102
103
104
105
106
107
        Ok(component)
    }
}

impl ServiceConfigBuilder {
    pub(crate) fn from_component(component: Component) -> Self {
        Self::default().component(component)
    }
}