-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrunner.py
More file actions
84 lines (75 loc) · 2.94 KB
/
runner.py
File metadata and controls
84 lines (75 loc) · 2.94 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
"""
Slack Event Handler runner.
Runs the unified Slack listener (huddle transcript tracking + Slack PR comment bot).
Supports multiple teams: one listener per team in SLACK_BOT_TOKEN, each in its own thread.
"""
import logging
import threading
from django.conf import settings
from config.workspace import WORKSPACE_PATH_SETUP_ERRORS
from slack_event_handler.workspace import get_workspace_root
from core.operations.slack_ops import get_slack_app_token
logger = logging.getLogger(__name__)
def run_slack_event_handler(bot_token=None, app_token=None):
"""
Main entry point for the unified Slack Event Handler.
If multiple teams are configured (SLACK_TEAM_IDS + SLACK_BOT_TOKEN_<id>), starts one
listener per team in a separate thread. Otherwise uses default team key (single/first in SLACK_TEAM_IDS).
"""
# Best-effort debug log only; matches exceptions from config.workspace.get_workspace_path.
try:
root = get_workspace_root()
logger.debug("Slack Event Handler workspace root: %s", root)
except WORKSPACE_PATH_SETUP_ERRORS as e:
logger.exception("Failed to resolve workspace root: %s", e)
tokens_map = getattr(settings, "SLACK_BOT_TOKEN", None) or {}
if not isinstance(tokens_map, dict):
tokens_map = {}
if tokens_map:
# Multiple (or single) teams from SLACK_TEAM_IDS + SLACK_BOT_TOKEN_<id> and SLACK_APP_TOKEN_<id>
from slack_event_handler.utils.slack_listener import start_slack_listener
listeners = []
started = 0
for team_id, token in tokens_map.items():
token = (token or "").strip()
if not token:
continue
try:
team_app_token = (app_token or "").strip() or get_slack_app_token(
team_id
)
except ValueError:
logger.warning(
"Skipping team %s: SLACK_APP_TOKEN_%s not set in .env",
team_id,
team_id,
)
continue
logger.info("Starting Slack Event Listener for team=%s", team_id)
t = threading.Thread(
target=start_slack_listener,
kwargs={
"bot_token": token,
"app_token": team_app_token,
"team_id": team_id,
},
daemon=True,
name=f"slack-listener-{team_id}",
)
t.start()
listeners.append(t)
started += 1
if started == 0:
logger.error(
"No valid team with both SLACK_BOT_TOKEN_<id> and SLACK_APP_TOKEN_<id> in .env"
)
else:
for t in listeners:
t.join()
else:
msg = (
"No teams configured. Set SLACK_TEAM_IDS and SLACK_BOT_TOKEN_<id> "
"(and SLACK_APP_TOKEN_<id>) in .env."
)
logger.error("%s", msg)
return