Skip to content

Commit

Permalink
Merge pull request #305 from RHsyseng/block-sync-task
Browse files Browse the repository at this point in the history
lock-sync-task
  • Loading branch information
adhil0 authored Oct 31, 2024
2 parents 599e384 + 55915b5 commit 5aa6536
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 56 deletions.
59 changes: 59 additions & 0 deletions dashboard/src/t5gweb/libtelco5g.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
get_random_member,
get_token,
make_headers,
set_cfg,
email_notify,
slack_notify,
)

# for portal to jira mapping
Expand All @@ -53,6 +56,7 @@


def jira_connection(cfg):
""" initiate a connection to the JIRA server"""
jira = JIRA(server=cfg["server"], token_auth=cfg["password"])

return jira
Expand Down Expand Up @@ -734,6 +738,61 @@ def sync_priority(cfg):
return out_of_sync


def sync_portal_to_jira():

cfg = set_cfg()

start = time.time()
cases = redis_get("cases")
cards = redis_get("cards")

open_cases = [case for case in cases if cases[case]["status"] != "Closed"]
card_cases = [cards[card]["case_number"] for card in cards]
logging.warning("found {} cases in JIRA".format(len(card_cases)))
new_cases = [case for case in open_cases if case not in card_cases]
logging.warning("new cases: {}".format(new_cases))

response = {"cards_created": 0}

if len(new_cases) > int(cfg["max_to_create"]):
logging.warning(
(
f"Warning: more than {cfg['max_to_create']} cases ({len(new_cases)}) "
f"will be created, so refusing to proceed. Please check log output\n"
)
)
email_content = [
(
f"Warning: more than {cfg['max_to_create']} cases ({len(new_cases)})"
f"will be created, so refusing to proceed. Please check log output\n"
)
]
email_content += ['New cases: {}\n"'.format(new_cases)]
cfg["to"] = cfg["alert_email"]
cfg["subject"] = "High New Case Count Detected"
email_notify(cfg, email_content)
elif len(new_cases) > 0:
logging.warning("need to create {} cases".format(len(new_cases)))
message_content, new_cards = create_cards(cfg, new_cases, action="create")
if message_content:
logging.warning("notifying team about new JIRA cards")
cfg["subject"] += ": {}".format(", ".join(new_cases))
email_notify(cfg, message_content)
if cfg["slack_token"] and cfg["slack_channel"]:
slack_notify(cfg, message_content)
else:
logging.warning("no slack token or channel specified")
cards.update(new_cards)
redis_set("cards", json.dumps(cards))
response = {"cards_created": len(new_cases)}
else:
logging.warning("no new cards required")

end = time.time()
logging.warning("synced to jira in {} seconds".format(end - start))
return response


def main():
print("libtelco5g")

Expand Down
65 changes: 14 additions & 51 deletions dashboard/src/t5gweb/taskmgr.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""start celery and manage tasks"""

import json
import logging
import os
Expand Down Expand Up @@ -115,58 +116,20 @@ def setup_scheduled_tasks(sender, **kwargs):

@mgr.task
def portal_jira_sync():
logging.warning("job: checking for new cases")
cfg = set_cfg()
max_to_create = os.environ.get("max_to_create")

start = time.time()

cases = libtelco5g.redis_get("cases")
cards = libtelco5g.redis_get("cards")

open_cases = [case for case in cases if cases[case]["status"] != "Closed"]
card_cases = [cards[card]["case_number"] for card in cards]
logging.warning("found {} cases in JIRA".format(len(card_cases)))
new_cases = [case for case in open_cases if case not in card_cases]
logging.warning("new cases: {}".format(new_cases))

if len(new_cases) > int(max_to_create):
logging.warning(
(
f"Warning: more than {max_to_create} cases ({len(new_cases)}) "
f"will be created, so refusing to proceed. Please check log output\n"
)
)
email_content = [
(
f"Warning: more than {max_to_create} cases ({len(new_cases)})"
f"will be created, so refusing to proceed. Please check log output\n"
)
]
email_content += ['New cases: {}\n"'.format(new_cases)]
cfg["to"] = os.environ.get("alert_email")
cfg["subject"] = "High New Case Count Detected"
email_notify(cfg, email_content)
elif len(new_cases) > 0:
logging.warning("need to create {} cases".format(len(new_cases)))
message_content, new_cards = libtelco5g.create_cards(
cfg, new_cases, action="create"
)
if message_content:
logging.warning("notifying team about new JIRA cards")
cfg["subject"] += ": {}".format(", ".join(new_cases))
email_notify(cfg, message_content)
if cfg["slack_token"] and cfg["slack_channel"]:
slack_notify(cfg, message_content)
else:
logging.warning("no slack token or channel specified")
cards.update(new_cards)
libtelco5g.redis_set("cards", json.dumps(cards))
else:
logging.warning("no new cards required")

end = time.time()
logging.warning("synced to jira in {} seconds".format(end - start))
logging.warning("job: checking for new cases")
have_lock = False
sync_lock = redis.Redis(host="redis").lock("sync_lock", timeout=60 * 60 * 2)
try:
have_lock = sync_lock.acquire(blocking=False)
if have_lock:
response = libtelco5g.sync_portal_to_jira()
else:
response = {"locked": "Task is Locked"}
finally:
if have_lock:
sync_lock.release()
return response


@mgr.task(autoretry_for=(Exception,), max_retries=5, retry_backoff=30)
Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/t5gweb/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def login():
error_reason=error_reason,
not_auth_warn=not_auth_warn,
wrong_permissions=wrong_permissions,
alert_email=cfg["alert_to"],
alert_email=cfg["alert_email"],
)
# JIT Provisioning
if attributes["rhatUUID"][0] not in users:
Expand Down
7 changes: 4 additions & 3 deletions dashboard/src/t5gweb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ def set_cfg():
cfg["smtp"] = os.environ.get("smtp_server")
cfg["from"] = os.environ.get("source_email")
cfg["to"] = os.environ.get("notification_email")
cfg["alert_to"] = os.environ.get("alert_email")
cfg["subject"] = os.environ.get("email_subject")
cfg["alert_email"] = os.environ.get("alert_email")
# slack
cfg["slack_token"] = os.environ.get("slack_token")
cfg["slack_channel"] = os.environ.get("slack_channel")
Expand All @@ -190,6 +190,7 @@ def set_cfg():
cfg["sla_settings"] = json.loads(os.environ.get("sla_settings"))
# sso
cfg["rbac"] = os.environ.get("rbac").split(",") if os.environ.get("rbac") else []
cfg["max_to_create"] = os.environ.get("max_to_create")
return cfg


Expand All @@ -199,7 +200,7 @@ def set_defaults():
defaults["smtp"] = "localhost"
defaults["from"] = "[email protected]"
defaults["to"] = ""
defaults["alert_to"] = "root@localhost"
defaults["alert_email"] = "root@localhost"
defaults["subject"] = "New Card(s) Have Been Created to Track Issues"
defaults["sprintname"] = ""
defaults["server"] = ""
Expand Down Expand Up @@ -307,7 +308,7 @@ def get_fake_data(path="data/fake_data.json"):
dict: Dictionary that contains deserialized JSON data
"""
path = os.path.abspath(path)
with open(path) as fake_data:
with open(path, encoding="utf-8") as fake_data:
data = json.load(fake_data)
return data

Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_set_default():
assert defaults["smtp"] == "localhost"
assert defaults["from"] == "[email protected]"
assert defaults["to"] == ""
assert defaults["alert_to"] == "root@localhost"
assert defaults["alert_email"] == "root@localhost"
assert defaults["subject"] == "New Card(s) Have Been Created to Track Issues"
assert defaults["sprintname"] == ""
assert defaults["server"] == ""
Expand Down

0 comments on commit 5aa6536

Please sign in to comment.