use std::sync::atomic::{AtomicU64, Ordering};
use swarm_engine_core::prelude::*;
pub struct CounterWorker {
id: WorkerId,
name: String,
count: AtomicU64,
}
impl CounterWorker {
pub fn new(id: usize, name: impl Into<String>) -> Self {
Self {
id: WorkerId(id),
name: name.into(),
count: AtomicU64::new(0),
}
}
}
impl WorkerAgent for CounterWorker {
fn think_and_act(&self, _state: &SwarmState, _guidance: Option<&Guidance>) -> WorkResult {
let count = self.count.fetch_add(1, Ordering::Relaxed) + 1;
if count.is_multiple_of(50) {
tracing::debug!(
worker_id = self.id.0 as u64,
action = "count",
success = true,
"worker_action"
);
}
WorkResult::Continuing {
progress: (count as f32 / 100.0).min(1.0),
}
}
fn id(&self) -> WorkerId {
self.id
}
fn name(&self) -> &str {
&self.name
}
}
pub struct SimpleManager {
id: ManagerId,
name: String,
}
impl SimpleManager {
pub fn new() -> Self {
Self {
id: ManagerId(0),
name: "SimpleManager".to_string(),
}
}
}
impl Default for SimpleManager {
fn default() -> Self {
Self::new()
}
}
impl ManagerAgent for SimpleManager {
fn prepare(&self, context: &TaskContext) -> BatchDecisionRequest {
tracing::debug!(tick = context.tick, "Manager preparing batch");
BatchDecisionRequest {
manager_id: self.id,
requests: Vec::new(),
}
}
fn finalize(
&self,
_context: &TaskContext,
_responses: Vec<(WorkerId, DecisionResponse)>,
) -> ManagementDecision {
ManagementDecision::default()
}
fn id(&self) -> ManagerId {
self.id
}
fn name(&self) -> &str {
&self.name
}
}