Skip to content

Commit

Permalink
adding slack, tidying up log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
PhillipsOwen committed Oct 31, 2022
1 parent 9d16d4e commit f3c1003
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
17 changes: 13 additions & 4 deletions src/archiver/archiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ def run(self) -> bool:
rule_def_version = rule_defs['rule_definition_version']

self.logger.info('<---------- New Run: %s ---------->', infile)
self.logger.info('APSViz Archiver start. Name: %s, Version: %s', rule_def_name, rule_def_version)

# create a start message
start_msg = f'APSViz Archiver start. Name: {rule_def_name}, Version: {rule_def_version}'

# send/log the start message
self.utils.send_slack_msg(start_msg, 'slack_status_channel')
self.logger.info(start_msg)

# process the rule set
for rule_set in rule_defs['rule_sets']:
Expand All @@ -90,17 +96,20 @@ def run(self) -> bool:

# no failures get a short message
if run_stats['failed'] == 0:
final_msg = 'status: Success.'
final_msg = f"Status: {len(rule_set['rules'])} rule(s) succeeded."

# set the success flag
ret_val = True
else:
# show all results on failure
final_msg = f"Failures detected: {len(rule_set['rules'])} rule(s) in set, {run_stats['moved']} Move rule(s), " \
final_msg = f"Status: Failures detected - {len(rule_set['rules'])} rule(s) in set, {run_stats['moved']} Move rule(s), " \
f"{run_stats['copied']} copy rule(s), {run_stats['removed']} remove rule(s), " \
f"{run_stats['swept']} sweep rule(s), {run_stats['failed']} failed rule(s)."

self.logger.info("Rule set complete. Run %s", final_msg)
self.logger.info("APSVix-Archiver Rule set %s complete. Run %s", rule_set['rule_set_name'], final_msg)

# send out a slack of the details
self.utils.send_slack_msg(final_msg, 'slack_status_channel')

self.logger.info('<---------- Run complete: %s ---------->\n', infile)
except Exception:
Expand Down
10 changes: 5 additions & 5 deletions src/common/general_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def send_slack_msg(self, msg, channel, debug_mode=False):
"""
sends a msg to the Slack channel
:param msg: the msg tpo be sent
: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
:param debug_mode: mode to indicate that this is a no-op
:return: nothing
"""
# init the final msg
Expand All @@ -63,17 +63,17 @@ def send_slack_msg(self, msg, channel, debug_mode=False):
# 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 == self.slack_channels['slack_status_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=channel, text=final_msg)
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', channel, final_msg)
self.logger.exception('Slack %s messaging failed. msg: %s', self.slack_channels[channel], final_msg)

@staticmethod
def load_rule_definition_file(infile: str) -> dict:
Expand Down

0 comments on commit f3c1003

Please sign in to comment.