app: add sidecar sync commands (export, detect, import, resolve, full export/import)
This commit is contained in:
48
crates/application/src/processing/commands/complete_job.rs
Normal file
48
crates/application/src/processing/commands/complete_job.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::Job,
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
ports::{EventPublisher, JobBatchRepository, JobRepository},
|
||||
value_objects::{DateTimeStamp, StructuredData, SystemId},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct CompleteJobCommand {
|
||||
pub job_id: SystemId,
|
||||
pub result: StructuredData,
|
||||
}
|
||||
|
||||
pub struct CompleteJobHandler {
|
||||
job_repo: Arc<dyn JobRepository>,
|
||||
batch_repo: Arc<dyn JobBatchRepository>,
|
||||
event_pub: Arc<dyn EventPublisher>,
|
||||
}
|
||||
|
||||
impl CompleteJobHandler {
|
||||
pub fn new(
|
||||
job_repo: Arc<dyn JobRepository>,
|
||||
batch_repo: Arc<dyn JobBatchRepository>,
|
||||
event_pub: Arc<dyn EventPublisher>,
|
||||
) -> Self {
|
||||
Self { job_repo, batch_repo, event_pub }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: CompleteJobCommand) -> Result<Job, DomainError> {
|
||||
let mut job = self.job_repo.find_by_id(&cmd.job_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Job {} not found", cmd.job_id)))?;
|
||||
job.complete(cmd.result);
|
||||
self.job_repo.save(&job).await?;
|
||||
if let Some(ref batch_id) = job.batch_id {
|
||||
let mut batch = self.batch_repo.find_by_id(batch_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Batch {} not found", batch_id)))?;
|
||||
batch.record_completion();
|
||||
self.batch_repo.save(&batch).await?;
|
||||
}
|
||||
self.event_pub.publish(DomainEvent::JobCompleted {
|
||||
job_id: job.job_id,
|
||||
timestamp: DateTimeStamp::now(),
|
||||
}).await?;
|
||||
Ok(job)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::ProcessingPipeline,
|
||||
errors::DomainError,
|
||||
ports::{PipelineRepository, PluginRepository},
|
||||
value_objects::{StructuredData, SystemId},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct PipelineStepConfig {
|
||||
pub plugin_id: SystemId,
|
||||
pub config: StructuredData,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct ConfigurePipelineCommand {
|
||||
pub trigger_event: String,
|
||||
pub steps: Vec<PipelineStepConfig>,
|
||||
}
|
||||
|
||||
pub struct ConfigurePipelineHandler {
|
||||
pipeline_repo: Arc<dyn PipelineRepository>,
|
||||
plugin_repo: Arc<dyn PluginRepository>,
|
||||
}
|
||||
|
||||
impl ConfigurePipelineHandler {
|
||||
pub fn new(
|
||||
pipeline_repo: Arc<dyn PipelineRepository>,
|
||||
plugin_repo: Arc<dyn PluginRepository>,
|
||||
) -> Self {
|
||||
Self { pipeline_repo, plugin_repo }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: ConfigurePipelineCommand) -> Result<ProcessingPipeline, DomainError> {
|
||||
for step in &cmd.steps {
|
||||
self.plugin_repo.find_by_id(&step.plugin_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Plugin {} not found", step.plugin_id)))?;
|
||||
}
|
||||
let mut pipeline = ProcessingPipeline::new(cmd.trigger_event);
|
||||
for step in cmd.steps {
|
||||
pipeline.add_step(step.plugin_id, step.config);
|
||||
}
|
||||
self.pipeline_repo.save(&pipeline).await?;
|
||||
Ok(pipeline)
|
||||
}
|
||||
}
|
||||
45
crates/application/src/processing/commands/enqueue_job.rs
Normal file
45
crates/application/src/processing/commands/enqueue_job.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::{Job, JobType},
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
ports::{EventPublisher, JobRepository},
|
||||
value_objects::{DateTimeStamp, StructuredData, SystemId},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct EnqueueJobCommand {
|
||||
pub job_type: JobType,
|
||||
pub priority: u32,
|
||||
pub payload: StructuredData,
|
||||
pub target_asset_id: Option<SystemId>,
|
||||
pub batch_id: Option<SystemId>,
|
||||
}
|
||||
|
||||
pub struct EnqueueJobHandler {
|
||||
job_repo: Arc<dyn JobRepository>,
|
||||
event_pub: Arc<dyn EventPublisher>,
|
||||
}
|
||||
|
||||
impl EnqueueJobHandler {
|
||||
pub fn new(job_repo: Arc<dyn JobRepository>, event_pub: Arc<dyn EventPublisher>) -> Self {
|
||||
Self { job_repo, event_pub }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: EnqueueJobCommand) -> Result<Job, DomainError> {
|
||||
let mut job = Job::new(cmd.job_type.clone(), cmd.priority, cmd.payload);
|
||||
if let Some(id) = cmd.target_asset_id {
|
||||
job = job.with_target(id);
|
||||
}
|
||||
if let Some(id) = cmd.batch_id {
|
||||
job = job.with_batch(id);
|
||||
}
|
||||
self.job_repo.save(&job).await?;
|
||||
self.event_pub.publish(DomainEvent::JobEnqueued {
|
||||
job_id: job.job_id,
|
||||
job_type: format!("{:?}", cmd.job_type),
|
||||
timestamp: DateTimeStamp::now(),
|
||||
}).await?;
|
||||
Ok(job)
|
||||
}
|
||||
}
|
||||
57
crates/application/src/processing/commands/fail_job.rs
Normal file
57
crates/application/src/processing/commands/fail_job.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::{Job, JobStatus},
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
ports::{EventPublisher, JobBatchRepository, JobRepository},
|
||||
value_objects::{DateTimeStamp, SystemId},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct FailJobCommand {
|
||||
pub job_id: SystemId,
|
||||
pub error: String,
|
||||
}
|
||||
|
||||
pub struct FailJobHandler {
|
||||
job_repo: Arc<dyn JobRepository>,
|
||||
batch_repo: Arc<dyn JobBatchRepository>,
|
||||
event_pub: Arc<dyn EventPublisher>,
|
||||
}
|
||||
|
||||
impl FailJobHandler {
|
||||
pub fn new(
|
||||
job_repo: Arc<dyn JobRepository>,
|
||||
batch_repo: Arc<dyn JobBatchRepository>,
|
||||
event_pub: Arc<dyn EventPublisher>,
|
||||
) -> Self {
|
||||
Self { job_repo, batch_repo, event_pub }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: FailJobCommand) -> Result<Job, DomainError> {
|
||||
let mut job = self.job_repo.find_by_id(&cmd.job_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Job {} not found", cmd.job_id)))?;
|
||||
job.fail(&cmd.error);
|
||||
self.job_repo.save(&job).await?;
|
||||
if job.status == JobStatus::Failed {
|
||||
if let Some(ref batch_id) = job.batch_id {
|
||||
let mut batch = self.batch_repo.find_by_id(batch_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Batch {} not found", batch_id)))?;
|
||||
batch.record_failure();
|
||||
self.batch_repo.save(&batch).await?;
|
||||
}
|
||||
self.event_pub.publish(DomainEvent::JobFailed {
|
||||
job_id: job.job_id,
|
||||
error: cmd.error,
|
||||
timestamp: DateTimeStamp::now(),
|
||||
}).await?;
|
||||
} else if job.status == JobStatus::Queued {
|
||||
self.event_pub.publish(DomainEvent::JobEnqueued {
|
||||
job_id: job.job_id,
|
||||
job_type: format!("{:?}", job.job_type),
|
||||
timestamp: DateTimeStamp::now(),
|
||||
}).await?;
|
||||
}
|
||||
Ok(job)
|
||||
}
|
||||
}
|
||||
63
crates/application/src/processing/commands/manage_plugin.rs
Normal file
63
crates/application/src/processing/commands/manage_plugin.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::{Plugin, PluginType},
|
||||
errors::DomainError,
|
||||
ports::PluginRepository,
|
||||
value_objects::{StructuredData, SystemId},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub enum PluginAction {
|
||||
Create {
|
||||
name: String,
|
||||
plugin_type: PluginType,
|
||||
config: StructuredData,
|
||||
},
|
||||
Enable,
|
||||
Disable,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct ManagePluginCommand {
|
||||
pub plugin_id: Option<SystemId>,
|
||||
pub action: PluginAction,
|
||||
}
|
||||
|
||||
pub struct ManagePluginHandler {
|
||||
plugin_repo: Arc<dyn PluginRepository>,
|
||||
}
|
||||
|
||||
impl ManagePluginHandler {
|
||||
pub fn new(plugin_repo: Arc<dyn PluginRepository>) -> Self {
|
||||
Self { plugin_repo }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: ManagePluginCommand) -> Result<Plugin, DomainError> {
|
||||
match cmd.action {
|
||||
PluginAction::Create { name, plugin_type, config } => {
|
||||
let mut plugin = Plugin::new(name, plugin_type);
|
||||
plugin.configuration = config;
|
||||
self.plugin_repo.save(&plugin).await?;
|
||||
Ok(plugin)
|
||||
}
|
||||
PluginAction::Enable => {
|
||||
let id = cmd.plugin_id
|
||||
.ok_or_else(|| DomainError::Validation("plugin_id required for Enable".into()))?;
|
||||
let mut plugin = self.plugin_repo.find_by_id(&id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Plugin {} not found", id)))?;
|
||||
plugin.enable();
|
||||
self.plugin_repo.save(&plugin).await?;
|
||||
Ok(plugin)
|
||||
}
|
||||
PluginAction::Disable => {
|
||||
let id = cmd.plugin_id
|
||||
.ok_or_else(|| DomainError::Validation("plugin_id required for Disable".into()))?;
|
||||
let mut plugin = self.plugin_repo.find_by_id(&id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Plugin {} not found", id)))?;
|
||||
plugin.disable();
|
||||
self.plugin_repo.save(&plugin).await?;
|
||||
Ok(plugin)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
6
crates/application/src/processing/commands/mod.rs
Normal file
6
crates/application/src/processing/commands/mod.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
pub mod enqueue_job;
|
||||
pub mod start_job;
|
||||
pub mod complete_job;
|
||||
pub mod fail_job;
|
||||
pub mod manage_plugin;
|
||||
pub mod configure_pipeline;
|
||||
30
crates/application/src/processing/commands/start_job.rs
Normal file
30
crates/application/src/processing/commands/start_job.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::Job,
|
||||
errors::DomainError,
|
||||
ports::JobRepository,
|
||||
value_objects::SystemId,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct StartJobCommand {
|
||||
pub job_id: SystemId,
|
||||
}
|
||||
|
||||
pub struct StartJobHandler {
|
||||
job_repo: Arc<dyn JobRepository>,
|
||||
}
|
||||
|
||||
impl StartJobHandler {
|
||||
pub fn new(job_repo: Arc<dyn JobRepository>) -> Self {
|
||||
Self { job_repo }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: StartJobCommand) -> Result<Job, DomainError> {
|
||||
let mut job = self.job_repo.find_by_id(&cmd.job_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Job {} not found", cmd.job_id)))?;
|
||||
job.start()?;
|
||||
self.job_repo.save(&job).await?;
|
||||
Ok(job)
|
||||
}
|
||||
}
|
||||
@@ -1 +1,10 @@
|
||||
// Processing commands/queries (future: EnqueueJob, ProcessBatch, etc.)
|
||||
pub mod commands;
|
||||
pub mod queries;
|
||||
|
||||
pub use commands::enqueue_job::{EnqueueJobCommand, EnqueueJobHandler};
|
||||
pub use commands::start_job::{StartJobCommand, StartJobHandler};
|
||||
pub use commands::complete_job::{CompleteJobCommand, CompleteJobHandler};
|
||||
pub use commands::fail_job::{FailJobCommand, FailJobHandler};
|
||||
pub use commands::manage_plugin::{ManagePluginCommand, ManagePluginHandler, PluginAction};
|
||||
pub use commands::configure_pipeline::{ConfigurePipelineCommand, ConfigurePipelineHandler, PipelineStepConfig};
|
||||
pub use queries::report_batch_progress::{ReportBatchProgressQuery, ReportBatchProgressHandler, BatchProgress};
|
||||
|
||||
1
crates/application/src/processing/queries/mod.rs
Normal file
1
crates/application/src/processing/queries/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod report_batch_progress;
|
||||
@@ -0,0 +1,36 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::{Job, JobBatch},
|
||||
errors::DomainError,
|
||||
ports::{JobBatchRepository, JobRepository},
|
||||
value_objects::SystemId,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct ReportBatchProgressQuery {
|
||||
pub batch_id: SystemId,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BatchProgress {
|
||||
pub batch: JobBatch,
|
||||
pub jobs: Vec<Job>,
|
||||
}
|
||||
|
||||
pub struct ReportBatchProgressHandler {
|
||||
batch_repo: Arc<dyn JobBatchRepository>,
|
||||
job_repo: Arc<dyn JobRepository>,
|
||||
}
|
||||
|
||||
impl ReportBatchProgressHandler {
|
||||
pub fn new(batch_repo: Arc<dyn JobBatchRepository>, job_repo: Arc<dyn JobRepository>) -> Self {
|
||||
Self { batch_repo, job_repo }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, query: ReportBatchProgressQuery) -> Result<BatchProgress, DomainError> {
|
||||
let batch = self.batch_repo.find_by_id(&query.batch_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Batch {} not found", query.batch_id)))?;
|
||||
let jobs = self.job_repo.find_by_batch(&query.batch_id).await?;
|
||||
Ok(BatchProgress { batch, jobs })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user