use crate::domain::repositories::task_repository::TaskRepository;
use log::{debug, warn};
use std::sync::Arc;
pub struct LockHeartbeat {
handle: tokio::task::JoinHandle<()>,
}
impl LockHeartbeat {
pub fn start(
repo: Arc<dyn TaskRepository>,
task_id: crate::domain::id::Id,
worker_id: crate::domain::id::Id,
lock_duration_seconds: u64,
) -> Self {
let interval_secs = (lock_duration_seconds / 3).max(1);
let handle = tokio::spawn(async move {
let mut ticker = tokio::time::interval(std::time::Duration::from_secs(interval_secs));
ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
ticker.tick().await;
loop {
ticker.tick().await;
match repo
.renew_lock(task_id, worker_id, lock_duration_seconds as i64)
.await
{
Ok(true) => {
debug!("Lock renewed for task {} by worker {}", task_id, worker_id);
}
Ok(false) => {
warn!(
"Lock renewal lost for task {} (worker {}), stopping heartbeat",
task_id, worker_id
);
break;
}
Err(e) => {
warn!(
"Lock renewal failed for task {} (worker {}): {}",
task_id, worker_id, e
);
}
}
}
});
Self { handle }
}
}
impl Drop for LockHeartbeat {
fn drop(&mut self) {
self.handle.abort();
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::models::Task;
use crate::domain::repositories::task_repository::{RepositoryError, TaskQueryParams};
use async_trait::async_trait;
use std::collections::HashSet;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
struct MockRepo {
renew_calls: AtomicU64,
renew_ok: bool,
}
impl MockRepo {
fn new(renew_ok: bool) -> Self {
Self {
renew_calls: AtomicU64::new(0),
renew_ok,
}
}
}
#[async_trait]
impl TaskRepository for MockRepo {
async fn create(&self, task: &Task) -> Result<Task, RepositoryError> {
Ok(task.clone())
}
async fn find_by_id(
&self,
_id: crate::domain::id::Id,
) -> Result<Option<Task>, RepositoryError> {
Ok(None)
}
async fn update(&self, task: &Task) -> Result<Task, RepositoryError> {
Ok(task.clone())
}
async fn acquire_next(
&self,
_worker_id: crate::domain::id::Id,
) -> Result<Option<Task>, RepositoryError> {
Ok(None)
}
async fn mark_completed(
&self,
_id: crate::domain::id::Id,
_lock_token: Option<crate::domain::id::Id>,
) -> Result<u64, RepositoryError> {
Ok(1)
}
async fn mark_failed(
&self,
_id: crate::domain::id::Id,
_lock_token: Option<crate::domain::id::Id>,
) -> Result<u64, RepositoryError> {
Ok(1)
}
async fn mark_cancelled(&self, _id: crate::domain::id::Id) -> Result<u64, RepositoryError> {
Ok(1)
}
async fn renew_lock(
&self,
_task_id: crate::domain::id::Id,
_worker_id: crate::domain::id::Id,
_extend_seconds: i64,
) -> Result<bool, RepositoryError> {
self.renew_calls.fetch_add(1, Ordering::SeqCst);
Ok(self.renew_ok)
}
async fn exists_by_url(&self, _url: &str) -> Result<bool, RepositoryError> {
Ok(false)
}
async fn find_existing_urls(
&self,
_urls: &[String],
) -> Result<HashSet<String>, RepositoryError> {
Ok(HashSet::new())
}
async fn reset_stuck_tasks(
&self,
_timeout: chrono::Duration,
) -> Result<u64, RepositoryError> {
Ok(0)
}
async fn cancel_tasks_by_crawl_id(
&self,
_crawl_id: crate::domain::id::Id,
) -> Result<u64, RepositoryError> {
Ok(0)
}
async fn expire_tasks(&self) -> Result<u64, RepositoryError> {
Ok(0)
}
async fn find_by_crawl_id(
&self,
_crawl_id: crate::domain::id::Id,
) -> Result<Vec<Task>, RepositoryError> {
Ok(vec![])
}
async fn query_tasks(
&self,
_params: TaskQueryParams,
) -> Result<(Vec<Task>, u64), RepositoryError> {
Ok((vec![], 0))
}
async fn batch_cancel(
&self,
_task_ids: Vec<crate::domain::id::Id>,
_team_id: crate::domain::id::Id,
_force: bool,
) -> Result<
(
Vec<crate::domain::id::Id>,
Vec<(crate::domain::id::Id, String)>,
),
RepositoryError,
> {
Ok((vec![], vec![]))
}
}
#[tokio::test]
async fn test_heartbeat_renews_periodically_and_stops_on_drop() {
let repo = Arc::new(MockRepo::new(true));
{
let _hb = LockHeartbeat::start(
repo.clone() as Arc<dyn TaskRepository>,
crate::domain::id::Id::generate(),
crate::domain::id::Id::generate(),
3, );
tokio::time::sleep(Duration::from_millis(2_500)).await;
}
tokio::time::sleep(Duration::from_millis(1_200)).await;
let calls = repo.renew_calls.load(Ordering::SeqCst);
assert!(calls >= 1, "heartbeat should have renewed at least once");
}
#[tokio::test]
async fn test_heartbeat_stops_when_lock_lost() {
let repo = Arc::new(MockRepo::new(false));
let _hb = LockHeartbeat::start(
repo.clone() as Arc<dyn TaskRepository>,
crate::domain::id::Id::generate(),
crate::domain::id::Id::generate(),
3,
);
tokio::time::sleep(Duration::from_millis(2_500)).await;
let calls = repo.renew_calls.load(Ordering::SeqCst);
assert!(
calls <= 2,
"heartbeat should stop after lock lost, got {calls}"
);
}
}