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

dariusbakunas / cogrs / 13418091285

19 Feb 2025 04:58PM UTC coverage: 38.013% (-0.2%) from 38.241%
13418091285

push

github

dariusbakunas
feat: introduce plugin loader singleton and move callback loading into it

0 of 31 new or added lines in 2 files covered. (0.0%)

1 existing line in 1 file now uncovered.

639 of 1681 relevant lines covered (38.01%)

1.25 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 cogrs_plugins::plugin_type::PluginType;
11
use serde_json::Value;
12
use std::cmp::min;
13
use std::collections::HashMap;
14
use std::sync::Arc;
15
use tokio::sync::Mutex;
16

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

26
const DEFAULT_FORKS: usize = 5;
27

28
impl TaskQueueManager {
29
    pub fn new(forks: Option<usize>) -> 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)),
×
37
        }
38
    }
39

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

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

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

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

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

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

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

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

81
        Ok(())
×
82
    }
83

84
    pub fn register_callback(&mut self, callback: Box<dyn CallbackPlugin>) {
×
85
        let callback: Arc<dyn CallbackPlugin> = Arc::from(callback);
×
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

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

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

111
        self.callbacks_loaded = true;
×
112
        Ok(())
×
113
    }
114

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

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

139
    pub fn forks(&self) -> usize {
×
140
        self.forks
×
141
    }
142
}
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