Skip to content

Commit

Permalink
refactor: add utility methods to Resources in the docker backend. (#15
Browse files Browse the repository at this point in the history
)

The new methods makes downstream code a little neater.
  • Loading branch information
peterhuene authored Feb 17, 2025
1 parent f2184d2 commit 5c32b29
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions crankshaft-engine/src/service/runner/backend/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,46 @@ pub enum Resources {
}

impl Resources {
/// Gets the number of nodes.
pub fn nodes(&self) -> usize {
match self {
Self::Local(_) => 1,
Self::Swarm(resources) => resources.nodes,
}
}

/// Gets the total CPUs available.
pub fn cpu(&self) -> u64 {
match self {
Self::Local(resources) => resources.cpu,
Self::Swarm(resources) => resources.cpu,
}
}

/// Gets the total memory available, in bytes.
pub fn memory(&self) -> u64 {
match self {
Self::Local(resources) => resources.memory,
Self::Swarm(resources) => resources.memory,
}
}

/// Gets the maximum CPUs available for a single node.
pub fn max_cpu(&self) -> u64 {
match self {
Self::Local(resources) => resources.cpu,
Self::Swarm(resources) => resources.max_cpu,
}
}

/// Gets the maximum memory available for a single node, in bytes.
pub fn max_memory(&self) -> u64 {
match self {
Self::Local(resources) => resources.memory,
Self::Swarm(resources) => resources.max_memory,
}
}

/// Determines if the docker backend will use a service instead of a
/// container based on the resources available.
///
Expand Down

0 comments on commit 5c32b29

Please sign in to comment.