Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -226,13 +226,12 @@ html_content = """
|
|
226 |
</html>
|
227 |
"""
|
228 |
|
229 |
-
# Define a model for the incoming JSON data
|
230 |
class ActionModel(BaseModel):
|
231 |
-
action: str #
|
232 |
-
|
233 |
class ModelData(BaseModel):
|
234 |
-
data: str
|
235 |
-
|
236 |
# Function to process emails in a loop
|
237 |
def email_processing_loop():
|
238 |
global running
|
@@ -248,34 +247,35 @@ async def home():
|
|
248 |
global running
|
249 |
status = "Running" if running else "Stopped"
|
250 |
return HTMLResponse(content=html_content.replace("{{ status }}", status), status_code=200)
|
251 |
-
|
252 |
-
# Endpoint to receive JSON data to start/stop the email processing loop
|
253 |
-
@app.get("/control", response_class=HTMLResponse)
|
254 |
-
async def control_email_loop(action: ActionModel,data: ModelData):
|
255 |
global running, loop_thread
|
256 |
logger.info(action.action)
|
|
|
257 |
if action.action == "start":
|
258 |
if not running:
|
259 |
running = True
|
260 |
-
email_data = data.data.dict()
|
261 |
-
logger.info(email_data)
|
262 |
loop_thread = threading.Thread(target=email_processing_loop, daemon=True)
|
263 |
loop_thread.start()
|
264 |
logger.info("Email processing loop started.")
|
265 |
else:
|
266 |
logger.info("Email processing loop is already running.")
|
|
|
267 |
elif action.action == "stop":
|
268 |
if running:
|
269 |
running = False
|
270 |
logger.info("Email processing loop stopped.")
|
271 |
-
else:
|
272 |
logger.info("Email processing loop is not running.")
|
|
|
273 |
else:
|
274 |
raise HTTPException(status_code=400, detail="Invalid action. Use 'start' or 'stop'.")
|
275 |
|
276 |
status = "Running" if running else "Stopped"
|
277 |
return HTMLResponse(content=html_content.replace("{{ status }}", status), status_code=200)
|
278 |
|
|
|
279 |
if __name__ == "__main__":
|
280 |
|
281 |
print('starting')
|
|
|
226 |
</html>
|
227 |
"""
|
228 |
|
|
|
229 |
class ActionModel(BaseModel):
|
230 |
+
action: str # 'start' or 'stop'
|
231 |
+
|
232 |
class ModelData(BaseModel):
|
233 |
+
data: str # Additional email-related information
|
234 |
+
|
235 |
# Function to process emails in a loop
|
236 |
def email_processing_loop():
|
237 |
global running
|
|
|
247 |
global running
|
248 |
status = "Running" if running else "Stopped"
|
249 |
return HTMLResponse(content=html_content.replace("{{ status }}", status), status_code=200)
|
250 |
+
async def control_email_loop(action: ActionModel, data: ModelData):
|
|
|
|
|
|
|
251 |
global running, loop_thread
|
252 |
logger.info(action.action)
|
253 |
+
|
254 |
if action.action == "start":
|
255 |
if not running:
|
256 |
running = True
|
257 |
+
email_data = data.data # This is already a string, no need to call .dict()
|
258 |
+
logger.info(f"Email Data: {email_data}")
|
259 |
loop_thread = threading.Thread(target=email_processing_loop, daemon=True)
|
260 |
loop_thread.start()
|
261 |
logger.info("Email processing loop started.")
|
262 |
else:
|
263 |
logger.info("Email processing loop is already running.")
|
264 |
+
|
265 |
elif action.action == "stop":
|
266 |
if running:
|
267 |
running = False
|
268 |
logger.info("Email processing loop stopped.")
|
269 |
+
else:
|
270 |
logger.info("Email processing loop is not running.")
|
271 |
+
|
272 |
else:
|
273 |
raise HTTPException(status_code=400, detail="Invalid action. Use 'start' or 'stop'.")
|
274 |
|
275 |
status = "Running" if running else "Stopped"
|
276 |
return HTMLResponse(content=html_content.replace("{{ status }}", status), status_code=200)
|
277 |
|
278 |
+
|
279 |
if __name__ == "__main__":
|
280 |
|
281 |
print('starting')
|