Skip to main content

rustfs_notify/
registry.rs

1// Copyright 2024 RustFS Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::Event;
16use crate::factory::builtin_target_plugins;
17use rustfs_config::notify::NOTIFY_ROUTE_PREFIX;
18use rustfs_config::server_config::{Config, KVS};
19use rustfs_targets::{Target, TargetError, TargetPluginRegistry};
20
21/// Registry for managing target factories
22pub struct TargetRegistry {
23    plugins: TargetPluginRegistry<Event>,
24}
25
26impl Default for TargetRegistry {
27    fn default() -> Self {
28        Self::new()
29    }
30}
31
32impl TargetRegistry {
33    /// Creates a new TargetRegistry with built-in factories
34    pub fn new() -> Self {
35        let mut plugins = TargetPluginRegistry::new();
36        plugins.register_all(builtin_target_plugins());
37
38        TargetRegistry { plugins }
39    }
40
41    #[cfg(test)]
42    pub(crate) fn with_plugins(plugins: TargetPluginRegistry<Event>) -> Self {
43        Self { plugins }
44    }
45
46    pub fn supports_target_type(&self, target_type: &str) -> bool {
47        self.plugins.supports_target_type(target_type)
48    }
49
50    /// Creates a target from configuration
51    pub async fn create_target(
52        &self,
53        target_type: &str,
54        id: String,
55        config: &KVS,
56    ) -> Result<Box<dyn Target<Event> + Send + Sync>, TargetError> {
57        self.plugins.create_target(target_type, id, config)
58    }
59
60    /// Creates all targets from a configuration
61    /// Create all notification targets from system configuration and environment variables.
62    /// This method processes the creation of each target concurrently as follows:
63    /// 1. Iterate through all registered target types (e.g. webhooks, mqtt).
64    /// 2. For each type, resolve its configuration in the configuration file and environment variables.
65    /// 3. Identify all target instance IDs that need to be created.
66    /// 4. Combine the default configuration, file configuration, and environment variable configuration for each instance.
67    /// 5. If the instance is enabled, create an asynchronous task for it to instantiate.
68    /// 6. Concurrency executes all creation tasks and collects results.
69    pub async fn create_targets_from_config(
70        &self,
71        config: &Config,
72    ) -> Result<Vec<Box<dyn Target<Event> + Send + Sync>>, TargetError> {
73        self.plugins.create_targets_from_config(config, NOTIFY_ROUTE_PREFIX).await
74    }
75
76    pub(crate) async fn create_dormant_targets_from_config(
77        &self,
78        config: &Config,
79    ) -> Result<(Vec<Box<dyn Target<Event> + Send + Sync>>, Vec<String>), TargetError> {
80        self.plugins
81            .create_dormant_targets_from_config(config, NOTIFY_ROUTE_PREFIX)
82            .await
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::TargetRegistry;
89    use rustfs_targets::target::ChannelTargetType;
90
91    #[test]
92    fn registry_registers_amqp_factory() {
93        let registry = TargetRegistry::new();
94
95        assert!(registry.supports_target_type(ChannelTargetType::Amqp.as_str()));
96    }
97}