Skip to content

Commit 1dc0010

Browse files
committed
chore: replace dummy handle with watch handle
1 parent c51a859 commit 1dc0010

File tree

4 files changed

+70
-27
lines changed

4 files changed

+70
-27
lines changed

crates/tap-agent/src/actor_migrate.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
//! Abstraction layer for migrating from ractor actors to tokio tasks
4+
//! Task management and lifecycle abstractions for the tokio-based TAP agent
55
//!
6-
//! This module provides a compatibility layer that allows gradual migration
7-
//! from ractor actors to tokio tasks while maintaining the same API.
6+
//! This module provides task spawning, lifecycle management, and communication
7+
//! abstractions for the tokio-based TAP agent architecture.
88
99
use std::{collections::HashMap, fmt::Debug, future::Future, sync::Arc, time::Duration};
1010

@@ -542,8 +542,6 @@ impl TaskRegistry {
542542
}
543543
}
544544

545-
// Compatibility module removed - tokio migration complete! 🎉
546-
547545
#[cfg(test)]
548546
mod tests {
549547
use super::*;

crates/tap-agent/src/agent.rs

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,24 @@ use crate::{
5454

5555
/// Actor, Arguments, State, Messages and implementation for [crate::agent::sender_account::SenderAccount]
5656
pub mod sender_account;
57-
/// Tokio task-based replacement for SenderAccount actor
57+
/// SenderAccount task implementation
5858
pub mod sender_account_task;
5959
/// Actor, Arguments, State, Messages and implementation for
6060
/// [crate::agent::sender_accounts_manager::SenderAccountsManager]
6161
pub mod sender_accounts_manager;
62-
/// Tokio task-based replacement for SenderAccountsManager actor
62+
/// SenderAccountsManager task implementation
6363
pub mod sender_accounts_manager_task;
6464
/// Actor, Arguments, State, Messages and implementation for [crate::agent::sender_allocation::SenderAllocation]
6565
pub mod sender_allocation;
66-
/// Tokio task-based replacement for SenderAllocation actor
66+
/// SenderAllocation task implementation
6767
pub mod sender_allocation_task;
6868
/// Tests for task lifecycle monitoring and health checks
6969
#[cfg(test)]
7070
mod test_lifecycle_monitoring;
71-
/// Comprehensive tests for tokio migration
71+
/// Comprehensive integration tests
7272
#[cfg(test)]
7373
mod test_tokio_migration;
74-
/// Regression tests comparing ractor vs tokio behavior
74+
/// Regression tests for system behavior
7575
#[cfg(test)]
7676
mod test_tokio_regression;
7777
/// Unaggregated receipts containing total value and last id stored in the table
@@ -83,7 +83,9 @@ use crate::actor_migrate::TaskHandle;
8383
///
8484
/// It uses the static [crate::CONFIG] to configure the agent.
8585
///
86-
/// 🎯 TOKIO MIGRATION: Now returns TaskHandle instead of ActorRef
86+
/// Returns:
87+
/// - TaskHandle for the main SenderAccountsManagerTask
88+
/// - JoinHandle for the system health monitoring task that triggers shutdown on critical failures
8789
pub async fn start_agent() -> (TaskHandle<SenderAccountsManagerMessage>, JoinHandle<()>) {
8890
let Config {
8991
indexer: IndexerConfig {
@@ -246,7 +248,7 @@ pub async fn start_agent() -> (TaskHandle<SenderAccountsManagerMessage>, JoinHan
246248
config
247249
}));
248250

249-
// 🎯 TOKIO MIGRATION: Using SenderAccountsManagerTask instead of ractor SenderAccountsManager
251+
// Initialize the tokio-based sender accounts manager
250252
let lifecycle = LifecycleManager::new();
251253

252254
let task_handle = SenderAccountsManagerTask::spawn(
@@ -263,15 +265,60 @@ pub async fn start_agent() -> (TaskHandle<SenderAccountsManagerMessage>, JoinHan
263265
.await
264266
.expect("Failed to start sender accounts manager task.");
265267

266-
// Create a dummy JoinHandle for compatibility with main.rs
267-
// In the tokio model, the lifecycle manager handles task monitoring
268-
let dummy_handle = tokio::spawn(async {
269-
// This task runs indefinitely until the application shuts down
270-
// The actual work is done by the SenderAccountsManagerTask and its children
271-
loop {
272-
tokio::time::sleep(std::time::Duration::from_secs(60)).await;
268+
// Create a proper system monitoring task that integrates with LifecycleManager
269+
// This task monitors overall system health and can trigger graceful shutdown
270+
let monitoring_handle = tokio::spawn({
271+
let lifecycle_clone = lifecycle.clone();
272+
async move {
273+
let mut interval = tokio::time::interval(std::time::Duration::from_secs(5));
274+
let mut consecutive_unhealthy_checks = 0;
275+
const MAX_UNHEALTHY_CHECKS: u32 = 12; // 60 seconds of unhealthy state
276+
277+
loop {
278+
interval.tick().await;
279+
280+
// Monitor system health
281+
let system_health = lifecycle_clone.get_system_health().await;
282+
283+
if !system_health.overall_healthy {
284+
consecutive_unhealthy_checks += 1;
285+
tracing::warn!(
286+
"System unhealthy: {}/{} healthy tasks, {} failed, {} restarting (check {}/{})",
287+
system_health.healthy_tasks,
288+
system_health.total_tasks,
289+
system_health.failed_tasks,
290+
system_health.restarting_tasks,
291+
consecutive_unhealthy_checks,
292+
MAX_UNHEALTHY_CHECKS
293+
);
294+
295+
// If system has been unhealthy for too long, trigger shutdown
296+
if consecutive_unhealthy_checks >= MAX_UNHEALTHY_CHECKS {
297+
tracing::error!(
298+
"System has been unhealthy for {} checks, initiating graceful shutdown",
299+
consecutive_unhealthy_checks
300+
);
301+
break;
302+
}
303+
} else {
304+
// Reset counter on healthy check
305+
if consecutive_unhealthy_checks > 0 {
306+
tracing::info!(
307+
"System health recovered: {}/{} healthy tasks",
308+
system_health.healthy_tasks,
309+
system_health.total_tasks
310+
);
311+
consecutive_unhealthy_checks = 0;
312+
}
313+
}
314+
315+
// Perform periodic health checks on all tasks
316+
lifecycle_clone.perform_health_check().await;
317+
}
318+
319+
tracing::info!("System monitoring task shutting down");
273320
}
274321
});
275322

276-
(task_handle, dummy_handle)
323+
(task_handle, monitoring_handle)
277324
}

crates/tap-agent/src/agent/sender_account_task.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
//! Tokio-based replacement for SenderAccount actor
4+
//! SenderAccount task implementation
55
//!
6-
//! This module provides a drop-in replacement for the ractor-based SenderAccount
7-
//! that uses tokio tasks and channels for message passing.
6+
//! This module provides the SenderAccount task that manages receipts and fees
7+
//! across allocations using tokio tasks and channels for message passing.
88
99
use std::{
1010
collections::{HashMap, HashSet},
@@ -47,7 +47,7 @@ use crate::tap::context::{Horizon, Legacy};
4747
type Balance = U256;
4848
type RavMap = HashMap<Address, u128>;
4949

50-
/// Tokio task-based replacement for SenderAccount actor
50+
/// SenderAccount task for managing receipts and fees across allocations
5151
pub struct SenderAccountTask;
5252

5353
/// State for the SenderAccount task
@@ -477,8 +477,7 @@ impl SenderAccountTask {
477477

478478
#[cfg(not(any(test, feature = "test")))]
479479
{
480-
// 🎯 PRODUCTION TAP MANAGER INTEGRATION
481-
// Create proper TAP manager and aggregator client for production deployment
480+
// Create TAP manager and aggregator client for production deployment
482481

483482
// Create a self-reference handle for the child to communicate back
484483
let (self_tx, self_rx) = mpsc::channel::<SenderAccountMessage>(10);

crates/tap-agent/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ async fn main() -> anyhow::Result<()> {
4242
// If we're here, we've received a signal to exit.
4343
tracing::info!("Shutting down...");
4444

45-
// 🎯 TOKIO MIGRATION: Use TaskHandle methods instead of ActorRef methods
4645
if manager.get_status().await == TaskStatus::Running {
4746
tracing::info!("Stopping sender accounts manager task...");
4847
manager.stop(Some("Shutdown signal received".to_string()));

0 commit comments

Comments
 (0)