Skip to content

Commit 1ac79db

Browse files
authored
Feature: Test run duration (#23)
1 parent b9fd658 commit 1ac79db

File tree

4 files changed

+38
-37
lines changed

4 files changed

+38
-37
lines changed

Cargo.lock

+13-34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

paws_config/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ pyo3 = {"version" = "0.17.3", features = ["auto-initialize"]}
1010
iso8601 = { version = "0.6.1", features = ["serde"] }
1111
serde = { version = "1.0.197", features = ["derive"] }
1212
serde_yaml = "0.9.34"
13+
chrono = "0.4.38"

paws_config/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ impl Duration {
88
pub fn as_std(&self) -> std::time::Duration {
99
self.0
1010
}
11+
12+
pub fn as_chrono(&self) -> chrono::Duration {
13+
chrono::Duration::from_std(self.0).unwrap()
14+
}
15+
1116
}
1217

1318
impl<'de> Deserialize<'de> for Duration {
@@ -57,6 +62,7 @@ pub struct PluginConfig {
5762

5863
#[derive(Debug, Deserialize)]
5964
pub struct KittypawsConfig {
65+
pub duration: Option<Duration>,
6066
pub plugins: Vec<PluginConfig>,
6167
}
6268

src/plug.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ use std::fs;
22
mod bash_plugin;
33
mod python_plugin;
44
use bash_plugin::load as load_sh_plugin;
5+
use chrono::{DateTime, Utc};
56
use python_plugin::load as load_py_plugin;
67

78
use crate::intervals::{time_till_next_run, wait_duration};
89
use crate::stdout_styling::style_line;
9-
use paws_config::{KittypawsConfig, PluginConfig};
10+
use paws_config::{KittypawsConfig, PluginConfig, Duration as ConfigDuration};
1011
use std::collections::HashMap;
1112
use std::path::PathBuf;
1213
use std::thread;
@@ -52,8 +53,17 @@ fn call_plugin(name: &str, plugin: &CallablePlugin, config: &HashMap<String, Str
5253
}
5354
}
5455

55-
fn start_plugin_loop(plugin: CallablePlugin, config: PluginConfig) -> JoinHandle<()> {
56+
fn start_plugin_loop(
57+
plugin: CallablePlugin,
58+
config: PluginConfig,
59+
loop_duration: &Option<ConfigDuration>,
60+
) -> JoinHandle<()> {
5661
let startup = config.startup.into();
62+
let mut deadline: Option<DateTime<Utc>> = None;
63+
64+
if let Some(loop_duration) = loop_duration {
65+
deadline = Some(Utc::now() + loop_duration.as_chrono());
66+
}
5767

5868
thread::spawn(move || {
5969
match startup {
@@ -73,6 +83,11 @@ fn start_plugin_loop(plugin: CallablePlugin, config: PluginConfig) -> JoinHandle
7383
if time_till_next_run(&config.frequency).is_none() {
7484
break;
7585
}
86+
if let Some(deadline) = deadline {
87+
if Utc::now() > deadline {
88+
break;
89+
}
90+
}
7691
}
7792
})
7893
}
@@ -83,7 +98,7 @@ pub fn start_main_loop(config: KittypawsConfig) {
8398
for plugconf in config.plugins {
8499
match load_plugin(&plugconf.name) {
85100
Ok(plugin) => {
86-
let thread = start_plugin_loop(plugin, plugconf);
101+
let thread = start_plugin_loop(plugin, plugconf, &config.duration);
87102

88103
handles.push(thread);
89104
}

0 commit comments

Comments
 (0)