Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lock-sync-task #305

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
lock-sync-task
  • Loading branch information
dcritch committed Oct 31, 2024
commit 699fc827447bc5420002b27de0da028218df56a0
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
4 changes: 3 additions & 1 deletion dashboard/src/t5gweb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ 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")
cfg["alert_email"] = os.environ.get("alert_email")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alert_email is set to cfg['alert_to'] above, maybe we should unify these two

return cfg


Expand Down Expand Up @@ -307,7 +309,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