-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
86 lines (68 loc) · 2.88 KB
/
app.py
File metadata and controls
86 lines (68 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import asyncio
from typing import Optional
from bot import FlyMeBot
from slack import create_slack_app, setup_slack_handlers, create_socket_handler
from graceful import GracefulShutdown
from config import Config, setup_logging
class FlyMeApp:
"""Main application class that orchestrates all components"""
def __init__(self, config: Config):
self.config = config
self.logger = setup_logging(config.log_level)
self.bot: Optional[FlyMeBot] = None
self.slack_app = None
self.handler = None
self.shutdown = GracefulShutdown()
async def initialize(self):
"""Initialize all application components"""
self.logger.info("Launching FlyMe application...")
# Create Slack app
self.slack_app = create_slack_app()
# Initialize bot with Slack client
self.bot = FlyMeBot(slack_client=self.slack_app.client)
await self.bot.initialize()
# Set up Slack handlers
setup_slack_handlers(self.slack_app, self.bot)
# Create Socket Mode handler
self.handler = await create_socket_handler(self.slack_app)
# Set up graceful shutdown
self.shutdown.add_handler(self.cleanup)
self.shutdown.setup_signal_handlers()
self.logger.info("FlyMe application initialized successfully")
async def cleanup(self):
"""Clean up resources during shutdown"""
if self.handler:
await self.handler.close_async()
async def run(self):
"""Run the application"""
self.print_banner()
try:
# Create tasks for both the handler and shutdown wait
handler_task = asyncio.create_task(self.handler.start_async())
shutdown_task = asyncio.create_task(self.shutdown.wait_for_shutdown())
# Wait for either the handler to stop or shutdown signal
done, pending = await asyncio.wait(
[handler_task, shutdown_task],
return_when=asyncio.FIRST_COMPLETED
)
# Cancel any pending tasks
for task in pending:
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
except KeyboardInterrupt:
self.logger.info("Received keyboard interrupt")
except Exception as e:
self.logger.error(f"Unexpected error: {e}")
raise
finally:
await self.shutdown.shutdown()
def print_banner(self):
"""Print application banner"""
print("\n" + "="*50)
print("🚀 FlyMe - Find Flights in Slack!")
print("="*50)
print("Bot is listening for messages... (Press Ctrl+C to stop)")
print("="*50 + "\n")