Skip to main content

fakecloud_batch/
state.rs

1//! In-memory state for AWS Batch. Control-plane resources are JSON-backed
2//! (the raw create input plus generated metadata, echoed verbatim on read),
3//! mirroring the Glue/Athena pattern. Real container-backed job execution
4//! lands in a later batch.
5
6use std::collections::BTreeMap;
7use std::sync::Arc;
8
9use parking_lot::RwLock;
10use serde::{Deserialize, Serialize};
11use serde_json::Value;
12
13pub type SharedBatchState = Arc<RwLock<BatchAccounts>>;
14
15/// A JSON-backed named-resource store: name -> (raw input + generated fields).
16pub type JsonStore = BTreeMap<String, Value>;
17
18#[derive(Debug, Default, Clone, Serialize, Deserialize)]
19pub struct BatchAccounts {
20    pub accounts: BTreeMap<String, BatchState>,
21}
22
23#[derive(Debug, Default, Clone, Serialize, Deserialize)]
24pub struct BatchState {
25    /// Compute environments keyed by name.
26    #[serde(default)]
27    pub compute_environments: JsonStore,
28    /// Job queues keyed by name.
29    #[serde(default)]
30    pub job_queues: JsonStore,
31    /// Job definitions keyed by `name:revision`; the active revision per name
32    /// is resolved at read time.
33    #[serde(default)]
34    pub job_definitions: JsonStore,
35    /// Jobs keyed by jobId.
36    #[serde(default)]
37    pub jobs: JsonStore,
38    /// Fair-share scheduling policies keyed by name.
39    #[serde(default)]
40    pub scheduling_policies: JsonStore,
41    /// Tags keyed by resource ARN -> { key: value }.
42    #[serde(default)]
43    pub tags: BTreeMap<String, BTreeMap<String, String>>,
44    /// Per-job-definition-name highest revision allocated.
45    #[serde(default)]
46    pub job_def_revisions: BTreeMap<String, i64>,
47}
48
49impl BatchAccounts {
50    pub fn new() -> Self {
51        Self::default()
52    }
53
54    pub fn get_or_create(&mut self, account_id: &str) -> &mut BatchState {
55        self.accounts.entry(account_id.to_string()).or_default()
56    }
57
58    pub fn get(&self, account_id: &str) -> Option<&BatchState> {
59        self.accounts.get(account_id)
60    }
61}
62
63/// On-disk snapshot envelope; versioned so format changes fail loudly.
64#[derive(Clone, Serialize, Deserialize)]
65pub struct BatchSnapshot {
66    pub schema_version: u32,
67    #[serde(default)]
68    pub accounts: Option<BatchAccounts>,
69}
70
71pub const BATCH_SNAPSHOT_SCHEMA_VERSION: u32 = 1;