conductor-sdk 0.2.0

Rust SDK for Netflix Conductor workflow orchestration
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
// Copyright {{.Year}} Conductor OSS
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

use std::collections::HashSet;
use std::panic::AssertUnwindSafe;
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use futures::FutureExt as _;
use tokio::sync::Semaphore;
use tracing::{debug, error, info, warn};

use crate::client::TaskClient;
use crate::configuration::{resolve_worker_config, WorkerConfig};
use crate::error::Result;
use crate::events::{
    exception_label, EventDispatcher, PollCompleted, PollFailure, PollSkippedPaused, PollStarted,
    TaskExecutionCompleted, TaskExecutionFailure, TaskExecutionStarted, TaskUpdateCompleted,
    TaskUpdateFailure, ThreadUncaughtException,
};
use crate::models::Task;

use super::{Worker, WorkerOutput};

/// Result of a task execution attempt, including panics.
///
/// Returned by [`TaskRunner::execute_catching_panic`] so the caller can
/// handle success, regular errors, and panics without accessing any
/// state that was inside the `AssertUnwindSafe` boundary.
enum TaskOutcome {
    Ok,
    Err(crate::error::ConductorError),
    Panic(String),
}

/// Task runner for a single worker type.
pub struct TaskRunner {
    worker: Arc<dyn Worker>,
    task_client: TaskClient,
    config: Arc<WorkerConfig>,
    event_dispatcher: EventDispatcher,

    // Control state
    running: Arc<AtomicBool>,
    paused: Arc<AtomicBool>,

    // Polling state
    consecutive_empty_polls: Arc<AtomicU64>,
    last_poll_time: Arc<parking_lot::Mutex<Instant>>,

    // Concurrency control - use atomic counter instead of HashSet for better performance
    semaphore: Arc<Semaphore>,
    /// Count of tasks currently being executed (after semaphore acquired).
    active_task_count: Arc<AtomicUsize>,
    /// Set of task IDs currently in flight (for debugging/monitoring).
    running_tasks: Arc<parking_lot::Mutex<HashSet<String>>>,
    /// Count of spawned tasks (including those waiting for semaphore).
    spawned_task_count: Arc<AtomicUsize>,
    /// Count of real poll attempts (successful or failed) made against the server so far. Used
    /// by [`super::TaskHandler::verify_workers_started`] to tell "the polling loop hasn't
    /// attempted a single real poll yet" apart from "the loop started but is legitimately
    /// waiting" -- [`TaskRunner::is_running`] alone can't distinguish those, since it flips
    /// `true` before the first poll ever happens.
    poll_attempts: Arc<AtomicU64>,
}

impl TaskRunner {
    /// Create a new task runner.
    pub fn new(
        worker: Arc<dyn Worker>,
        task_client: TaskClient,
        event_dispatcher: EventDispatcher,
    ) -> Self {
        // Resolve configuration from environment
        let defaults = WorkerConfig {
            task_definition_name: worker.task_definition_name().to_owned(),
            poll_interval: Duration::from_millis(worker.poll_interval_millis()),
            domain: worker.domain().map(std::borrow::ToOwned::to_owned),
            worker_id: worker.identity(),
            thread_count: worker.thread_count(),
            ..Default::default()
        };

        let config = resolve_worker_config(worker.task_definition_name(), defaults);

        info!(
            task_type = %config.task_definition_name,
            worker_id = %config.worker_id,
            thread_count = config.thread_count,
            poll_interval_ms = config.poll_interval.as_millis(),
            domain = ?config.domain,
            paused = config.paused,
            "Task runner initialized"
        );

        let paused = config.paused;
        let thread_count = config.thread_count;

        Self {
            worker,
            task_client,
            config: Arc::new(config),
            event_dispatcher,
            running: Arc::new(AtomicBool::new(false)),
            paused: Arc::new(AtomicBool::new(paused)),
            consecutive_empty_polls: Arc::new(AtomicU64::new(0)),
            last_poll_time: Arc::new(parking_lot::Mutex::new(Instant::now())),
            semaphore: Arc::new(Semaphore::new(thread_count)),
            active_task_count: Arc::new(AtomicUsize::new(0)),
            running_tasks: Arc::new(parking_lot::Mutex::new(HashSet::new())),
            spawned_task_count: Arc::new(AtomicUsize::new(0)),
            poll_attempts: Arc::new(AtomicU64::new(0)),
        }
    }

    /// Get the task type this runner handles.
    #[must_use]
    pub fn task_type(&self) -> &str {
        &self.config.task_definition_name
    }

    /// Get the worker configuration.
    #[must_use]
    pub fn config(&self) -> &WorkerConfig {
        &self.config
    }

    /// Get the number of currently active tasks (executing, not waiting for semaphore).
    #[must_use]
    pub fn active_task_count(&self) -> usize {
        self.active_task_count.load(Ordering::SeqCst)
    }

    /// Get the number of spawned tasks (including those waiting for semaphore).
    #[must_use]
    pub fn spawned_task_count(&self) -> usize {
        self.spawned_task_count.load(Ordering::SeqCst)
    }

    /// Get the number of real poll attempts (successful or failed) made against the server so
    /// far. Zero until the first one completes -- see the field's doc comment for why this
    /// exists separately from [`TaskRunner::is_running`].
    #[must_use]
    pub fn poll_attempt_count(&self) -> u64 {
        self.poll_attempts.load(Ordering::SeqCst)
    }

    /// Check if the runner is running.
    #[must_use]
    pub fn is_running(&self) -> bool {
        self.running.load(Ordering::SeqCst)
    }

    /// Check if the runner is paused.
    #[must_use]
    pub fn is_paused(&self) -> bool {
        self.paused.load(Ordering::SeqCst)
    }

    /// Pause the runner.
    pub fn pause(&self) {
        self.paused.store(true, Ordering::SeqCst);
        info!(task_type = %self.config.task_definition_name, "Task runner paused");
    }

    /// Resume the runner.
    pub fn resume(&self) {
        self.paused.store(false, Ordering::SeqCst);
        info!(task_type = %self.config.task_definition_name, "Task runner resumed");
    }

    /// Stop the runner.
    pub fn stop(&self) {
        self.running.store(false, Ordering::SeqCst);
        info!(task_type = %self.config.task_definition_name, "Task runner stopped");
    }

    /// Run the polling loop.
    pub async fn run(&self) {
        self.running.store(true, Ordering::SeqCst);

        info!(
            task_type = %self.config.task_definition_name,
            "Starting task runner polling loop"
        );

        while self.running.load(Ordering::SeqCst) {
            if let Err(e) = self.run_once().await {
                error!(
                    task_type = %self.config.task_definition_name,
                    error = %e,
                    "Error in polling loop"
                );
            }
        }

        // Wait for in-flight tasks to complete (graceful shutdown)
        self.wait_for_tasks_to_complete().await;

        info!(
            task_type = %self.config.task_definition_name,
            "Task runner polling loop ended"
        );
    }

    /// Wait for all spawned tasks to complete (used during shutdown).
    async fn wait_for_tasks_to_complete(&self) {
        let shutdown_timeout = Duration::from_secs(30);
        let start = Instant::now();

        while self.spawned_task_count.load(Ordering::SeqCst) > 0 {
            if start.elapsed() > shutdown_timeout {
                let remaining = self.spawned_task_count.load(Ordering::SeqCst);
                warn!(
                    task_type = %self.config.task_definition_name,
                    remaining_tasks = remaining,
                    "Shutdown timeout reached, {} tasks still in flight",
                    remaining
                );
                break;
            }
            tokio::time::sleep(Duration::from_millis(100)).await;
        }
    }

    /// Run one iteration of the polling loop.
    async fn run_once(&self) -> Result<()> {
        // Check if paused
        if self.paused.load(Ordering::SeqCst) {
            self.event_dispatcher
                .publish_poll_skipped_paused(&PollSkippedPaused::new(
                    &self.config.task_definition_name,
                    &self.config.worker_id,
                ));
            tokio::time::sleep(Duration::from_millis(100)).await;
            return Ok(());
        }

        // Calculate available capacity based on ACTIVE tasks (those that have acquired semaphore)
        // This is more accurate than tracking spawned tasks since semaphore controls actual concurrency
        let active_count = self.active_task_count.load(Ordering::SeqCst);
        let available_slots = self.config.thread_count.saturating_sub(active_count);

        if available_slots == 0 {
            // At capacity, wait briefly
            tokio::time::sleep(Duration::from_millis(1)).await;
            return Ok(());
        }

        // Apply adaptive backoff
        let empty_polls = self.consecutive_empty_polls.load(Ordering::SeqCst);
        if empty_polls > 0 {
            let backoff = Duration::from_millis(1 << empty_polls.min(10));
            let backoff = backoff.min(self.config.poll_interval);

            let elapsed = self.last_poll_time.lock().elapsed();
            if elapsed < backoff {
                tokio::time::sleep(backoff.checked_sub(elapsed).unwrap_or_default()).await;
            }
        }

        // Poll for tasks
        let poll_start = Instant::now();

        // Publish poll started event
        self.event_dispatcher
            .publish_poll_started(&PollStarted::new(
                &self.config.task_definition_name,
                &self.config.worker_id,
                available_slots,
            ));

        let poll_result = self
            .task_client
            .batch_poll(
                &self.config.task_definition_name,
                Some(&self.config.worker_id),
                self.config.domain.as_deref(),
                available_slots,
                self.config.poll_timeout,
            )
            .await;

        let poll_duration = poll_start.elapsed();
        *self.last_poll_time.lock() = Instant::now();
        self.poll_attempts.fetch_add(1, Ordering::SeqCst);

        match poll_result {
            Ok(tasks) => {
                // Publish poll completed event
                self.event_dispatcher
                    .publish_poll_completed(&PollCompleted::new(
                        &self.config.task_definition_name,
                        &self.config.worker_id,
                        poll_duration,
                        tasks.len(),
                    ));

                if tasks.is_empty() {
                    self.consecutive_empty_polls.fetch_add(1, Ordering::SeqCst);
                } else {
                    self.consecutive_empty_polls.store(0, Ordering::SeqCst);

                    // Process tasks
                    for task in tasks {
                        self.spawn_task_execution(task);
                    }
                }
            }
            Err(e) => {
                let exception = exception_label(&e);

                self.event_dispatcher
                    .publish_poll_failure(&PollFailure::new(
                        &self.config.task_definition_name,
                        &self.config.worker_id,
                        poll_duration,
                        e.to_string(),
                        exception,
                    ));

                self.consecutive_empty_polls.fetch_add(1, Ordering::SeqCst);
            }
        }

        Ok(())
    }

    /// Spawn task execution in background.
    ///
    /// This method correctly handles the semaphore acquisition order to avoid
    /// race conditions in capacity calculation:
    /// 1. Increment spawned count (for shutdown tracking)
    /// 2. Spawn task
    /// 3. Acquire semaphore (wait if at capacity)
    /// 4. Increment active count (now executing)
    /// 5. Track task ID in `running_tasks`
    /// 6. Execute task
    /// 7. Decrement active count and remove from `running_tasks`
    /// 8. Decrement spawned count
    fn spawn_task_execution(&self, task: Task) {
        let task_id = task.task_id.clone();

        // Increment spawned task count for shutdown tracking
        self.spawned_task_count.fetch_add(1, Ordering::SeqCst);

        let worker = Arc::clone(&self.worker);
        let task_client = self.task_client.clone();
        let event_dispatcher = self.event_dispatcher.clone();
        let config = Arc::clone(&self.config);
        let semaphore = Arc::clone(&self.semaphore);
        let active_task_count = Arc::clone(&self.active_task_count);
        let running_tasks = Arc::clone(&self.running_tasks);
        let spawned_task_count = Arc::clone(&self.spawned_task_count);

        let task_type = self.config.task_definition_name.clone();

        tokio::spawn(async move {
            // Acquire semaphore permit FIRST - this is the actual concurrency control
            let Ok(_permit) = semaphore.acquire().await else {
                // Semaphore was closed (shouldn't happen in normal operation)
                error!(task_id = %task_id, "Semaphore closed, dropping task");
                spawned_task_count.fetch_sub(1, Ordering::SeqCst);
                return;
            };

            // NOW increment active count and track the task
            // This ensures capacity calculation is accurate
            active_task_count.fetch_add(1, Ordering::SeqCst);
            running_tasks.lock().insert(task_id.clone());

            // `worker`, `config`, and `task` are moved into the panic-catching
            // boundary and cannot be accessed in the cleanup code below.
            let outcome =
                Self::execute_catching_panic(worker, &task_client, &event_dispatcher, config, task)
                    .await;

            // Cleanup: only atomics, locks, and event_dispatcher are accessible
            running_tasks.lock().remove(&task_id);
            active_task_count.fetch_sub(1, Ordering::SeqCst);
            spawned_task_count.fetch_sub(1, Ordering::SeqCst);

            match outcome {
                TaskOutcome::Ok => {}
                TaskOutcome::Err(e) => {
                    error!(
                        task_id = %task_id,
                        error = %e,
                        "Task execution failed"
                    );
                }
                TaskOutcome::Panic(panic_msg) => {
                    error!(
                        task_id = %task_id,
                        task_type = %task_type,
                        panic_message = %panic_msg,
                        "Uncaught panic in worker task"
                    );
                    event_dispatcher.publish_thread_uncaught_exception(
                        &ThreadUncaughtException::new(&task_type, "Panic"),
                    );
                }
            }
        });
    }

    /// Execute a task inside a panic-catching boundary.
    ///
    /// `worker`, `config`, and `task` are consumed so that the caller
    /// cannot access them after a potential panic — only the returned
    /// [`TaskOutcome`] carries the information needed for logging and
    /// event publishing.
    async fn execute_catching_panic(
        worker: Arc<dyn Worker>,
        task_client: &TaskClient,
        event_dispatcher: &EventDispatcher,
        config: Arc<WorkerConfig>,
        task: Task,
    ) -> TaskOutcome {
        match AssertUnwindSafe(Self::execute_and_update_task(
            &worker,
            task_client,
            event_dispatcher,
            &config,
            task,
        ))
        .catch_unwind()
        .await
        {
            Ok(Ok(())) => TaskOutcome::Ok,
            Ok(Err(e)) => TaskOutcome::Err(e),
            Err(panic_payload) => {
                let msg = panic_payload
                    .downcast_ref::<String>()
                    .map(std::string::String::as_str)
                    .or_else(|| panic_payload.downcast_ref::<&str>().copied())
                    .unwrap_or("<non-string panic>");
                TaskOutcome::Panic(msg.to_owned())
            }
        }
    }

    /// Execute a task and update the result.
    ///
    /// Takes ownership of the Task to wrap it in Arc, avoiding clones
    /// when passing to workers.
    async fn execute_and_update_task(
        worker: &Arc<dyn Worker>,
        task_client: &TaskClient,
        event_dispatcher: &EventDispatcher,
        config: &Arc<WorkerConfig>,
        task: Task,
    ) -> Result<()> {
        // Wrap task in Arc once - this is the only allocation
        let task = Arc::new(task);

        let task_id = &task.task_id;
        let task_type = &task.task_type;
        let workflow_id = &task.workflow_instance_id;

        debug!(
            task_id = %task_id,
            task_type = %task_type,
            "Executing task"
        );

        // Publish execution started event
        event_dispatcher.publish_task_execution_started(&TaskExecutionStarted::new(
            task_type,
            task_id,
            workflow_id,
            &config.worker_id,
        ));

        let exec_start = Instant::now();

        // Start a lease-extension heartbeat alongside execution, if configured -- a per-task
        // spawned tokio task (cheap here, unlike an OS thread) rather than a shared
        // background-thread manager.
        // The guard aborts the heartbeat when it goes out of scope, including by unwinding if
        // the worker future panics -- a bare `JoinHandle` only detaches on drop, which would
        // leave the heartbeat extending the lease indefinitely.
        let heartbeat_guard = Self::maybe_spawn_lease_heartbeat(task_client, &task, config)
            .map(tokio_util::task::AbortOnDropHandle::new);

        // Execute the worker - pass reference to avoid clone in worker trait
        let exec_result = worker.execute(&task).await;
        let exec_duration = exec_start.elapsed();

        drop(heartbeat_guard);

        // Convert result to TaskResult
        let task_result = match exec_result {
            Ok(output) => {
                let output_size = match &output {
                    WorkerOutput::Completed(data) => {
                        serde_json::to_string(data).map(|s| s.len()).ok()
                    }
                    _ => None,
                };

                // Publish execution completed event
                event_dispatcher.publish_task_execution_completed(&TaskExecutionCompleted::new(
                    task_type,
                    task_id,
                    workflow_id,
                    &config.worker_id,
                    exec_duration,
                    output_size,
                ));

                output.into_task_result(&task, &config.worker_id)
            }
            Err(e) => {
                let error_msg = e.to_string();
                let exception = exception_label(&e);
                let is_retryable = e.is_retryable();

                event_dispatcher.publish_task_execution_failure(&TaskExecutionFailure::new(
                    task_type,
                    task_id,
                    workflow_id,
                    &config.worker_id,
                    exec_duration,
                    &error_msg,
                    exception,
                    is_retryable,
                ));

                WorkerOutput::Failed(error_msg).into_task_result(&task, &config.worker_id)
            }
        };

        // Update task with retry
        let update_start = Instant::now();
        match task_client.update_task_with_retry(&task_result, 4).await {
            Ok(_) => {
                let update_duration = update_start.elapsed();
                debug!(task_id = %task_id, "Task updated successfully");

                event_dispatcher.publish_task_update_completed(&TaskUpdateCompleted::new(
                    task_type,
                    task_id,
                    workflow_id,
                    &config.worker_id,
                    update_duration,
                ));
            }
            Err(e) => {
                let update_duration = update_start.elapsed();
                error!(task_id = %task_id, error = %e, "Failed to update task after retries");
                let exception = exception_label(&e);

                event_dispatcher.publish_task_update_failure(&TaskUpdateFailure::new(
                    task_type,
                    task_id,
                    workflow_id,
                    &config.worker_id,
                    update_duration,
                    e.to_string(),
                    exception,
                    4,
                ));
            }
        }

        Ok(())
    }

    /// Start a background heartbeat loop for `task`, if lease extension is enabled and the
    /// task's `response_timeout_seconds` makes it worthwhile. Returns `None` (spawning nothing)
    /// when disabled.
    fn maybe_spawn_lease_heartbeat(
        task_client: &TaskClient,
        task: &Arc<Task>,
        config: &Arc<WorkerConfig>,
    ) -> Option<tokio::task::JoinHandle<()>> {
        if !config.lease_extend_enabled {
            return None;
        }
        if task.response_timeout_seconds <= 0 {
            return None;
        }
        let interval_secs = task.response_timeout_seconds as f64 * config.lease_extend_threshold;
        // An interval under a second isn't worth scheduling a repeating heartbeat for.
        if interval_secs < 1.0 {
            return None;
        }

        let task_client = task_client.clone();
        let task_id = task.task_id.clone();
        let workflow_instance_id = task.workflow_instance_id.clone();
        let interval = Duration::from_secs_f64(interval_secs);

        Some(tokio::spawn(Self::send_lease_heartbeats(
            task_client,
            task_id,
            workflow_instance_id,
            interval,
        )))
    }

    /// Send a lease-extension heartbeat every `interval`, starting `interval` after this is
    /// spawned (not immediately). Runs until the caller aborts the returned `JoinHandle` (when
    /// the task finishes), which is the only way this loop ends.
    #[expect(clippy::infinite_loop)]
    async fn send_lease_heartbeats(
        task_client: TaskClient,
        task_id: String,
        workflow_instance_id: String,
        interval: Duration,
    ) {
        // A short, fixed retry count with fast backoff -- deliberately not
        // `TaskClient::update_task_with_retry`'s 10/20/30s schedule, which is sized for
        // terminal completion updates, not a fast-repeating keep-alive that will just get
        // another chance at the next tick anyway.
        const LEASE_EXTEND_RETRY_COUNT: u32 = 3;

        let mut ticker = tokio::time::interval_at(tokio::time::Instant::now() + interval, interval);
        loop {
            ticker.tick().await;
            let heartbeat = crate::models::TaskResult {
                task_id: task_id.clone(),
                workflow_instance_id: workflow_instance_id.clone(),
                status: crate::models::TaskResultStatus::InProgress,
                extend_lease: true,
                ..Default::default()
            };

            for attempt in 0..LEASE_EXTEND_RETRY_COUNT {
                match task_client.update_task(&heartbeat).await {
                    Ok(_) => {
                        debug!(task_id = %task_id, "Extended lease");
                        break;
                    }
                    Err(e) if attempt + 1 < LEASE_EXTEND_RETRY_COUNT => {
                        warn!(task_id = %task_id, error = %e, attempt, "Lease heartbeat failed, retrying");
                        tokio::time::sleep(Duration::from_millis(500 * u64::from(attempt + 2)))
                            .await;
                    }
                    Err(e) => {
                        error!(task_id = %task_id, error = %e, "Failed to extend lease after {LEASE_EXTEND_RETRY_COUNT} attempts");
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::configuration::Configuration;
    use crate::http::ApiClient;
    use async_trait::async_trait;

    struct TestWorker;

    #[async_trait]
    impl Worker for TestWorker {
        fn task_definition_name(&self) -> &'static str {
            "test_task"
        }

        async fn execute(&self, task: &Task) -> Result<WorkerOutput> {
            let name = task
                .get_input_string("name")
                .unwrap_or_else(|| "World".to_owned());
            Ok(WorkerOutput::completed_with_result(format!(
                "Hello, {name}!"
            )))
        }

        fn thread_count(&self) -> usize {
            5
        }
    }

    #[test]
    fn test_task_runner_config() {
        let config = Configuration::new("http://localhost:8080/api");
        let api = ApiClient::new(config).unwrap();
        let task_client = TaskClient::new(api);
        let worker = Arc::new(TestWorker);
        let dispatcher = EventDispatcher::new();

        let runner = TaskRunner::new(worker, task_client, dispatcher);

        assert_eq!(runner.task_type(), "test_task");
        assert_eq!(runner.config().thread_count, 5);
        assert_eq!(runner.poll_attempt_count(), 0);
        assert!(!runner.is_running());
    }

    fn test_task_client() -> TaskClient {
        let config = Configuration::new("http://localhost:8080/api");
        TaskClient::new(ApiClient::new(config).unwrap())
    }

    #[test]
    fn test_lease_heartbeat_not_spawned_when_disabled() {
        let task_client = test_task_client();
        let task = Arc::new(Task {
            response_timeout_seconds: 30,
            ..Default::default()
        });
        let config = Arc::new(WorkerConfig::new("t").with_lease_extend_enabled(false));

        let handle = TaskRunner::maybe_spawn_lease_heartbeat(&task_client, &task, &config);
        assert!(handle.is_none());
    }

    #[test]
    fn test_lease_heartbeat_not_spawned_without_response_timeout() {
        let task_client = test_task_client();
        let task = Arc::new(Task {
            response_timeout_seconds: 0,
            ..Default::default()
        });
        let config = Arc::new(WorkerConfig::new("t").with_lease_extend_enabled(true));

        let handle = TaskRunner::maybe_spawn_lease_heartbeat(&task_client, &task, &config);
        assert!(handle.is_none());
    }

    #[test]
    fn test_lease_heartbeat_not_spawned_when_interval_too_short() {
        let task_client = test_task_client();
        // 1s timeout * 0.8 threshold = 0.8s, under the 1-second skip threshold.
        let task = Arc::new(Task {
            response_timeout_seconds: 1,
            ..Default::default()
        });
        let config = Arc::new(WorkerConfig::new("t").with_lease_extend_enabled(true));

        let handle = TaskRunner::maybe_spawn_lease_heartbeat(&task_client, &task, &config);
        assert!(handle.is_none());
    }

    #[tokio::test]
    async fn test_lease_heartbeat_spawned_when_enabled_and_worthwhile() {
        let task_client = test_task_client();
        let task = Arc::new(Task {
            response_timeout_seconds: 30,
            ..Default::default()
        });
        let config = Arc::new(
            WorkerConfig::new("t")
                .with_lease_extend_enabled(true)
                .with_lease_extend_threshold(0.8),
        );

        let handle = TaskRunner::maybe_spawn_lease_heartbeat(&task_client, &task, &config);
        assert!(handle.is_some());
        // Abort immediately -- this test only checks that a heartbeat loop gets scheduled at
        // all, not its actual network behavior (which needs a live/mock server and real time).
        handle.unwrap().abort();
    }
}