oximedia-batch 0.1.8

Comprehensive batch processing engine for OxiMedia
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
//! Comprehensive examples of batch processing usage

#![allow(dead_code)]

use crate::job::{BatchJob, BatchOperation, InputSpec, OutputSpec, PipelineStep};
use crate::operations::{AnalysisType, FileOperation, OutputFormat};
use crate::types::{Priority, ResourceRequirements, RetryPolicy, Schedule};
use crate::{BatchEngine, Result};
use std::path::PathBuf;
use std::sync::Arc;

/// Example: Basic file copy job
///
/// This example demonstrates how to create a simple job that copies files
/// from one location to another.
///
/// # Errors
///
/// Returns an error if job submission fails
pub async fn example_basic_copy(engine: &Arc<BatchEngine>) -> Result<()> {
    let mut job = BatchJob::new(
        "Copy Media Files".to_string(),
        BatchOperation::FileOp {
            operation: FileOperation::Copy { overwrite: false },
        },
    );

    // Add input specification
    job.add_input(InputSpec::new("*.mp4".to_string()).recursive(false));

    // Add output specification
    job.add_output(OutputSpec::new(
        "/output/{filename}".to_string(),
        OutputFormat::Mp4,
    ));

    // Set priority
    job.set_priority(Priority::Normal);

    // Submit the job
    let job_id = engine.submit_job(job).await?;
    tracing::info!("Submitted copy job: {}", job_id);

    Ok(())
}

/// Example: Transcoding with multiple outputs
///
/// This example shows how to transcode a video file to multiple formats
/// and qualities simultaneously.
///
/// # Errors
///
/// Returns an error if job submission fails
pub async fn example_multi_output_transcode(engine: &Arc<BatchEngine>) -> Result<()> {
    let mut job = BatchJob::new(
        "Multi-Output Transcode".to_string(),
        BatchOperation::Transcode {
            preset: "web".to_string(),
        },
    );

    // Input
    job.add_input(InputSpec::new("source.mp4".to_string()));

    // Multiple outputs
    job.add_output(OutputSpec::new(
        "output_1080p.mp4".to_string(),
        OutputFormat::Mp4,
    ));
    job.add_output(OutputSpec::new(
        "output_720p.mp4".to_string(),
        OutputFormat::Mp4,
    ));
    job.add_output(OutputSpec::new(
        "output_480p.mp4".to_string(),
        OutputFormat::Mp4,
    ));

    // Set high priority for important transcode jobs
    job.set_priority(Priority::High);

    // Set retry policy for reliability
    job.set_retry_policy(RetryPolicy::new(3, 60, true));

    // Set resource requirements
    job.set_resources(ResourceRequirements {
        cpu_cores: Some(4),
        memory_mb: Some(4096),
        gpu: true,
        disk_space_mb: Some(10240),
    });

    let job_id = engine.submit_job(job).await?;
    tracing::info!("Submitted multi-output transcode job: {}", job_id);

    Ok(())
}

/// Example: Quality control check
///
/// This example demonstrates running automated quality control checks
/// on media files.
///
/// # Errors
///
/// Returns an error if job submission fails
pub async fn example_quality_control(engine: &Arc<BatchEngine>) -> Result<()> {
    let mut job = BatchJob::new(
        "QC Check".to_string(),
        BatchOperation::QualityCheck {
            profile: "full".to_string(),
        },
    );

    // Input pattern
    job.add_input(
        InputSpec::new("**/*.mp4".to_string())
            .recursive(true)
            .with_base_dir(PathBuf::from("/incoming")),
    );

    // Metadata
    job.add_metadata("project".to_string(), "production-2024".to_string());
    job.add_metadata("client".to_string(), "ACME Corp".to_string());

    let job_id = engine.submit_job(job).await?;
    tracing::info!("Submitted QC job: {}", job_id);

    Ok(())
}

/// Example: Scheduled batch job
///
/// This example shows how to schedule a job to run at a specific time.
///
/// # Errors
///
/// Returns an error if job submission fails
///
/// # Panics
///
/// Panics if the scheduled time cannot be constructed (invalid date arithmetic).
pub async fn example_scheduled_job(engine: &Arc<BatchEngine>) -> Result<()> {
    let mut job = BatchJob::new(
        "Nightly Archive".to_string(),
        BatchOperation::FileOp {
            operation: FileOperation::Archive {
                format: crate::operations::file_ops::ArchiveFormat::TarGz,
                compression: 9,
            },
        },
    );

    job.add_input(InputSpec::new("daily_files/*".to_string()));
    job.add_output(OutputSpec::new(
        "archive_{date}.tar.gz".to_string(),
        OutputFormat::Custom("tar.gz".to_string()),
    ));

    // Schedule for tomorrow at 2 AM
    let tomorrow = chrono::Utc::now() + chrono::Duration::days(1);
    let scheduled_time = tomorrow
        .date_naive()
        .and_hms_opt(2, 0, 0)
        .ok_or_else(|| crate::error::BatchError::InvalidJobConfig("invalid time 02:00:00".into()))?
        .and_utc();

    job.set_schedule(Schedule::At(scheduled_time));

    let job_id = engine.submit_job(job).await?;
    tracing::info!("Submitted scheduled job: {}", job_id);

    Ok(())
}

/// Example: Recurring batch job
///
/// This example demonstrates creating a recurring job that runs on a schedule.
///
/// # Errors
///
/// Returns an error if job submission fails
pub async fn example_recurring_job(engine: &Arc<BatchEngine>) -> Result<()> {
    let mut job = BatchJob::new(
        "Hourly Cleanup".to_string(),
        BatchOperation::FileOp {
            operation: FileOperation::Delete { confirm: true },
        },
    );

    job.add_input(InputSpec::new("temp/*.tmp".to_string()));

    // Run every hour
    job.set_schedule(Schedule::Recurring {
        expression: "0 * * * *".to_string(), // Cron expression
    });

    let job_id = engine.submit_job(job).await?;
    tracing::info!("Submitted recurring job: {}", job_id);

    Ok(())
}

/// Example: Job with dependencies
///
/// This example shows how to create jobs that depend on other jobs completing first.
///
/// # Errors
///
/// Returns an error if job submission fails
pub async fn example_job_dependencies(engine: &Arc<BatchEngine>) -> Result<()> {
    // Job 1: Download files
    let mut job1 = BatchJob::new(
        "Download Files".to_string(),
        BatchOperation::FileOp {
            operation: FileOperation::Copy { overwrite: false },
        },
    );
    job1.add_input(InputSpec::new("source/*.mp4".to_string()));
    let job1_id = engine.submit_job(job1).await?;

    // Job 2: Transcode (depends on job1)
    let mut job2 = BatchJob::new(
        "Transcode".to_string(),
        BatchOperation::Transcode {
            preset: "web".to_string(),
        },
    );
    job2.add_dependency(job1_id.clone());
    let job2_id = engine.submit_job(job2).await?;

    // Job 3: QC Check (depends on job2)
    let mut job3 = BatchJob::new(
        "QC Check".to_string(),
        BatchOperation::QualityCheck {
            profile: "quick".to_string(),
        },
    );
    job3.add_dependency(job2_id.clone());
    let job3_id = engine.submit_job(job3).await?;

    tracing::info!(
        "Submitted job chain: {} -> {} -> {}",
        job1_id,
        job2_id,
        job3_id
    );

    Ok(())
}

/// Example: Multi-step pipeline
///
/// This example demonstrates creating a complex pipeline with multiple steps.
///
/// # Errors
///
/// Returns an error if job submission fails
pub async fn example_pipeline(engine: &Arc<BatchEngine>) -> Result<()> {
    let pipeline_steps = vec![
        PipelineStep {
            name: "Copy Input".to_string(),
            operation: BatchOperation::FileOp {
                operation: FileOperation::Copy { overwrite: false },
            },
            continue_on_error: false,
            condition: None,
        },
        PipelineStep {
            name: "Transcode".to_string(),
            operation: BatchOperation::Transcode {
                preset: "web".to_string(),
            },
            continue_on_error: false,
            condition: Some("file_size > 1048576".to_string()),
        },
        PipelineStep {
            name: "QC Check".to_string(),
            operation: BatchOperation::QualityCheck {
                profile: "full".to_string(),
            },
            continue_on_error: true,
            condition: None,
        },
        PipelineStep {
            name: "Create Archive".to_string(),
            operation: BatchOperation::FileOp {
                operation: FileOperation::Archive {
                    format: crate::operations::file_ops::ArchiveFormat::Zip,
                    compression: 6,
                },
            },
            continue_on_error: false,
            condition: None,
        },
    ];

    let mut job = BatchJob::new(
        "Complete Pipeline".to_string(),
        BatchOperation::Pipeline {
            steps: pipeline_steps,
        },
    );

    job.add_input(InputSpec::new("*.mp4".to_string()));
    job.set_priority(Priority::High);

    let job_id = engine.submit_job(job).await?;
    tracing::info!("Submitted pipeline job: {}", job_id);

    Ok(())
}

/// Example: Batch analysis jobs
///
/// This example shows how to run various analysis operations on media files.
///
/// # Errors
///
/// Returns an error if job submission fails
pub async fn example_batch_analysis(engine: &Arc<BatchEngine>) -> Result<()> {
    // Scene detection
    let mut scene_job = BatchJob::new(
        "Scene Detection".to_string(),
        BatchOperation::Analyze {
            analysis_type: AnalysisType::SceneDetection,
        },
    );
    scene_job.add_input(InputSpec::new("*.mp4".to_string()));
    let scene_id = engine.submit_job(scene_job).await?;

    // Black frame detection
    let mut black_job = BatchJob::new(
        "Black Frame Detection".to_string(),
        BatchOperation::Analyze {
            analysis_type: AnalysisType::BlackFrameDetection,
        },
    );
    black_job.add_input(InputSpec::new("*.mp4".to_string()));
    let black_id = engine.submit_job(black_job).await?;

    // Audio loudness measurement
    let mut loudness_job = BatchJob::new(
        "Loudness Measurement".to_string(),
        BatchOperation::Analyze {
            analysis_type: AnalysisType::LoudnessMeasurement,
        },
    );
    loudness_job.add_input(InputSpec::new("*.mp4".to_string()));
    let loudness_id = engine.submit_job(loudness_job).await?;

    tracing::info!(
        "Submitted analysis jobs: {}, {}, {}",
        scene_id,
        black_id,
        loudness_id
    );

    Ok(())
}

/// Example: Archive creation with multiple files
///
/// This example shows how to create archives from multiple source files.
///
/// # Errors
///
/// Returns an error if job submission fails
pub async fn example_archive_creation(engine: &Arc<BatchEngine>) -> Result<()> {
    let mut job = BatchJob::new(
        "Create Project Archive".to_string(),
        BatchOperation::FileOp {
            operation: FileOperation::Archive {
                format: crate::operations::file_ops::ArchiveFormat::Zip,
                compression: 9,
            },
        },
    );

    // Include multiple file patterns
    job.add_input(InputSpec::new("project/**/*.mp4".to_string()).recursive(true));
    job.add_input(InputSpec::new("project/**/*.xml".to_string()).recursive(true));
    job.add_input(InputSpec::new("project/**/*.aaf".to_string()).recursive(true));

    job.add_output(OutputSpec::new(
        "project_{date}.zip".to_string(),
        OutputFormat::Custom("zip".to_string()),
    ));

    let job_id = engine.submit_job(job).await?;
    tracing::info!("Submitted archive job: {}", job_id);

    Ok(())
}

/// Example: File verification with checksums
///
/// This example demonstrates calculating and verifying file checksums.
///
/// # Errors
///
/// Returns an error if job submission fails
pub async fn example_checksum_verification(engine: &Arc<BatchEngine>) -> Result<()> {
    let mut job = BatchJob::new(
        "Calculate Checksums".to_string(),
        BatchOperation::FileOp {
            operation: FileOperation::Checksum {
                algorithm: crate::operations::file_ops::HashAlgorithm::Sha256,
            },
        },
    );

    job.add_input(InputSpec::new("deliverables/*.mp4".to_string()));

    // Output checksum files
    job.add_output(OutputSpec::new(
        "{filename}.sha256".to_string(),
        OutputFormat::Custom("txt".to_string()),
    ));

    let job_id = engine.submit_job(job).await?;
    tracing::info!("Submitted checksum job: {}", job_id);

    Ok(())
}

/// Example: Broadcast delivery workflow
///
/// This example shows a complete workflow for broadcast delivery.
///
/// # Errors
///
/// Returns an error if job submission fails
pub async fn example_broadcast_workflow(engine: &Arc<BatchEngine>) -> Result<()> {
    // Step 1: QC check
    let mut qc_job = BatchJob::new(
        "Broadcast QC".to_string(),
        BatchOperation::QualityCheck {
            profile: "broadcast".to_string(),
        },
    );
    qc_job.add_input(InputSpec::new("master.mxf".to_string()));
    qc_job.set_priority(Priority::High);
    let qc_id = engine.submit_job(qc_job).await?;

    // Step 2: Transcode to broadcast format
    let mut transcode_job = BatchJob::new(
        "Broadcast Transcode".to_string(),
        BatchOperation::Transcode {
            preset: "broadcast".to_string(),
        },
    );
    transcode_job.add_dependency(qc_id.clone());
    transcode_job.add_output(OutputSpec::new(
        "broadcast_{date}.mxf".to_string(),
        OutputFormat::Mxf,
    ));
    transcode_job.set_priority(Priority::High);
    let transcode_id = engine.submit_job(transcode_job).await?;

    // Step 3: Generate checksums
    let mut checksum_job = BatchJob::new(
        "Generate Checksums".to_string(),
        BatchOperation::FileOp {
            operation: FileOperation::Checksum {
                algorithm: crate::operations::file_ops::HashAlgorithm::Sha256,
            },
        },
    );
    checksum_job.add_dependency(transcode_id.clone());
    let checksum_id = engine.submit_job(checksum_job).await?;

    tracing::info!(
        "Submitted broadcast workflow: QC {} -> Transcode {} -> Checksum {}",
        qc_id,
        transcode_id,
        checksum_id
    );

    Ok(())
}

/// Example: Web delivery workflow
///
/// This example shows a workflow for web platform delivery.
///
/// # Errors
///
/// Returns an error if job submission fails
pub async fn example_web_delivery(engine: &Arc<BatchEngine>) -> Result<()> {
    let mut job = BatchJob::new(
        "Web Delivery".to_string(),
        BatchOperation::Transcode {
            preset: "web".to_string(),
        },
    );

    job.add_input(InputSpec::new("source.mp4".to_string()));

    // Multiple web formats
    job.add_output(
        OutputSpec::new("web_1080p.mp4".to_string(), OutputFormat::Mp4)
            .with_option("resolution".to_string(), "1920x1080".to_string())
            .with_option("bitrate".to_string(), "5000k".to_string()),
    );

    job.add_output(
        OutputSpec::new("web_720p.mp4".to_string(), OutputFormat::Mp4)
            .with_option("resolution".to_string(), "1280x720".to_string())
            .with_option("bitrate".to_string(), "2500k".to_string()),
    );

    job.add_output(
        OutputSpec::new("web_480p.mp4".to_string(), OutputFormat::Mp4)
            .with_option("resolution".to_string(), "854x480".to_string())
            .with_option("bitrate".to_string(), "1000k".to_string()),
    );

    job.set_priority(Priority::Normal);
    job.add_metadata("platform".to_string(), "web".to_string());

    let job_id = engine.submit_job(job).await?;
    tracing::info!("Submitted web delivery job: {}", job_id);

    Ok(())
}

/// Example: Mobile delivery workflow
///
/// This example shows a workflow optimized for mobile delivery.
///
/// # Errors
///
/// Returns an error if job submission fails
pub async fn example_mobile_delivery(engine: &Arc<BatchEngine>) -> Result<()> {
    let mut job = BatchJob::new(
        "Mobile Delivery".to_string(),
        BatchOperation::Transcode {
            preset: "mobile".to_string(),
        },
    );

    job.add_input(InputSpec::new("source.mp4".to_string()));

    // Mobile-optimized output
    job.add_output(
        OutputSpec::new("mobile.mp4".to_string(), OutputFormat::Mp4)
            .with_option("resolution".to_string(), "640x360".to_string())
            .with_option("bitrate".to_string(), "500k".to_string())
            .with_option("profile".to_string(), "baseline".to_string()),
    );

    job.set_priority(Priority::Normal);
    job.add_metadata("platform".to_string(), "mobile".to_string());

    let job_id = engine.submit_job(job).await?;
    tracing::info!("Submitted mobile delivery job: {}", job_id);

    Ok(())
}

/// Example: Social media workflow
///
/// This example shows workflows for various social media platforms.
///
/// # Errors
///
/// Returns an error if job submission fails
pub async fn example_social_media(engine: &Arc<BatchEngine>) -> Result<()> {
    // YouTube
    let mut youtube_job = BatchJob::new(
        "YouTube Upload".to_string(),
        BatchOperation::Transcode {
            preset: "youtube".to_string(),
        },
    );
    youtube_job.add_input(InputSpec::new("source.mp4".to_string()));
    youtube_job.add_output(
        OutputSpec::new("youtube.mp4".to_string(), OutputFormat::Mp4)
            .with_option("resolution".to_string(), "1920x1080".to_string())
            .with_option("bitrate".to_string(), "8000k".to_string()),
    );
    let youtube_id = engine.submit_job(youtube_job).await?;

    // Instagram
    let mut instagram_job = BatchJob::new(
        "Instagram Post".to_string(),
        BatchOperation::Transcode {
            preset: "instagram".to_string(),
        },
    );
    instagram_job.add_input(InputSpec::new("source.mp4".to_string()));
    instagram_job.add_output(
        OutputSpec::new("instagram.mp4".to_string(), OutputFormat::Mp4)
            .with_option("resolution".to_string(), "1080x1080".to_string())
            .with_option("bitrate".to_string(), "3500k".to_string()),
    );
    let instagram_id = engine.submit_job(instagram_job).await?;

    // TikTok
    let mut tiktok_job = BatchJob::new(
        "TikTok Video".to_string(),
        BatchOperation::Transcode {
            preset: "tiktok".to_string(),
        },
    );
    tiktok_job.add_input(InputSpec::new("source.mp4".to_string()));
    tiktok_job.add_output(
        OutputSpec::new("tiktok.mp4".to_string(), OutputFormat::Mp4)
            .with_option("resolution".to_string(), "1080x1920".to_string())
            .with_option("bitrate".to_string(), "2000k".to_string()),
    );
    let tiktok_id = engine.submit_job(tiktok_job).await?;

    tracing::info!(
        "Submitted social media jobs: YouTube {}, Instagram {}, TikTok {}",
        youtube_id,
        instagram_id,
        tiktok_id
    );

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    async fn create_test_engine() -> (Arc<BatchEngine>, TempDir) {
        let temp_dir = TempDir::new().expect("failed to create temp dir");
        let db_path = temp_dir.path().join("test.db");
        let db_path_str = db_path
            .to_str()
            .expect("path should be valid UTF-8")
            .to_string();
        (
            Arc::new(BatchEngine::new(&db_path_str, 2).expect("failed to create")),
            temp_dir,
        )
    }

    #[tokio::test]
    async fn test_basic_copy_example() {
        let (engine, _dir) = create_test_engine().await;
        let result = example_basic_copy(&engine).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_quality_control_example() {
        let (engine, _dir) = create_test_engine().await;
        let result = example_quality_control(&engine).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_scheduled_job_example() {
        let (engine, _dir) = create_test_engine().await;
        let result = example_scheduled_job(&engine).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_pipeline_example() {
        let (engine, _dir) = create_test_engine().await;
        let result = example_pipeline(&engine).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_batch_analysis_example() {
        let (engine, _dir) = create_test_engine().await;
        let result = example_batch_analysis(&engine).await;
        assert!(result.is_ok());
    }
}