29
29
from src .management_api .routes import admin_app
30
30
from src .prefetch import prefetch_endpoints
31
31
from src .ratelimit import ip_rate_limits , RateLimit
32
- from src .requests import requester
32
+ from src .requests import requester , ServerMonitor
33
33
from src .utils .my_logger import init_logger
34
34
from src .utils .utils import is_development
35
35
@@ -340,9 +340,14 @@ async def compare_tokens():
340
340
return response
341
341
342
342
343
+ #######################################################################################################################
344
+
345
+
343
346
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
344
347
# # # # # # # # # # # # # # STARTUP EVENTS
345
348
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
349
+ remote_servers = ServerMonitor ()
350
+
346
351
347
352
# On Start Up Run the following Tasks
348
353
@app .on_event ('startup' )
@@ -390,18 +395,25 @@ async def backup_cf_firewall_data():
390
395
391
396
async def clean_up_memcache ():
392
397
while True :
393
- # This cleans up the cache every ten minutes
398
+ # This cleans up the cache every 30 minutes
394
399
total_cleaned = await redis_cache .memcache_ttl_cleaner ()
395
400
app_logger .info (f"Cleaned Up { total_cleaned } Expired Mem Cache Values" )
396
401
await asyncio .sleep (delay = 60 * 30 )
397
402
403
+ async def monitor_servers ():
404
+ """will prioritize servers which are responsive and also available"""
405
+ while True :
406
+ await remote_servers .sort_api_servers_by_health ()
407
+ await asyncio .sleep (delay = 60 * 5 )
408
+
398
409
asyncio .create_task (setup_cf_firewall ())
399
410
asyncio .create_task (backup_cf_firewall_data ())
400
411
asyncio .create_task (update_api_keys_background_task ())
401
412
# asyncio.create_task(prefetch())
402
413
asyncio .create_task (process_credit_queue ())
403
414
asyncio .create_task (email_process .process_message_queues ())
404
415
asyncio .create_task (clean_up_memcache ())
416
+ asyncio .create_task (monitor_servers ())
405
417
406
418
407
419
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
@@ -437,7 +449,7 @@ async def v1_gateway(request: Request, path: str):
437
449
_path = f"/api/v1/{ path } "
438
450
await create_take_credit_args (api_key = api_key , path = _path )
439
451
440
- api_urls = [f'{ api_server_url } /api/v1/{ path } ' for api_server_url in api_server_urls ]
452
+ api_urls = [f'{ api_server_url } /api/v1/{ path } ' for api_server_url in remote_servers . healthy_server_urls ]
441
453
442
454
# Will Take at least six second on the cache if it finds nothing will return None
443
455
# need an improved get timeout for the articles
@@ -450,7 +462,7 @@ async def v1_gateway(request: Request, path: str):
450
462
return JSONResponse (content = response , status_code = 200 , headers = {"Content-Type" : "application/json" })
451
463
452
464
app_logger .info (msg = "All cached responses not found- Must Be a Slow Day" )
453
- for api_url in api_urls :
465
+ for api_url in remote_servers . healthy_server_urls :
454
466
try :
455
467
# 5 minutes timeout on resource fetching from backend - some resources may take very long
456
468
response = await requester (api_url = api_url , timeout = 9600 )
0 commit comments