Skip to content

Commit

Permalink
Merge pull request #818 from openzim/add_notification_timeout
Browse files Browse the repository at this point in the history
Add notification timeout
  • Loading branch information
benoit74 authored Sep 18, 2023
2 parents 3427596 + 4026bc4 commit 996fc8e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
65 changes: 65 additions & 0 deletions dispatcher/backend/maint-scripts/list_running_tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import sys

import requests
from get_token import get_token, get_token_headers, get_url

running_statuses = [
"reserved",
"started",
"scraper_started",
"scraper_completed",
"scraper_killed",
"cancel_requested",
]


def get_status_query():
filters = [f"status={status}" for status in running_statuses]
return "&".join(filters)


def get_timestamp(task, status):
if status in task["timestamp"]:
return task["timestamp"][status]
else:
return "-"


def main(username, password):
"""Print in STDOUT a markdown table of running tasks with various information"""
access_token, refresh_token = get_token(username, password)
response = requests.get(
f"{get_url('/tasks')}?{get_status_query()}",
headers=get_token_headers(access_token),
)
tasks = response.json()["items"]
print(
"| Task ID | worker | kind | DB Status | last update at | requested"
" | reserved | started | scraper_started | scraper_completed |"
)
print("|--|--|--|--|--|--|--|--|--|--|")
for task in sorted(tasks, key=lambda task: task["updated_at"]):
response = requests.get(
f"{get_url('/tasks')}/{task['_id']}",
headers=get_token_headers(access_token),
)
task_details = response.json()

print(
f"| [{task['_id']}](https://farm.openzim.org/pipeline/{task['_id']}) "
f"| {task['worker']} "
f"| {task_details['config']['task_name']} "
f"| {task['status']} "
f"| {task['updated_at'][:19]} "
f"| {get_timestamp(task, 'requested')[:19]} "
f"| {get_timestamp(task, 'reserved')[:19]} "
f"| {get_timestamp(task, 'started')[:19]} "
f"| {get_timestamp(task, 'scraper_started')[:19]} "
f"| {get_timestamp(task, 'scraper_completed')[:19]} "
f"|"
)


if __name__ == "__main__":
args = sys.argv[1:]
main(*args)
2 changes: 2 additions & 0 deletions dispatcher/backend/src/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

# NOTIFICATIONS

WEB_NOTIFICATIONS_TIMEOUT = int(os.getenv("WEB_NOTIFICATIONS_TIMEOUT", 5))

# in-notification URLs
PUBLIC_URL = os.getenv("PUBLIC_URL", "https://farm.openzim.org")
ZIM_DOWNLOAD_URL = os.getenv(
Expand Down
8 changes: 7 additions & 1 deletion dispatcher/backend/src/common/emailing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import requests
from werkzeug.datastructures import MultiDict

from common.constants import MAILGUN_API_KEY, MAILGUN_API_URL, MAILGUN_FROM
from common.constants import (
MAILGUN_API_KEY,
MAILGUN_API_URL,
MAILGUN_FROM,
WEB_NOTIFICATIONS_TIMEOUT,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -52,6 +57,7 @@ def send_email_via_mailgun(
]
if attachments
else [],
timeout=WEB_NOTIFICATIONS_TIMEOUT,
)
resp.raise_for_status()
except Exception as exc:
Expand Down
3 changes: 3 additions & 0 deletions dispatcher/backend/src/common/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
SLACK_ICON,
SLACK_URL,
SLACK_USERNAME,
WEB_NOTIFICATIONS_TIMEOUT,
ZIM_DOWNLOAD_URL,
)
from common.emailing import send_email_via_mailgun
Expand Down Expand Up @@ -95,6 +96,7 @@ def handle_webhook_notification(task, urls):
url,
data=dumps(task).encode("UTF-8"),
headers={"Content-Type": "application/json"},
timeout=WEB_NOTIFICATIONS_TIMEOUT,
)
resp.raise_for_status()
except Exception as exc:
Expand All @@ -112,6 +114,7 @@ def handle_slack_notification(task, channels):
try:
requests.post(
SLACK_URL,
timeout=WEB_NOTIFICATIONS_TIMEOUT,
json={
# destination. prefix with # for chans or @ for account
"channel": channel,
Expand Down

0 comments on commit 996fc8e

Please sign in to comment.