Spaces:
Sleeping
Sleeping
- reset when the user rejoining min 2 users
Browse files- backend/app.py +22 -1
backend/app.py
CHANGED
|
@@ -18,6 +18,8 @@ sys.path.append(".")
|
|
| 18 |
|
| 19 |
app = FastAPI()
|
| 20 |
|
|
|
|
|
|
|
| 21 |
# app.mount("/", StaticFiles(directory="frontend", html=True), name="frontend")
|
| 22 |
|
| 23 |
app.add_middleware(
|
|
@@ -99,7 +101,18 @@ async def periodic_recommendation():
|
|
| 99 |
while True:
|
| 100 |
user_list = list(clients.keys())
|
| 101 |
if len(user_list) >= 2:
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
if clients: # Only run if someone is connected
|
| 104 |
if len(chat_history) > 0:
|
| 105 |
# 1. Detect emotion dan ambil (label, score)
|
|
@@ -140,6 +153,14 @@ async def periodic_recommendation():
|
|
| 140 |
else:
|
| 141 |
await asyncio.sleep(2)
|
| 142 |
await broadcast_user_list()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
@app.on_event("startup")
|
| 145 |
async def start_recommender():
|
|
|
|
| 18 |
|
| 19 |
app = FastAPI()
|
| 20 |
|
| 21 |
+
reset_timer = asyncio.Event()
|
| 22 |
+
|
| 23 |
# app.mount("/", StaticFiles(directory="frontend", html=True), name="frontend")
|
| 24 |
|
| 25 |
app.add_middleware(
|
|
|
|
| 101 |
while True:
|
| 102 |
user_list = list(clients.keys())
|
| 103 |
if len(user_list) >= 2:
|
| 104 |
+
sleep_task = asyncio.create_task(asyncio.sleep(60)) # Start sleep
|
| 105 |
+
|
| 106 |
+
try:
|
| 107 |
+
await asyncio.wait_for(sleep_task, timeout=60) # Wait 60s unless interrupted
|
| 108 |
+
except asyncio.TimeoutError:
|
| 109 |
+
pass # Timer completed without reset
|
| 110 |
+
except asyncio.CancelledError:
|
| 111 |
+
continue # Timer was reset, restart loop
|
| 112 |
+
|
| 113 |
+
if reset_timer.is_set(): # If reset happened, restart countdown
|
| 114 |
+
continue
|
| 115 |
+
|
| 116 |
if clients: # Only run if someone is connected
|
| 117 |
if len(chat_history) > 0:
|
| 118 |
# 1. Detect emotion dan ambil (label, score)
|
|
|
|
| 153 |
else:
|
| 154 |
await asyncio.sleep(2)
|
| 155 |
await broadcast_user_list()
|
| 156 |
+
reset_recommendation_timer()
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
async def reset_recommendation_timer():
|
| 160 |
+
"""Call this function when you want to reset the timer to 60 seconds."""
|
| 161 |
+
if reset_timer.is_set(): # Check if the timer is running before resetting
|
| 162 |
+
reset_timer.clear() # Clear the event before setting it again
|
| 163 |
+
reset_timer.set() # Trigger the reset
|
| 164 |
|
| 165 |
@app.on_event("startup")
|
| 166 |
async def start_recommender():
|