acecalisto3 commited on
Commit
2a8d7ab
1 Parent(s): 2ff1685

Update app2.py

Browse files
Files changed (1) hide show
  1. app2.py +30 -12
app2.py CHANGED
@@ -4,6 +4,7 @@ import hashlib
4
  import os
5
  from typing import List, Tuple, Dict, Any, Optional
6
  import datetime
 
7
  import feedparser
8
  import aiohttp
9
  import gradio as gr
@@ -13,6 +14,9 @@ from sqlalchemy.exc import SQLAlchemyError
13
  from sqlalchemy.orm import declarative_base, sessionmaker
14
  from urllib.parse import urljoin
15
  import logging
 
 
 
16
  Base = declarative_base()
17
 
18
  class Article(Base):
@@ -253,11 +257,8 @@ async def main():
253
  try:
254
  engine, Session = await create_db_engine("sqlite:///monitoring.db")
255
  db_session = Session()
256
- except SQLAlchemyError as e:
257
- logger.error(f"Failed to connect to database: {e}")
258
- return
259
 
260
- demo = gr.Blocks()
261
 
262
  with demo:
263
  gr.Markdown("# Website Monitor and Chatbot")
@@ -318,23 +319,40 @@ async def main():
318
  send_button.click(
319
  chatbot_response,
320
  inputs=[message_input, chatbot_interface],
321
- outputs=[chatbot_interface, message_input])
322
-
323
-
324
 
325
- asyncio.create_task(periodic_update_with_error_handling(db_session))
326
- asyncio.create_task(update_db_status(db_status))
327
-
328
- feed_updater = gr.Timer(300) # 5 minutes
329
  feed_updater.tick(fn=update_feed_content,
330
  outputs=feed_content)
331
- try:
 
 
 
 
 
 
 
332
  await demo.launch()
 
 
 
 
 
 
 
 
333
  finally:
334
  if db_session:
335
  await db_session.close()
 
336
  engine.dispose()
337
 
 
 
 
338
 
339
  if __name__ == "__main__":
 
 
340
  asyncio.run(main())
 
4
  import os
5
  from typing import List, Tuple, Dict, Any, Optional
6
  import datetime
7
+ import signal
8
  import feedparser
9
  import aiohttp
10
  import gradio as gr
 
14
  from sqlalchemy.orm import declarative_base, sessionmaker
15
  from urllib.parse import urljoin
16
  import logging
17
+ from sqlalchemy.orm import Session
18
+ from sqlalchemy.future import select
19
+
20
  Base = declarative_base()
21
 
22
  class Article(Base):
 
257
  try:
258
  engine, Session = await create_db_engine("sqlite:///monitoring.db")
259
  db_session = Session()
 
 
 
260
 
261
+ demo = gr.Blocks()
262
 
263
  with demo:
264
  gr.Markdown("# Website Monitor and Chatbot")
 
319
  send_button.click(
320
  chatbot_response,
321
  inputs=[message_input, chatbot_interface],
322
+ outputs=[chatbot_interface, message_input])
 
 
323
 
324
+ # Set up the timer
325
+ feed_updater = gr.Timer(interval=300)
 
 
326
  feed_updater.tick(fn=update_feed_content,
327
  outputs=feed_content)
328
+
329
+ # Create background tasks
330
+ update_tasks = [
331
+ asyncio.create_task(periodic_update_with_error_handling(db_session)),
332
+ asyncio.create_task(update_db_status(db_status))
333
+ ]
334
+
335
+ # Launch the demo
336
  await demo.launch()
337
+
338
+ # Wait for background tasks to complete
339
+ for task in update_tasks:
340
+ task.cancel()
341
+ await asyncio.gather(*update_tasks, return_exceptions=True)
342
+
343
+ except Exception as e:
344
+ logger.error(f"Error in main: {e}")
345
  finally:
346
  if db_session:
347
  await db_session.close()
348
+ if engine:
349
  engine.dispose()
350
 
351
+ def signal_handler():
352
+ for task in asyncio.all_tasks():
353
+ task.cancel()
354
 
355
  if __name__ == "__main__":
356
+ signal.signal(signal.SIGINT, signal_handler)
357
+ signal.signal(signal.SIGTERM, signal_handler)
358
  asyncio.run(main())