Skip to content

Commit

Permalink
Moved send_slack_message function to utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
n2iw committed Dec 20, 2019
1 parent 63fabd3 commit fa01966
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
13 changes: 1 addition & 12 deletions common/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from configparser import ConfigParser
import os, sys

from requests import post
import yaml

from .utils import get_logger
Expand Down Expand Up @@ -39,7 +38,6 @@

PROP_FILE_ENV_VAR = 'ICDC_DATA_LOADER_PROP'
property_file = os.environ.get(PROP_FILE_ENV_VAR, 'config/props.yml')
util_log = get_logger('Utils')
if property_file and os.path.isfile(property_file):
with open(property_file) as prop_file:
PROPS = yaml.safe_load(prop_file)['Properties']
Expand All @@ -52,13 +50,4 @@
PROP_FILE_ENV_VAR))
sys.exit(1)

def send_slack_message(messaage, log):
if SLACK_URL:
headers = {"Content-type": "application/json"}
result = post(SLACK_URL, json=messaage, headers=headers)
if not result or result.status_code != 200:
log.error('Sending Slack messages failed!')
if result:
log.error(result.content)
else:
log.error('Slack URL not set in configuration file!')

14 changes: 14 additions & 0 deletions common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import re
from urllib.parse import urlparse

from requests import post


def get_logger(name):
formatter = logging.Formatter('%(asctime)s %(levelname)s: (%(name)s) - %(message)s')
Expand Down Expand Up @@ -42,6 +44,18 @@ def check_schema_files(schemas, log):
return True


def send_slack_message(url, messaage, log):
if url:
headers = {"Content-type": "application/json"}
result = post(url, json=messaage, headers=headers)
if not result or result.status_code != 200:
log.error('Sending Slack messages failed!')
if result:
log.error(result.content)
else:
log.error('Slack URL not set in configuration file!')


NODES_CREATED = 'nodes_created'
RELATIONSHIP_CREATED = 'relationship_created'
NODES_DELETED = 'nodes_deleted'
Expand Down
6 changes: 3 additions & 3 deletions file_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
from botocore.exceptions import ClientError

from common.utils import UUID, NODES_CREATED, RELATIONSHIP_CREATED, removeTrailingSlash,\
get_logger, UPSERT_MODE
get_logger, UPSERT_MODE, send_slack_message
from common.config import INDEXD_GUID_PREFIX, INDEXD_MANIFEST_EXT, VISIBILITY_TIMEOUT, \
TEMP_FOLDER, PSWD_ENV, send_slack_message
TEMP_FOLDER, PSWD_ENV, SLACK_URL
from common.sqs import Queue, VisibilityExtender
from common.data_loader import DataLoader
from common.icdc_schema import ICDC_Schema, get_uuid_for_node
Expand Down Expand Up @@ -412,7 +412,7 @@ def send_success_email(self, file_name, final_path, file_list, manifests, loadin
content += '*Running time: {:.2f} seconds*\n'.format(running_time)

self.log.info('Sending success message to Slack ...')
send_slack_message({"text": content}, self.log)
send_slack_message(SLACK_URL, {"text": content}, self.log)

self.log.info('Success message sent')

Expand Down

0 comments on commit fa01966

Please sign in to comment.