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

dariusbakunas / cogrs / 13444881106

20 Feb 2025 09:19PM UTC coverage: 37.361% (+0.9%) from 36.493%
13444881106

push

github

dariusbakunas
feat: add merge option for variable combination

2 of 9 new or added lines in 3 files covered. (22.22%)

126 existing lines in 6 files now uncovered.

640 of 1713 relevant lines covered (37.36%)

1.33 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::sync::Arc;
14

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

24
const DEFAULT_FORKS: usize = 5;
25

26
impl TaskQueueManager {
UNCOV
27
    pub fn new(forks: Option<usize>) -> Self {
×
28
        Self {
29
            callbacks: HashMap::new(),
×
30
            callbacks_loaded: false,
31
            forks: forks.unwrap_or(DEFAULT_FORKS),
×
32
            terminated: false,
33
            unreachable_hosts: HashMap::new(),
×
UNCOV
34
            workers: Vec::with_capacity(forks.unwrap_or(DEFAULT_FORKS)),
×
35
        }
36
    }
37

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

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

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

UNCOV
60
        self.emit_event(EventType::PlaybookOnPlayStart, None).await;
×
61

UNCOV
62
        let strategy = *play.strategy();
×
63

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

67
        self.forks = min(self.forks, play_iterator.batch_size());
×
68

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

79
        Ok(())
×
80
    }
81

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

91
    pub fn get_unreachable_hosts(&self) -> &HashMap<String, Host> {
×
UNCOV
92
        &self.unreachable_hosts
×
93
    }
94

UNCOV
95
    pub fn is_terminated(&self) -> bool {
×
UNCOV
96
        self.terminated
×
97
    }
98

99
    async fn load_callbacks(&mut self) -> Result<()> {
×
UNCOV
100
        let plugin_loader = cogrs_plugins::plugin_loader::PluginLoader::instance();
×
UNCOV
101
        let loader = plugin_loader.lock().await;
×
102

103
        let plugins = loader.get_callback_plugins().await?;
×
UNCOV
104
        for plugin in plugins {
×
UNCOV
105
            self.register_callback(plugin);
×
106
        }
107

108
        self.callbacks_loaded = true;
×
UNCOV
109
        Ok(())
×
110
    }
111

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

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

136
    pub fn forks(&self) -> usize {
×
137
        self.forks
×
138
    }
139
}
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