hammerwork 2.4.0

A high-performance, database-driven job queue for Rust with PostgreSQL and MySQL support, featuring job prioritization, cron scheduling, event streaming (Kafka/Kinesis/PubSub), webhooks, rate limiting, Prometheus metrics, and comprehensive monitoring
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
//! Dynamic job spawning functionality for creating child jobs from parent jobs.
//!
//! This module provides the [`SpawnHandler`] trait and related types that enable jobs
//! to dynamically create other jobs during their execution. This is particularly useful
//! for fan-out processing patterns where a single job needs to spawn multiple child jobs.

use crate::{
    Result,
    error::HammerworkError,
    job::{Job, JobId},
    queue::DatabaseQueue,
};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// Result of a spawn operation containing information about spawned jobs.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpawnResult {
    /// The parent job that performed the spawn operation
    pub parent_job_id: JobId,
    /// List of spawned child jobs
    pub spawned_jobs: Vec<JobId>,
    /// When the spawn operation occurred
    pub spawned_at: DateTime<Utc>,
    /// Optional spawn operation identifier for tracking
    pub spawn_operation_id: Option<String>,
}

/// Configuration for spawn operations.
///
/// Controls how jobs are spawned, including limits, inheritance settings,
/// and operational metadata.
///
/// # Examples
///
/// ## Basic Configuration
///
/// ```rust
/// use hammerwork::spawn::SpawnConfig;
///
/// // Use default configuration
/// let config = SpawnConfig::default();
/// assert_eq!(config.max_spawn_count, Some(100));
/// assert!(config.inherit_priority);
/// assert!(config.inherit_retry_strategy);
/// assert!(!config.inherit_timeout);
/// assert!(config.inherit_trace_context);
/// ```
///
/// ## Custom Configuration for File Processing
///
/// ```rust
/// use hammerwork::spawn::SpawnConfig;
///
/// let config = SpawnConfig {
///     max_spawn_count: Some(50),        // Limit to 50 files
///     inherit_priority: true,           // Inherit parent priority
///     inherit_retry_strategy: true,     // Inherit retry settings
///     inherit_timeout: false,           // Each file has own timeout
///     inherit_trace_context: true,      // Maintain tracing
///     operation_id: Some("file_batch_001".to_string()),
/// };
/// ```
///
/// ## Configuration for Large Data Processing
///
/// ```rust
/// use hammerwork::spawn::SpawnConfig;
///
/// let config = SpawnConfig {
///     max_spawn_count: Some(1000),      // Allow many chunks
///     inherit_priority: false,          // Let chunks be normal priority
///     inherit_retry_strategy: true,     // Inherit retry logic
///     inherit_timeout: true,            // Inherit timeout settings
///     inherit_trace_context: true,      // Maintain trace correlation
///     operation_id: Some("data_processing_2024_01".to_string()),
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpawnConfig {
    /// Maximum number of child jobs that can be spawned by a single parent
    pub max_spawn_count: Option<usize>,
    /// Whether spawned jobs should inherit the parent's priority
    pub inherit_priority: bool,
    /// Whether spawned jobs should inherit the parent's retry strategy
    pub inherit_retry_strategy: bool,
    /// Whether spawned jobs should inherit the parent's timeout
    pub inherit_timeout: bool,
    /// Whether spawned jobs should inherit the parent's trace context
    pub inherit_trace_context: bool,
    /// Custom spawn operation identifier
    pub operation_id: Option<String>,
}

impl Default for SpawnConfig {
    fn default() -> Self {
        Self {
            max_spawn_count: Some(100), // Reasonable default limit
            inherit_priority: true,
            inherit_retry_strategy: true,
            inherit_timeout: false, // Timeout is usually job-specific
            inherit_trace_context: true,
            operation_id: None,
        }
    }
}

/// Context provided to spawn handlers with information about the parent job.
pub struct SpawnContext<DB: sqlx::Database> {
    /// The parent job that is spawning child jobs
    pub parent_job: Job,
    /// Configuration for the spawn operation
    pub config: SpawnConfig,
    /// Reference to the job queue for enqueuing spawned jobs
    pub queue: Arc<dyn DatabaseQueue<Database = DB> + Send + Sync>,
}

/// Error types specific to spawn operations.
#[derive(Debug, thiserror::Error)]
pub enum SpawnError {
    #[error("Spawn limit exceeded: attempted to spawn {attempted} jobs, limit is {limit}")]
    SpawnLimitExceeded { attempted: usize, limit: usize },

    #[error("Invalid spawn configuration: {message}")]
    InvalidConfig { message: String },

    #[error("Parent job {parent_id} is not eligible for spawning")]
    ParentNotEligible { parent_id: JobId },

    #[error("Spawn operation failed: {message}")]
    SpawnOperationFailed { message: String },
}

/// Trait for handling dynamic job spawning logic.
///
/// Implement this trait to define how a job should spawn child jobs during execution.
/// The spawn handler receives the parent job's context and returns a list of child jobs
/// to be enqueued.
///
/// # Examples
///
/// ## Basic File Processing Spawner
///
/// ```rust,no_run
/// use async_trait::async_trait;
/// use serde_json::json;
/// use hammerwork::{Job, spawn::{SpawnHandler, SpawnContext}};
///
/// struct FileProcessingSpawner;
///
/// #[async_trait]
/// impl<DB: sqlx::Database + Send + Sync> SpawnHandler<DB> for FileProcessingSpawner {
///     async fn spawn_jobs(&self, context: SpawnContext<DB>) -> hammerwork::Result<Vec<Job>> {
///         let files = context.parent_job.payload["files"].as_array()
///             .ok_or_else(|| hammerwork::error::HammerworkError::InvalidJobPayload {
///                 message: "Missing files array in payload".to_string()
///             })?;
///
///         let mut child_jobs = Vec::new();
///         for file in files {
///             let child_job = Job::new(
///                 "process_file".to_string(),
///                 json!({
///                     "file_path": file,
///                     "parent_job_id": context.parent_job.id
///                 })
///             );
///             child_jobs.push(child_job);
///         }
///
///         Ok(child_jobs)
///     }
/// }
/// ```
///
/// ## Data Chunking Spawner with Validation
///
/// This example shows how to split large datasets into smaller processing chunks:
///
/// ```rust,no_run
/// use async_trait::async_trait;
/// use serde_json::json;
/// use hammerwork::{Job, spawn::{SpawnHandler, SpawnContext, SpawnConfig}};
///
/// struct DataChunkingSpawner;
///
/// #[async_trait]
/// impl<DB: sqlx::Database + Send + Sync> SpawnHandler<DB> for DataChunkingSpawner {
///     async fn spawn_jobs(&self, context: SpawnContext<DB>) -> hammerwork::Result<Vec<Job>> {
///         // Break large data processing into manageable chunks
///         let total_records = context.parent_job.payload["total_records"].as_u64().unwrap_or(0) as usize;
///         let chunk_size = context.parent_job.payload["chunk_size"].as_u64().unwrap_or(1000) as usize;
///
///         let mut child_jobs = Vec::new();
///         let mut offset = 0;
///
///         while offset < total_records {
///             let limit = std::cmp::min(chunk_size, total_records - offset);
///             
///             let child_job = Job::new(
///                 "process_chunk".to_string(),
///                 json!({
///                     "offset": offset,
///                     "limit": limit,
///                     "parent_job_id": context.parent_job.id
///                 })
///             );
///             child_jobs.push(child_job);
///             offset += chunk_size;
///         }
///         
///         Ok(child_jobs)
///     }
/// }
/// ```
#[async_trait]
pub trait SpawnHandler<DB: sqlx::Database>: Send + Sync {
    /// Generate child jobs based on the parent job's context.
    ///
    /// This method is called when a job with spawn capabilities completes successfully.
    /// It should analyze the parent job's payload and create appropriate child jobs.
    ///
    /// # Arguments
    ///
    /// * `context` - Context containing the parent job and spawn configuration
    ///
    /// # Returns
    ///
    /// A vector of child jobs to be enqueued, or an error if spawning fails.
    async fn spawn_jobs(&self, context: SpawnContext<DB>) -> Result<Vec<Job>>;

    /// Optional validation method called before spawning jobs.
    ///
    /// This method can be used to validate the parent job's payload or perform
    /// any pre-spawn checks. The default implementation always returns `Ok(())`.
    ///
    /// # Arguments
    ///
    /// * `parent_job` - The parent job that wants to spawn children
    /// * `config` - The spawn configuration
    ///
    /// # Returns
    ///
    /// `Ok(())` if validation passes, or an error if spawning should be prevented.
    async fn validate_spawn(&self, _parent_job: &Job, _config: &SpawnConfig) -> Result<()> {
        Ok(())
    }

    /// Optional post-spawn callback called after child jobs are enqueued.
    ///
    /// This method can be used to perform cleanup or additional processing
    /// after the spawn operation completes. The default implementation does nothing.
    ///
    /// # Arguments
    ///
    /// * `result` - Information about the completed spawn operation
    async fn on_spawn_complete(&self, _result: &SpawnResult) -> Result<()> {
        Ok(())
    }
}

/// A spawn handler that creates jobs based on a simple closure.
pub struct ClosureSpawnHandler<F, DB: sqlx::Database> {
    handler: F,
    _phantom: std::marker::PhantomData<DB>,
}

impl<F, DB> ClosureSpawnHandler<F, DB>
where
    F: Fn(SpawnContext<DB>) -> Result<Vec<Job>> + Send + Sync,
    DB: sqlx::Database + Send + Sync,
{
    /// Create a new closure-based spawn handler.
    pub fn new(handler: F) -> Self {
        Self {
            handler,
            _phantom: std::marker::PhantomData,
        }
    }
}

#[async_trait]
impl<F, DB> SpawnHandler<DB> for ClosureSpawnHandler<F, DB>
where
    F: Fn(SpawnContext<DB>) -> Result<Vec<Job>> + Send + Sync,
    DB: sqlx::Database + Send + Sync,
{
    async fn spawn_jobs(&self, context: SpawnContext<DB>) -> Result<Vec<Job>> {
        (self.handler)(context)
    }
}

/// Manager for registering and executing spawn handlers.
///
/// The SpawnManager coordinates job spawning by mapping job types to their corresponding
/// spawn handlers. It handles the registration of handlers, validation, and execution
/// of spawn operations.
///
/// # Examples
///
/// ## Basic Usage
///
/// ```rust,no_run
/// use hammerwork::spawn::SpawnManager;
/// use async_trait::async_trait;
/// use serde_json::json;
/// use hammerwork::{Job, spawn::{SpawnHandler, SpawnContext}};
/// use std::sync::Arc;
///
/// struct SimpleSpawner;
///
/// #[async_trait]
/// impl<DB: sqlx::Database + Send + Sync> SpawnHandler<DB> for SimpleSpawner {
///     async fn spawn_jobs(&self, context: SpawnContext<DB>) -> hammerwork::Result<Vec<Job>> {
///         let count = context.parent_job.payload["count"].as_u64().unwrap_or(1) as usize;
///         let mut jobs = Vec::new();
///         for i in 0..count {
///             jobs.push(Job::new("child_task".to_string(), json!({"index": i, "parent": context.parent_job.id})));
///         }
///         Ok(jobs)
///     }
/// }
///
/// // Create and configure spawn manager
/// # #[cfg(feature = "postgres")]
/// # {
/// let mut spawn_manager: SpawnManager<sqlx::Postgres> = SpawnManager::new();
/// spawn_manager.register_handler("parent_task", SimpleSpawner);
///
/// // Check if handler is registered
/// assert!(spawn_manager.has_handler("parent_task"));
/// assert!(!spawn_manager.has_handler("other_task"));
///
/// // Get registered types
/// let types = spawn_manager.registered_types();
/// assert!(types.contains(&"parent_task".to_string()));
/// # }
/// ```
///
/// ## Integration with Worker
///
/// ```rust,no_run
/// use hammerwork::{spawn::SpawnManager, Worker};
/// use async_trait::async_trait;
/// use hammerwork::spawn::{SpawnHandler, SpawnContext};
/// use std::sync::Arc;
///
/// struct TaskSpawner;
///
/// #[async_trait]
/// impl<DB: sqlx::Database + Send + Sync> SpawnHandler<DB> for TaskSpawner {
///     async fn spawn_jobs(&self, context: SpawnContext<DB>) -> hammerwork::Result<Vec<hammerwork::Job>> {
///         // Implementation here
///         Ok(vec![])
///     }
/// }
///
/// // Set up spawn manager with handlers
/// # #[cfg(feature = "postgres")]
/// # {
/// let mut spawn_manager: SpawnManager<sqlx::Postgres> = SpawnManager::new();
/// spawn_manager.register_handler("spawning_task", TaskSpawner);
/// # }
///
/// // In real usage, integrate with Worker:
/// // let worker = Worker::new(queue, "spawning_task".to_string(), handler)
/// //     .with_spawn_manager(Arc::new(spawn_manager));
/// ```
pub struct SpawnManager<DB: sqlx::Database> {
    handlers: std::collections::HashMap<String, Arc<dyn SpawnHandler<DB>>>,
    _phantom: std::marker::PhantomData<DB>,
}

impl<DB: sqlx::Database> SpawnManager<DB> {
    /// Create a new spawn manager.
    pub fn new() -> Self {
        Self {
            handlers: std::collections::HashMap::new(),
            _phantom: std::marker::PhantomData,
        }
    }

    /// Register a spawn handler for a specific job type.
    ///
    /// # Arguments
    ///
    /// * `job_type` - The job type identifier (typically the queue name)
    /// * `handler` - The spawn handler implementation
    pub fn register_handler<H>(&mut self, job_type: impl Into<String>, handler: H)
    where
        H: SpawnHandler<DB> + 'static,
    {
        self.handlers.insert(job_type.into(), Arc::new(handler));
    }

    /// Execute spawn logic for a completed job.
    ///
    /// This method checks if the job has a registered spawn handler and executes it
    /// if found. The spawned jobs are enqueued one at a time, outside any transaction;
    /// [`Worker`](crate::Worker)s instead use [`prepare_spawn`](Self::prepare_spawn) and
    /// enqueue the children together with the parent's completion
    /// ([`DatabaseQueue::complete_job_run_with_children`]).
    ///
    /// # Arguments
    ///
    /// * `job` - The parent job that completed successfully
    /// * `config` - Spawn configuration
    /// * `queue` - Reference to the job queue for enqueuing child jobs
    ///
    /// # Returns
    ///
    /// `Some(SpawnResult)` if jobs were spawned, `None` if no spawn handler was found.
    pub async fn execute_spawn(
        &self,
        job: Job,
        config: SpawnConfig,
        queue: Arc<dyn DatabaseQueue<Database = DB> + Send + Sync>,
    ) -> Result<Option<SpawnResult>> {
        let Some(child_jobs) = self.prepare_spawn(&job, &config, queue.clone()).await? else {
            return Ok(None);
        };

        // Enqueue child jobs
        let mut spawned_job_ids = Vec::new();
        for child_job in child_jobs {
            let job_id = queue.enqueue(child_job).await?;
            spawned_job_ids.push(job_id);
        }

        let spawn_result = SpawnResult {
            parent_job_id: job.id,
            spawned_jobs: spawned_job_ids,
            spawned_at: Utc::now(),
            spawn_operation_id: config.operation_id.clone(),
        };
        self.notify_spawn_complete(&job, &spawn_result).await?;
        Ok(Some(spawn_result))
    }

    /// Build the child jobs `job` spawns, without enqueueing them.
    ///
    /// Runs the job type's handler (`validate_spawn`, then `spawn_jobs`), enforces
    /// `config.max_spawn_count` and applies the inheritance settings. Every child
    /// depends on the parent and joins its workflow. Returns `None` when no handler is
    /// registered for the job's queue.
    ///
    /// Workers call this before recording the parent's completion and then enqueue the
    /// children in the same transaction as the completion
    /// ([`DatabaseQueue::complete_job_run_with_children`]); an error here fails the
    /// parent's run, so it is retried instead of completing without its children.
    pub async fn prepare_spawn(
        &self,
        job: &Job,
        config: &SpawnConfig,
        queue: Arc<dyn DatabaseQueue<Database = DB> + Send + Sync>,
    ) -> Result<Option<Vec<Job>>> {
        let Some(handler) = self.handlers.get(&job.queue_name) else {
            return Ok(None);
        };
        // Validate spawn operation
        handler.validate_spawn(job, config).await?;

        // Generate child jobs
        let context = SpawnContext {
            parent_job: job.clone(),
            config: config.clone(),
            queue,
        };
        let mut child_jobs = handler.spawn_jobs(context).await?;

        // Check spawn limits
        if let Some(max_count) = config.max_spawn_count
            && child_jobs.len() > max_count
        {
            return Err(HammerworkError::SpawnError(
                SpawnError::SpawnLimitExceeded {
                    attempted: child_jobs.len(),
                    limit: max_count,
                },
            ));
        }

        // Apply inheritance settings. An optional setting is inherited when the parent
        // has one, so a parent without it never wipes the child's own.
        fn inherit<T: Clone>(child: &mut Option<T>, parent: &Option<T>) {
            if parent.is_some() {
                child.clone_from(parent);
            }
        }
        for child_job in &mut child_jobs {
            if config.inherit_priority {
                child_job.priority = job.priority;
            }
            if config.inherit_retry_strategy {
                inherit(&mut child_job.retry_strategy, &job.retry_strategy);
            }
            if config.inherit_timeout {
                inherit(&mut child_job.timeout, &job.timeout);
            }
            if config.inherit_trace_context {
                inherit(&mut child_job.trace_id, &job.trace_id);
                inherit(&mut child_job.correlation_id, &job.correlation_id);
                inherit(&mut child_job.parent_span_id, &job.parent_span_id);
                inherit(&mut child_job.span_context, &job.span_context);
            }

            // Set up parent-child relationship: every child depends on the parent, in
            // addition to any dependencies of its own.
            if !child_job.depends_on.contains(&job.id) {
                child_job.depends_on.push(job.id);
            }
            child_job.workflow_id = job.workflow_id;
            child_job.workflow_name = job.workflow_name.clone();
        }

        Ok(Some(child_jobs))
    }

    /// Call the post-spawn callback (`on_spawn_complete`) of `parent`'s handler, once
    /// its children were enqueued.
    pub async fn notify_spawn_complete(&self, parent: &Job, result: &SpawnResult) -> Result<()> {
        match self.handlers.get(&parent.queue_name) {
            Some(handler) => handler.on_spawn_complete(result).await,
            None => Ok(()),
        }
    }

    /// Check if a job type has a registered spawn handler.
    pub fn has_handler(&self, job_type: &str) -> bool {
        self.handlers.contains_key(job_type)
    }

    /// Get the list of registered job types with spawn handlers.
    pub fn registered_types(&self) -> Vec<String> {
        self.handlers.keys().cloned().collect()
    }
}

impl<DB: sqlx::Database> Default for SpawnManager<DB> {
    fn default() -> Self {
        Self::new()
    }
}

/// Extension trait for adding spawn capabilities to jobs.
///
/// This trait provides convenience methods for configuring jobs to spawn
/// child jobs when they complete successfully.
///
/// # Examples
///
/// ## Basic Job Spawning
///
/// ```rust
/// use hammerwork::{Job, spawn::{JobSpawnExt, SpawnConfig}};
/// use serde_json::json;
///
/// // Create a job that will spawn children with default config
/// let job = Job::new("file_batch".to_string(), json!({"files": ["a.txt", "b.txt"]}))
///     .with_spawning()?;
///
/// // Check that spawn config was added to payload
/// assert!(job.payload.get("_spawn_config").is_some());
/// # Ok::<(), hammerwork::HammerworkError>(())
/// ```
///
/// ## Custom Spawn Configuration
///
/// ```rust
/// use hammerwork::{Job, spawn::{JobSpawnExt, SpawnConfig}};
/// use serde_json::json;
///
/// let spawn_config = SpawnConfig {
///     max_spawn_count: Some(10),
///     inherit_priority: true,
///     inherit_retry_strategy: false,
///     inherit_timeout: true,
///     inherit_trace_context: true,
///     operation_id: Some("batch_001".to_string()),
/// };
///
/// let job = Job::new("data_processing".to_string(), json!({"records": 5000}))
///     .as_high_priority()
///     .with_spawn_config(spawn_config)?;
///
/// // Verify the configuration is stored
/// let stored_config = job.payload.get("_spawn_config").unwrap();
/// let parsed_config: SpawnConfig = serde_json::from_value(stored_config.clone()).unwrap();
/// assert_eq!(parsed_config.max_spawn_count, Some(10));
/// # Ok::<(), hammerwork::HammerworkError>(())
/// ```
///
/// ## Fan-out Processing Pattern
///
/// ```rust
/// use hammerwork::{Job, spawn::JobSpawnExt};
/// use serde_json::json;
///
/// // Parent job that will spawn one child job per user
/// let user_ids = vec![1, 2, 3, 4, 5];
/// let notification_job = Job::new(
///     "send_notifications".to_string(),
///     json!({"user_ids": user_ids})
/// )
/// .as_high_priority()
/// .with_spawning()?; // Uses default spawn configuration
///
/// // When this job completes, the spawn handler will create
/// // individual notification jobs for each user
/// # Ok::<(), hammerwork::HammerworkError>(())
/// ```
///
/// ## Payload requirement
///
/// The configuration lives in the payload, so a non-object payload is rejected
/// instead of silently dropping the configuration:
///
/// ```rust
/// use hammerwork::{Job, spawn::JobSpawnExt};
/// use serde_json::json;
///
/// let job = Job::new("queue".to_string(), json!([1, 2, 3]));
/// assert!(job.with_spawning().is_err());
/// ```
pub trait JobSpawnExt {
    /// Enable spawning for this job with the given configuration.
    ///
    /// The configuration is stored in the job payload under
    /// [`SPAWN_CONFIG_KEY`] (`"_spawn_config"`), so the payload must be a JSON object.
    ///
    /// # Errors
    ///
    /// Returns [`HammerworkError::InvalidJobPayload`] if the payload is not a JSON object
    /// (the configuration would have nowhere to live), or a serialization error if the
    /// configuration cannot be serialized. The job is never silently left without its
    /// spawn configuration.
    fn with_spawn_config(self, config: SpawnConfig) -> Result<Self>
    where
        Self: Sized;

    /// Enable spawning for this job with default configuration.
    ///
    /// # Errors
    ///
    /// Same as [`JobSpawnExt::with_spawn_config`].
    fn with_spawning(self) -> Result<Self>
    where
        Self: Sized;
}

impl JobSpawnExt for Job {
    fn with_spawn_config(mut self, config: SpawnConfig) -> Result<Self> {
        let value = serde_json::to_value(config)?;
        let kind = json_kind(&self.payload);
        let payload_obj =
            self.payload
                .as_object_mut()
                .ok_or_else(|| HammerworkError::InvalidJobPayload {
                    message: format!(
                        "a job's spawn configuration is stored in its payload under \
                         `{SPAWN_CONFIG_KEY}`, so the payload must be a JSON object (got {kind})"
                    ),
                })?;
        payload_obj.insert(SPAWN_CONFIG_KEY.to_string(), value);
        Ok(self)
    }

    fn with_spawning(self) -> Result<Self> {
        self.with_spawn_config(SpawnConfig::default())
    }
}

/// The payload key under which a job's [`SpawnConfig`] is stored.
///
/// The job schema has no dedicated spawn column, so the configuration travels in the
/// payload. A worker reads it from the job's *decrypted* payload, so spawning works for
/// encrypted jobs too; the stored (encrypted) row, however, does not expose the key to SQL,
/// which means `cargo hammerwork spawn` queries only see jobs whose payload is stored in
/// the clear.
pub const SPAWN_CONFIG_KEY: &str = "_spawn_config";

fn json_kind(value: &serde_json::Value) -> &'static str {
    match value {
        serde_json::Value::Null => "null",
        serde_json::Value::Bool(_) => "a boolean",
        serde_json::Value::Number(_) => "a number",
        serde_json::Value::String(_) => "a string",
        serde_json::Value::Array(_) => "an array",
        serde_json::Value::Object(_) => "an object",
    }
}

/// Read the spawn configuration out of a job payload.
///
/// Returns `Ok(None)` when the payload carries no [`SPAWN_CONFIG_KEY`], and an error when the
/// key is present but is not a valid [`SpawnConfig`], so a misconfigured job is reported
/// instead of silently never spawning.
pub fn spawn_config_from_payload(payload: &serde_json::Value) -> Result<Option<SpawnConfig>> {
    match payload.get(SPAWN_CONFIG_KEY) {
        None => Ok(None),
        Some(value) => serde_json::from_value::<SpawnConfig>(value.clone())
            .map(Some)
            .map_err(|e| HammerworkError::InvalidJobPayload {
                message: format!("invalid `{SPAWN_CONFIG_KEY}` in job payload: {e}"),
            }),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    struct TestSpawnHandler;

    #[async_trait]
    impl<DB: sqlx::Database> SpawnHandler<DB> for TestSpawnHandler {
        async fn spawn_jobs(&self, context: SpawnContext<DB>) -> Result<Vec<Job>> {
            let count = context.parent_job.payload["spawn_count"]
                .as_u64()
                .unwrap_or(1) as usize;
            let mut jobs = Vec::new();

            for i in 0..count {
                let job = Job::new(
                    "child_task".to_string(),
                    json!({
                        "index": i,
                        "parent_id": context.parent_job.id
                    }),
                );
                jobs.push(job);
            }

            Ok(jobs)
        }
    }

    #[tokio::test]
    async fn test_spawn_handler_basic() {
        let _handler = TestSpawnHandler;
        let _parent_job = Job::new("parent_task".to_string(), json!({"spawn_count": 3}));

        // Note: This test would need a mock queue implementation
        // We'll implement proper tests when we add the queue integration
    }

    #[test]
    fn test_spawn_config_defaults() {
        let config = SpawnConfig::default();
        assert_eq!(config.max_spawn_count, Some(100));
        assert!(config.inherit_priority);
        assert!(config.inherit_retry_strategy);
        assert!(!config.inherit_timeout);
        assert!(config.inherit_trace_context);
    }

    #[cfg(feature = "postgres")]
    #[test]
    fn test_spawn_manager_registration() {
        let mut manager: SpawnManager<sqlx::Postgres> = SpawnManager::new();
        assert!(!manager.has_handler("test_job"));

        manager.register_handler("test_job", TestSpawnHandler);
        assert!(manager.has_handler("test_job"));

        let types = manager.registered_types();
        assert!(types.contains(&"test_job".to_string()));
    }

    #[test]
    fn test_with_spawn_config_stores_config_in_object_payload() {
        let job = Job::new("q".to_string(), json!({"a": 1}))
            .with_spawn_config(SpawnConfig {
                max_spawn_count: Some(7),
                operation_id: Some("op".to_string()),
                ..SpawnConfig::default()
            })
            .unwrap();
        assert_eq!(job.payload["a"], 1);
        let config = spawn_config_from_payload(&job.payload).unwrap().unwrap();
        assert_eq!(config.max_spawn_count, Some(7));
        assert_eq!(config.operation_id.as_deref(), Some("op"));

        let job = Job::new("q".to_string(), json!({}))
            .with_spawning()
            .unwrap();
        assert!(job.payload.get(SPAWN_CONFIG_KEY).is_some());
    }

    #[test]
    fn test_with_spawn_config_rejects_non_object_payloads() {
        for payload in [
            json!(null),
            json!(3),
            json!("text"),
            json!([1, 2]),
            json!(true),
        ] {
            let job = Job::new("q".to_string(), payload.clone());
            let err = job.with_spawning().unwrap_err();
            assert!(
                matches!(err, HammerworkError::InvalidJobPayload { .. }),
                "{payload}: {err}"
            );
            assert!(err.to_string().contains("must be a JSON object"), "{err}");
        }
    }

    #[test]
    fn test_spawn_config_from_payload() {
        assert!(spawn_config_from_payload(&json!({})).unwrap().is_none());
        assert!(spawn_config_from_payload(&json!("x")).unwrap().is_none());
        let err = spawn_config_from_payload(&json!({"_spawn_config": {"max_spawn_count": "many"}}))
            .unwrap_err();
        assert!(matches!(err, HammerworkError::InvalidJobPayload { .. }));
        assert!(err.to_string().contains("_spawn_config"));
    }

    #[cfg(all(feature = "test", feature = "postgres"))]
    struct OwnSettingsSpawner {
        sibling: crate::JobId,
    }

    #[cfg(all(feature = "test", feature = "postgres"))]
    #[async_trait]
    impl<DB: sqlx::Database> SpawnHandler<DB> for OwnSettingsSpawner {
        async fn spawn_jobs(&self, _context: SpawnContext<DB>) -> Result<Vec<Job>> {
            Ok(vec![
                Job::new("child".to_string(), json!({}))
                    .with_retry_strategy(crate::retry::RetryStrategy::fixed(
                        std::time::Duration::from_secs(7),
                    ))
                    .with_timeout(std::time::Duration::from_secs(9))
                    .with_trace_id("child-trace")
                    .depends_on(&self.sibling),
            ])
        }
    }

    /// A parent without a retry strategy, timeout or trace id no longer wipes the
    /// child's own (a parent that has one still overrides it), and the child keeps its
    /// own dependencies in addition to the parent.
    #[cfg(all(feature = "test", feature = "postgres"))]
    #[tokio::test]
    async fn test_prepare_spawn_keeps_the_childs_own_settings() {
        use crate::queue::test::TestQueue;
        let sibling = uuid::Uuid::new_v4();
        let mut manager = SpawnManager::<sqlx::Postgres>::new();
        manager.register_handler("parent", OwnSettingsSpawner { sibling });
        let parent = Job::new("parent".to_string(), json!({}));
        assert!(parent.retry_strategy.is_none() && parent.trace_id.is_none());
        let config = SpawnConfig {
            inherit_timeout: true,
            ..Default::default()
        };
        let queue: Arc<dyn DatabaseQueue<Database = sqlx::Postgres> + Send + Sync> =
            Arc::new(TestQueue::new());
        let children = manager
            .prepare_spawn(&parent, &config, queue)
            .await
            .unwrap()
            .unwrap();
        let child = &children[0];
        assert_eq!(
            child.retry_strategy,
            Some(crate::retry::RetryStrategy::fixed(
                std::time::Duration::from_secs(7)
            ))
        );
        assert_eq!(child.timeout, Some(std::time::Duration::from_secs(9)));
        assert_eq!(child.trace_id.as_deref(), Some("child-trace"));
        assert!(child.depends_on.contains(&parent.id));
        assert!(child.depends_on.contains(&sibling));

        // A child without settings of its own still inherits the parent's.
        let parent = Job::new("parent".to_string(), json!({}))
            .with_retry_strategy(crate::retry::RetryStrategy::fixed(
                std::time::Duration::from_secs(3),
            ))
            .with_trace_id("parent-trace");
        let mut manager = SpawnManager::<sqlx::Postgres>::new();
        manager.register_handler("parent", TestSpawnHandler);
        let queue: Arc<dyn DatabaseQueue<Database = sqlx::Postgres> + Send + Sync> =
            Arc::new(TestQueue::new());
        let children = manager
            .prepare_spawn(&parent, &SpawnConfig::default(), queue)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(children[0].retry_strategy, parent.retry_strategy);
        assert_eq!(children[0].trace_id.as_deref(), Some("parent-trace"));
        assert_eq!(children[0].depends_on, vec![parent.id]);
    }
}