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

feat: monitor queued jobs #18

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[metadata]
name = github-workflows-monitoring
version = 0.1.3
version = 0.2.0
license-file = LICENSE

[options]
python_requires = >=3.8
packages = find:
install_requires =
Flask>=2.2,<3
Flask-APScheduler==1.13.1

[flake8]
max-line-length = 120
Expand Down
28 changes: 28 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os

from flask import Flask, abort, request
from flask_apscheduler import APScheduler


from const import GithubHeaders, LOGGING_CONFIG
Expand All @@ -12,6 +13,8 @@
dictConfig(LOGGING_CONFIG)

app = Flask(__name__)
scheduler = APScheduler()
scheduler.init_app(app)

# set to WARNING to disable access log
log = logging.getLogger("werkzeug")
Expand Down Expand Up @@ -131,7 +134,32 @@ def process_workflow_job():
return True


@scheduler.task('interval', id='monitor_queued', seconds=30)
def monitor_queued_jobs():
""" Return the job that has been queued and not starting for long time. """
app.logger.debug("Starting monitor_queued_jobs")
if not jobs:
return

old_queued_job = min(jobs.items(), key=lambda x: x[1])
job_id, time_start = old_queued_job
delay = datetime.now().timestamp() - time_start

if delay <= int(os.getenv("QUEUED_JOBS_DELAY_THRESHOLD", 150)):
return

context_details = {
"action": "monitor_queued",
"job_id": job_id,
"started_at": time_start,
"delay": delay,
}

app.logger.info(dict_to_logfmt(context_details))


allowed_events = {"workflow_job": process_workflow_job}
scheduler.start()


@app.route("/github-webhook", methods=["POST"])
Expand Down
1 change: 1 addition & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Flask
Flask-APScheduler==1.13.1
pytest
pytest-cov
flake8
Loading