1010from .deeploy_mixin import _DeeployMixin
1111from .deeploy_target_nodes_mixin import _DeeployTargetNodesMixin
1212from extensions .business .mixins .node_tags_mixin import _NodeTagsMixin
13+ from extensions .business .mixins .request_tracking_mixin import _RequestTrackingMixin
1314from .deeploy_const import (
1415 DEEPLOY_CREATE_REQUEST , DEEPLOY_CREATE_REQUEST_MULTI_PLUGIN , DEEPLOY_GET_APPS_REQUEST , DEEPLOY_DELETE_REQUEST ,
1516 DEEPLOY_ERRORS , DEEPLOY_KEYS , DEEPLOY_SCALE_UP_JOB_WORKERS_REQUEST , DEEPLOY_STATUS , DEEPLOY_INSTANCE_COMMAND_REQUEST ,
2021
2122from naeural_core .business .default .web_app .supervisor_fast_api_web_app import SupervisorFastApiWebApp as BasePlugin
2223
23- DEEPLOY_REQUESTS_CSTORE_HKEY = "DEEPLOY_REQUESTS"
24-
2524__VER__ = '0.6.0'
2625
2726
3938 'WARMUP_DELAY' : 300 ,
4039 'PIPELINES_CHECK_DELAY' : 300 ,
4140 'MIN_ETH_BALANCE' : 0.00005 ,
41+
42+ 'REQUESTS_CSTORE_HKEY' : 'DEEPLOY_REQUESTS' ,
4243 'REQUESTS_LOG_INTERVAL' : 5 * 60 ,
4344 'REQUESTS_MAX_RECORDS' : 2 ,
4445
@@ -55,6 +56,7 @@ class DeeployManagerApiPlugin(
5556 _DeeployTargetNodesMixin ,
5657 _NodeTagsMixin ,
5758 _DeeployJobMixin ,
59+ _RequestTrackingMixin ,
5860 ):
5961 """
6062 This plugin is the dAuth FastAPI web app that provides an endpoints for decentralized authentication.
@@ -86,68 +88,16 @@ def on_init(self):
8688 color = 'r' , boxed = True
8789 )
8890 self .maybe_stop_tunnel_engine ()
89- # Request tracking state
90- self .__recent_requests = self .deque (maxlen = self .cfg_requests_max_records )
91- self .__last_requests_log_time = 0
91+ self ._init_request_tracking ()
9292 return
9393
9494
9595 def on_request (self , request ):
96- """
97- Hook called when a new request arrives from the FastAPI side (monitor thread).
98- Captures minimal request metadata and writes the last N records to cstore.
99-
100- Parameters
101- ----------
102- request : dict
103- Raw request payload pulled from the server queue.
104- Structure: {'id': str, 'value': tuple, 'profile': dict|None}
105- """
106- try :
107- value = request .get ('value' )
108- request_id = request .get ('id' )
109- endpoint = value [0 ] if isinstance (value , (list , tuple )) and len (value ) > 0 else 'unknown'
110- record = {
111- 'id' : request_id ,
112- 'endpoint' : endpoint ,
113- 'date_start' : self .datetime .now (self .timezone .utc ).strftime ('%Y-%m-%d %H:%M:%S' ),
114- 'date_complete' : None ,
115- }
116- self .__recent_requests .append (record )
117- self ._save_requests_to_cstore ()
118- except Exception as e :
119- self .P (f"Error tracking request in cstore: { e } " , color = 'r' )
96+ self ._track_request (request )
12097 return
12198
122-
12399 def on_response (self , method , response ):
124- """
125- Hook called before the response is sent back to the FastAPI side (main thread).
126-
127- Parameters
128- ----------
129- method : str
130- The endpoint name that was called.
131- response : dict
132- Response dict with 'id' and 'value' keys.
133- """
134- try :
135- request_id = response .get ('id' )
136- for record in self .__recent_requests :
137- if record .get ('id' ) == request_id :
138- record ['date_complete' ] = self .datetime .now (self .timezone .utc ).strftime ('%Y-%m-%d %H:%M:%S' )
139- self ._save_requests_to_cstore ()
140- break
141- except Exception as e :
142- self .P (f"Error tracking response in cstore: { e } " , color = 'r' )
143- return
144-
145- def _save_requests_to_cstore (self ):
146- self .chainstore_hset (
147- hkey = DEEPLOY_REQUESTS_CSTORE_HKEY ,
148- key = self .ee_id ,
149- value = list (self .__recent_requests ),
150- )
100+ self ._track_response (method , response )
151101 return
152102
153103
@@ -1117,17 +1067,5 @@ def process(self):
11171067 self .P (f"Error checking running pipelines: { e } " , color = 'r' )
11181068 self .__last_pipelines_check_time = self .time ()
11191069
1120- # Periodic dump of all nodes' recent requests from cstore
1121- if (self .time () - self .__last_requests_log_time ) > self .cfg_requests_log_interval :
1122- try :
1123- all_requests = self .chainstore_hgetall (hkey = DEEPLOY_REQUESTS_CSTORE_HKEY )
1124- if all_requests :
1125- self .P (f"Deeploy requests across all nodes:\n { self .json_dumps (all_requests , indent = 2 )} " )
1126- else :
1127- self .P ("Deeploy requests across all nodes: no data" )
1128- except Exception as e :
1129- self .P (f"Error dumping deeploy requests from cstore: { e } " , color = 'r' )
1130- # end try
1131- self .__last_requests_log_time = self .time ()
1132- # end if
1070+ self ._maybe_log_tracked_requests ()
11331071 return
0 commit comments