@@ -14,11 +14,17 @@ use ntex::web::HttpRequest;
1414use rand:: Rng ;
1515use tokio_util:: sync:: CancellationToken ;
1616
17- use crate :: { background_tasks:: BackgroundTask , consts:: ROUTER_VERSION } ;
17+ use crate :: {
18+ background_tasks:: { BackgroundTask , BackgroundTasksManager } ,
19+ consts:: ROUTER_VERSION ,
20+ } ;
1821
19- pub fn create_hive_user_agent ( usage_config : & UsageReportingConfig ) -> UsageAgent {
22+ pub fn init_hive_user_agent (
23+ bg_tasks_manager : & mut BackgroundTasksManager ,
24+ usage_config : & UsageReportingConfig ,
25+ ) -> Arc < UsageAgent > {
2026 let user_agent = format ! ( "hive-router/{}" , ROUTER_VERSION ) ;
21- hive_console_sdk:: agent:: UsageAgent :: new (
27+ let hive_user_agent = hive_console_sdk:: agent:: UsageAgent :: new (
2228 usage_config. access_token . clone ( ) ,
2329 usage_config. endpoint . clone ( ) ,
2430 usage_config. target_id . clone ( ) ,
@@ -28,7 +34,10 @@ pub fn create_hive_user_agent(usage_config: &UsageReportingConfig) -> UsageAgent
2834 usage_config. accept_invalid_certs ,
2935 usage_config. flush_interval ,
3036 user_agent,
31- )
37+ ) ;
38+ let hive_user_agent_arc = Arc :: new ( hive_user_agent) ;
39+ bg_tasks_manager. register_task ( hive_user_agent_arc. clone ( ) ) ;
40+ hive_user_agent_arc
3241}
3342
3443#[ inline]
@@ -37,7 +46,7 @@ pub fn collect_usage_report(
3746 duration : Duration ,
3847 req : & HttpRequest ,
3948 client_request_details : & ClientRequestDetails ,
40- usage_agent : & UsageAgent ,
49+ hive_usage_agent : & UsageAgent ,
4150 usage_config : & UsageReportingConfig ,
4251 execution_result : & PlanExecutionOutput ,
4352) {
@@ -75,7 +84,7 @@ pub fn collect_usage_report(
7584 persisted_document_hash : None ,
7685 } ;
7786
78- if let Err ( err) = usage_agent . add_report ( execution_report) {
87+ if let Err ( err) = hive_usage_agent . add_report ( execution_report) {
7988 tracing:: error!( "Failed to send usage report: {}" , err) ;
8089 }
8190}
0 commit comments