• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dariusbakunas / cogrs / 13484990103

23 Feb 2025 04:47PM UTC coverage: 37.129% (-0.1%) from 37.246%
13484990103

push

github

dariusbakunas
feat: get callback plugin path from configuration

0 of 12 new or added lines in 3 files covered. (0.0%)

1 existing line in 1 file now uncovered.

714 of 1923 relevant lines covered (37.13%)

1.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/cogrs-core/src/executor/task_queue_manager.rs
1
use crate::executor::play_iterator::PlayIterator;
2
use crate::inventory::host::Host;
3
use crate::inventory::manager::InventoryManager;
4
use crate::playbook::play::Play;
5
use crate::strategy::linear::LinearStrategy;
6
use crate::strategy::Strategy;
7
use crate::vars::manager::VariableManager;
8
use anyhow::Result;
9
use cogrs_plugins::callback::{CallbackPlugin, EventType};
10
use serde_json::Value;
11
use std::cmp::min;
12
use std::collections::HashMap;
13
use std::path::PathBuf;
14
use std::sync::Arc;
15

16
pub struct TaskQueueManager {
17
    forks: usize,
18
    callbacks_loaded: bool,
19
    callbacks: HashMap<EventType, Vec<Arc<dyn CallbackPlugin>>>,
20
    terminated: bool,
21
    unreachable_hosts: HashMap<String, Host>,
22
    workers: Vec<tokio::task::JoinHandle<()>>,
23
    callback_plugin_path: PathBuf,
24
}
25

26
const DEFAULT_FORKS: usize = 5;
27

28
impl TaskQueueManager {
NEW
29
    pub fn new(forks: Option<usize>, callback_plugin_path: &PathBuf) -> Self {
×
30
        Self {
31
            callbacks: HashMap::new(),
×
32
            callbacks_loaded: false,
33
            forks: forks.unwrap_or(DEFAULT_FORKS),
×
34
            terminated: false,
35
            unreachable_hosts: HashMap::new(),
×
36
            workers: Vec::with_capacity(forks.unwrap_or(DEFAULT_FORKS)),
×
NEW
37
            callback_plugin_path: callback_plugin_path.to_path_buf(),
×
38
        }
39
    }
40

41
    pub fn get_worker(&mut self, index: usize) -> Option<&tokio::task::JoinHandle<()>> {
×
42
        self.workers.get(index)
×
43
    }
44

45
    pub fn set_worker(&mut self, index: usize, worker: tokio::task::JoinHandle<()>) {
×
46
        self.workers.insert(index, worker);
×
47
    }
48

49
    /// Iterates over the roles/tasks in a play, using the given (or default)
50
    /// strategy for queueing tasks. The default is the linear strategy, which
51
    /// operates like classic Ansible by keeping all hosts in lock-step with
52
    /// a given task (meaning no hosts move on to the next task until all hosts
53
    /// are done with the current task).
54
    pub async fn run(
×
55
        &mut self,
56
        play: Play,
57
        variable_manager: &VariableManager,
58
        inventory_manager: &InventoryManager,
59
    ) -> Result<()> {
60
        self.load_callbacks().await?;
×
61
        let all_vars = variable_manager.get_vars(Some(&play), None, None, None, true, true);
×
62

63
        self.emit_event(EventType::PlaybookOnPlayStart, None).await;
×
64

65
        let strategy = *play.strategy();
×
66

67
        let mut play_iterator = PlayIterator::new(play);
×
68
        play_iterator.init(inventory_manager)?;
×
69

70
        self.forks = min(self.forks, play_iterator.batch_size());
×
71

72
        match strategy {
×
73
            Strategy::Linear => {
74
                let mut strategy = LinearStrategy::new(self, inventory_manager, variable_manager);
×
75
                strategy.run(&mut play_iterator).await?;
×
76
            }
77
            Strategy::Free => {
78
                todo!()
79
            }
80
        }
81

82
        Ok(())
×
83
    }
84

85
    pub fn register_callback(&mut self, callback: Arc<dyn CallbackPlugin>) {
×
86
        for event in callback.get_interested_events() {
×
87
            self.callbacks
×
88
                .entry(event)
89
                .or_insert_with(Vec::new)
90
                .push(callback.clone());
×
91
        }
92
    }
93

94
    pub fn get_unreachable_hosts(&self) -> &HashMap<String, Host> {
×
95
        &self.unreachable_hosts
×
96
    }
97

98
    pub fn is_terminated(&self) -> bool {
×
99
        self.terminated
×
100
    }
101

102
    async fn load_callbacks(&mut self) -> Result<()> {
×
103
        let plugin_loader = cogrs_plugins::plugin_loader::PluginLoader::instance();
×
104
        let loader = plugin_loader.lock().await;
×
105

NEW
106
        let plugins = loader
×
NEW
107
            .get_callback_plugins(&self.callback_plugin_path)
×
NEW
108
            .await?;
×
109
        for plugin in plugins {
×
110
            self.register_callback(plugin);
×
111
        }
112

113
        self.callbacks_loaded = true;
×
114
        Ok(())
×
115
    }
116

117
    pub async fn emit_event(&self, event: EventType, data: Option<Value>) {
×
118
        if let Some(callbacks) = self.callbacks.get(&event) {
×
119
            // Spawn and collect tasks
120
            let tasks: Vec<_> = callbacks
×
121
                .iter()
122
                .map(|callback| {
×
123
                    let callback = callback.clone();
×
124
                    let event = event.clone();
×
125
                    let data = data.clone();
×
126
                    tokio::spawn(async move {
×
127
                        callback.on_event(&event, data.as_ref()); // Async invocation
×
128
                    })
129
                })
130
                .collect();
131

132
            // Wait for all spawned tasks to complete
133
            for task in tasks {
×
134
                if let Err(err) = task.await {
×
135
                    eprintln!("Callback task panicked: {:?}", err);
×
136
                }
137
            }
138
        }
139
    }
140

141
    pub fn forks(&self) -> usize {
×
142
        self.forks
×
143
    }
144
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc