From 3e2ec661a109387aa1485460f0e99a988d0462c7 Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Sun, 28 Jul 2024 18:34:03 +0200 Subject: [PATCH] feat: add executor config --- TODO.md | 3 ++- config.example.yaml | 39 ++++++++++++++++++++++++++----- config.yaml | 8 +++---- src/executor.rs | 7 ++++-- src/models.rs | 56 +++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 96 insertions(+), 17 deletions(-) diff --git a/TODO.md b/TODO.md index b7379f4..6cb150c 100644 --- a/TODO.md +++ b/TODO.md @@ -2,11 +2,12 @@ ## TODO +- [x] Add CSS badge and color code on job status +- [ ] Add tasks timeout - [ ] Support connecting to remote server by SSH to execute task remotely - [ ] Implement basic auth with OAuth2 - [ ] Add a way to categorize tasks, regroup tasks - [ ] Don't use long UUID, but only ids -- [ ] Add CSS badge and color code on job status - [ ] Validating config file - validate schedule CRON syntax - [ ] Add `Dockerfile` and docker-compose example diff --git a/config.example.yaml b/config.example.yaml index aa9ad7d..94a83cc 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -1,17 +1,44 @@ +instance: + name: Example organization + logo_uri: https://example.org/logo.png + +executor: + environment: + SUPER_COOL_DEFAULT_ENV: 438548 + tasks: - - id: do_magic_stuff + do_magic_stuff: name: Do magic incantation - env: + environment: PYTHONUNBUFFERED: "1" - SIMULATION_SPEED: 0.2 + SIMULATION_SPEED: 11 command: - /usr/bin/python3 - - /home/mbess/workspace/autotasker/examples/do_something_1.py - store_logs: true - - id: reindex_db + - /path/to/autotasker/examples/do_something_1.py + + reindex_db: name: Reindex the whole database env: {} command: - ls - /etc/fstab + schedule: + hours: 1 + + clean_up: + name: Clean up things + env: {} + command: + - cat + - /etc/environment + schedule: + "0 * * * * *" + +webhooks: + - id: 1 + name: "Trigger magic stuff" + token: aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee + target_tasks: + - do_magic_stuff + debounce_secs: 10 diff --git a/config.yaml b/config.yaml index af77b1f..af3b884 100644 --- a/config.yaml +++ b/config.yaml @@ -1,11 +1,11 @@ -instance: - name: Example company - logo_uri: https://src.lefuturiste.fr/images/lefuturiste-300-300.png +executor: + environment: + SUPER_COOL_DEFAULT_ENV: 438548 tasks: do_magic_stuff: name: Do magic incantation - env: + environment: PYTHONUNBUFFERED: "1" SIMULATION_SPEED: 11 command: diff --git a/src/executor.rs b/src/executor.rs index be154c2..35012be 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -112,11 +112,14 @@ async fn run_task(state: AppState, order: ExecutorOrder) -> Result<()> { cmd.args(task.command.iter().skip(1).collect::>()) .stdout(Stdio::piped()) .stderr(Stdio::piped()); - for (key, val) in task.env.iter() { + // add OS environment variables from default config and task config + for (key, val) in state.config.executor.environment.iter() { + cmd.env(key, val); + } + for (key, val) in task.environment.iter() { cmd.env(key, val); } let status = execute_process(&state, &order, &mut cmd).await?; - if !status.success() { error!("Non successful exit code found: {}", status); } diff --git a/src/models.rs b/src/models.rs index 6806855..a857ce1 100644 --- a/src/models.rs +++ b/src/models.rs @@ -83,9 +83,13 @@ enum ScheduleConfig { #[fully_pub] struct Task { name: String, + #[serde(default)] description: Option, - env: HashMap, + /// OS environment to add at runtime + #[serde(default)] + environment: HashMap, command: Vec, + #[serde(default)] schedule: Option } @@ -99,11 +103,55 @@ struct ExecutorOrder { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[fully_pub] +/// branding config of the autotasker struct InstanceConfig { name: String, logo_uri: String } +#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] +#[serde(untagged)] +#[fully_pub] +enum LogPolicy { + ErrorsOnly, + HeadAndTailOnly, + #[default] + All +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[fully_pub] +struct ExecutorConfig { + /// the maximum duration of a task run in seconds + #[serde(default)] + execution_timeout: u64, + + /// the maximum heap memory that a task can allocate in KiloBytes + #[serde(default)] + max_memory: u64, + + /// the default filter logs policy to apply on a task output + #[serde(default)] + log_policy: LogPolicy, + + /// default OS environment to add at runtime + #[serde(default)] + environment: HashMap, +} + +impl Default for ExecutorConfig { + fn default() -> ExecutorConfig { + ExecutorConfig { + execution_timeout: 1000, + max_memory: 100_000, + log_policy: LogPolicy::All, + environment: HashMap::from([ + ("EXECUTOR".into(), "autotasker".into()) + ]) + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[fully_pub] struct Webhook { @@ -122,9 +170,9 @@ struct Webhook { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[fully_pub] struct Config { - instance: InstanceConfig, + #[serde(default = "ExecutorConfig::default")] + executor: ExecutorConfig, + instance: Option, tasks: HashMap, webhooks: Option> } - -