-
Notifications
You must be signed in to change notification settings - Fork 0
/
general_utils.py
66 lines (52 loc) · 2.26 KB
/
general_utils.py
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
# SPDX-FileCopyrightText: 2022 Renaissance Computing Institute. All rights reserved.
# SPDX-FileCopyrightText: 2023 Renaissance Computing Institute. All rights reserved.
#
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-License-Identifier: LicenseRef-RENCI
# SPDX-License-Identifier: MIT
"""
General Utils - Various utilities common to this project's components.
Authors: Lisa Stillwell, Phil Owen @RENCI.org
"""
import os
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
class GeneralUtils:
"""
Utility methods used for components in this project.
"""
def __init__(self, _logger):
"""
Initializes this class
"""
self.logger = _logger
# init the Slack channels
self.slack_channels: dict = {'slack_status_channel': os.getenv('SLACK_STATUS_CHANNEL'),
'slack_issues_channel': os.getenv('SLACK_ISSUES_CHANNEL')}
# get the environment this instance is running on
self.system = os.getenv('SYSTEM', 'System name not set')
def send_slack_msg(self, msg, channel, debug_mode=False):
"""
sends a msg to the Slack channel
:param msg: the msg to be sent
:param channel: the Slack channel to post the message to
:param debug_mode: mode to indicate that this is a no-op
:return: nothing
"""
# init the final msg
final_msg = f"APSViz Msg Handler ({self.system}) - {msg}"
# log the message
self.logger.info(final_msg)
# send the message to Slack if not in debug mode and not running locally
if not debug_mode and self.system in ['Dev', 'Prod', 'AWS/EKS']:
# determine the client based on the channel
if channel == 'slack_status_channel':
client = WebClient(token=os.getenv('SLACK_STATUS_TOKEN'))
else:
client = WebClient(token=os.getenv('SLACK_ISSUES_TOKEN'))
try:
# send the message
client.chat_postMessage(channel=self.slack_channels[channel], text=final_msg)
except SlackApiError:
# log the error
self.logger.exception('Slack %s messaging failed. msg: %s', self.slack_channels[channel], final_msg)