ChandimaPrabath commited on
Commit
02c2293
·
1 Parent(s): 05777c4

cleanup expired token every 6 hours

Browse files
Files changed (1) hide show
  1. main.py +20 -2
main.py CHANGED
@@ -10,11 +10,14 @@ from user_agents import parse
10
  from dotenv import load_dotenv
11
  from supabase import create_client, Client
12
  from typing import Optional
 
 
 
13
 
14
  load_dotenv()
15
 
16
  SERVER_NAME = "Nexus Authentication Service"
17
- VERSION = "1.0.1 debug"
18
 
19
  # Supabase Configuration
20
  SUPABASE_URL = os.getenv("SUPABASE_URL")
@@ -87,6 +90,20 @@ def create_device_token(username: str, user_agent: str) -> str:
87
  def is_token_expired(expiration_time: datetime) -> bool:
88
  return datetime.now(timezone.utc) > expiration_time
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  # Initialize system user
91
  async def init_system_user():
92
  system_user_data = {
@@ -398,4 +415,5 @@ app.include_router(admin_router)
398
  # Initialize system user on startup
399
  @app.on_event("startup")
400
  async def startup_event():
401
- await init_system_user()
 
 
10
  from dotenv import load_dotenv
11
  from supabase import create_client, Client
12
  from typing import Optional
13
+ import logging
14
+ from time import sleep
15
+ import asyncio
16
 
17
  load_dotenv()
18
 
19
  SERVER_NAME = "Nexus Authentication Service"
20
+ VERSION = "1.0.2 debug"
21
 
22
  # Supabase Configuration
23
  SUPABASE_URL = os.getenv("SUPABASE_URL")
 
90
  def is_token_expired(expiration_time: datetime) -> bool:
91
  return datetime.now(timezone.utc) > expiration_time
92
 
93
+
94
+ logging.basicConfig(level=logging.INFO)
95
+
96
+ async def clean_expired_tokens():
97
+ while True:
98
+ current_time = datetime.now(timezone.utc)
99
+ try:
100
+ supabase.table("sessions").delete().lt("expires", current_time.isoformat()).execute()
101
+ logging.info(f"Expired tokens cleaned at {current_time.isoformat()}")
102
+ except Exception as e:
103
+ logging.error(f"Failed to clean expired tokens: {e}")
104
+ await sleep(6 * 60 * 60)
105
+
106
+
107
  # Initialize system user
108
  async def init_system_user():
109
  system_user_data = {
 
415
  # Initialize system user on startup
416
  @app.on_event("startup")
417
  async def startup_event():
418
+ await init_system_user()
419
+ asyncio.create_task(clean_expired_tokens())