TaskConfig

Struct TaskConfig 

Source
pub struct TaskConfig {
    pub command: String,
    pub args: Option<Vec<String>>,
    pub working_dir: Option<String>,
    pub env: Option<HashMap<String, String>>,
    pub timeout_ms: Option<u64>,
    pub enable_stdin: Option<bool>,
    pub ready_indicator: Option<String>,
    pub ready_indicator_source: Option<StreamSource>,
    pub use_process_group: Option<bool>,
}
Expand description

Configuration for a task to be executed.

TaskConfig defines all parameters needed to execute a system process securely. It includes the command, arguments, environment setup, timeouts, and monitoring options.

§Examples

§Basic Command

use tcrm_task::tasks::config::TaskConfig;

let config = TaskConfig::new("cmd")
    .args(["/C", "dir", "C:\\"]);

§Complex Configuration

use tcrm_task::tasks::config::{TaskConfig, StreamSource};
use std::collections::HashMap;

let mut env = HashMap::new();
env.insert("PATH".to_string(), "C:\\Windows\\System32".to_string());

let config = TaskConfig::new("cmd")
    .args(["/C", "echo", "Server started"])
    .working_dir("C:\\")
    .env(env)
    .timeout_ms(30000)
    .enable_stdin(true)
    .ready_indicator("Server started")
    .ready_indicator_source(StreamSource::Stdout);

Fields§

§command: String

The command or executable to run

§args: Option<Vec<String>>

Arguments to pass to the command

§working_dir: Option<String>

Working directory for the command

§env: Option<HashMap<String, String>>

Environment variables for the command

§timeout_ms: Option<u64>

Maximum allowed runtime in milliseconds

§enable_stdin: Option<bool>

Allow providing input to the task via stdin

§ready_indicator: Option<String>

Optional string to indicate the task is ready (for long-running processes like servers)

§ready_indicator_source: Option<StreamSource>

Source of the ready indicator string (stdout/stderr)

§use_process_group: Option<bool>

Enable process group management for child process termination (default: true)

When enabled, creates process groups (Unix) or Job Objects (Windows) to ensure all child processes and their descendants are terminated when the main process is killed.

Implementations§

Source§

impl TaskConfig

Source

pub fn new(command: impl Into<String>) -> Self

Create a new task configuration with the given command

§Arguments
  • command - The executable command to run (e.g., “ls”, “node”, “python”)
§Examples
use tcrm_task::tasks::config::TaskConfig;

let config = TaskConfig::new("echo");
let config2 = TaskConfig::new("node".to_string());
Source

pub fn args<I, S>(self, args: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Set the arguments for the command

§Arguments
  • args - Iterator of arguments to pass to the command
§Examples
use tcrm_task::tasks::config::TaskConfig;

let config = TaskConfig::new("ls")
    .args(["-la", "/tmp"]);
     
let config2 = TaskConfig::new("cargo")
    .args(vec!["build", "--release"]);
Source

pub fn working_dir(self, dir: impl Into<String>) -> Self

Set the working directory for the command

The working directory must exist when the task is executed.

§Arguments
  • dir - Path to the working directory
§Examples
use tcrm_task::tasks::config::TaskConfig;

let config = TaskConfig::new("ls")
    .working_dir("/tmp");
     
let config2 = TaskConfig::new("cargo")
    .working_dir("/path/to/project");
Source

pub fn env<K, V, I>(self, env: I) -> Self
where K: Into<String>, V: Into<String>, I: IntoIterator<Item = (K, V)>,

Set environment variables for the command

§Arguments
  • env - Iterator of (key, value) pairs for environment variables
§Examples
use tcrm_task::tasks::config::TaskConfig;
use std::collections::HashMap;

// Using tuples
let config = TaskConfig::new("node")
    .env([("NODE_ENV", "production"), ("PORT", "3000")]);

// Using HashMap
let mut env = HashMap::new();
env.insert("RUST_LOG".to_string(), "debug".to_string());
let config2 = TaskConfig::new("cargo")
    .env(env);
Source

pub fn timeout_ms(self, timeout: u64) -> Self

Set the maximum allowed runtime in milliseconds

If the task runs longer than this timeout, it will be terminated.

§Arguments
  • timeout - Timeout in milliseconds (must be > 0)
§Examples
use tcrm_task::tasks::config::TaskConfig;

// 30 second timeout
let config = TaskConfig::new("long-running-task")
    .timeout_ms(30000);

// 5 minute timeout
let config2 = TaskConfig::new("build-script")
    .timeout_ms(300000);
Source

pub fn enable_stdin(self, b: bool) -> Self

Enable or disable stdin for the task

When enabled, you can send input to the process via the stdin channel.

§Arguments
  • b - Whether to enable stdin input
§Examples
use tcrm_task::tasks::config::TaskConfig;

// Interactive command that needs input
let config = TaskConfig::new("python")
    .args(["-i"])
    .enable_stdin(true);
Source

pub fn ready_indicator(self, indicator: impl Into<String>) -> Self

Set the ready indicator for the task

For long-running processes (like servers), this string indicates when the process is ready to accept requests. When this string appears in the process output, a Ready event will be emitted.

§Arguments
  • indicator - String to look for in process output
§Examples
use tcrm_task::tasks::config::TaskConfig;

let config = TaskConfig::new("my-server")
    .ready_indicator("Server listening on port");

let config2 = TaskConfig::new("database")
    .ready_indicator("Database ready for connections");
Source

pub fn ready_indicator_source(self, source: StreamSource) -> Self

Set the source of the ready indicator

Specifies whether to look for the ready indicator in stdout or stderr.

§Arguments
  • source - Stream source (Stdout or Stderr)
§Examples
use tcrm_task::tasks::config::{TaskConfig, StreamSource};

let config = TaskConfig::new("my-server")
    .ready_indicator("Ready")
    .ready_indicator_source(StreamSource::Stderr);
Source

pub fn use_process_group(self, enabled: bool) -> Self

Enable or disable process group management

When enabled (default), creates process groups on Unix or Job Objects on Windows to ensure all child processes and their descendants are terminated when the main process is killed. This prevents orphaned processes.

§Arguments
  • enabled - Whether to use process group management
§Examples
use tcrm_task::tasks::config::TaskConfig;

// Disable process group management
let config = TaskConfig::new("cmd")
    .use_process_group(false);
     
// Explicitly enable (though it's enabled by default)
let config2 = TaskConfig::new("node")
    .use_process_group(true);
Source

pub fn validate(&self) -> Result<(), TaskError>

Validate the configuration

Validates all configuration parameters. This method should be called before executing the task to ensure safe operation.

§Validation Checks
  • all fields length limits
  • Command: Must not be empty, contain shell injection patterns
  • Arguments: Must not contain null bytes or shell injection patterns
  • Working Directory: Must exist and be a valid directory
  • Environment Variables: Keys must not contain spaces, ‘=’, or null bytes
  • Timeout: Must be greater than 0 if specified
  • Ready Indicator: Must not be empty if specified
§Returns
  • Ok(()) if the configuration is valid
  • Err(TaskError::InvalidConfiguration) with details if validation fails
§Errors

Returns a TaskError if any validation check fails:

§Examples
use tcrm_task::tasks::config::TaskConfig;

// Valid config
let config = TaskConfig::new("echo")
    .args(["hello", "world"]);
assert!(config.validate().is_ok());

// Invalid config (empty command)
let config = TaskConfig::new("");
assert!(config.validate().is_err());

// Invalid config (zero timeout)
let config = TaskConfig::new("sleep")
    .timeout_ms(0);
assert!(config.validate().is_err());
Source

pub fn is_process_group_enabled(&self) -> bool

Check if process group management is enabled

Returns true if process group management should be used, false otherwise. Defaults to true if not explicitly set.

§Examples
use tcrm_task::tasks::config::TaskConfig;

let config = TaskConfig::new("cmd");
assert!(config.is_process_group_enabled()); // Default is true

let config2 = TaskConfig::new("cmd").use_process_group(false);
assert!(!config2.is_process_group_enabled());

Trait Implementations§

Source§

impl Clone for TaskConfig

Source§

fn clone(&self) -> TaskConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TaskConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TaskConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.