Skip to content
This repository was archived by the owner on Mar 13, 2026. It is now read-only.

Commit 7a34c92

Browse files
duhowgerardsegarra
andauthored
feat: monitor queued jobs (#18)
* feat: monitor queued jobs * add test requirements * change to one-line extract job Co-authored-by: Gerard Segarra <[email protected]> --------- Co-authored-by: Gerard Segarra <[email protected]>
1 parent 992bfd8 commit 7a34c92

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
[metadata]
22
name = github-workflows-monitoring
3-
version = 0.1.3
3+
version = 0.2.0
44
license-file = LICENSE
55

66
[options]
77
python_requires = >=3.8
88
packages = find:
99
install_requires =
1010
Flask>=2.2,<3
11+
Flask-APScheduler==1.13.1
1112

1213
[flake8]
1314
max-line-length = 120

src/app.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55

66
from flask import Flask, abort, request
7+
from flask_apscheduler import APScheduler
78

89

910
from const import GithubHeaders, LOGGING_CONFIG
@@ -12,6 +13,8 @@
1213
dictConfig(LOGGING_CONFIG)
1314

1415
app = Flask(__name__)
16+
scheduler = APScheduler()
17+
scheduler.init_app(app)
1518

1619
# set to WARNING to disable access log
1720
log = logging.getLogger("werkzeug")
@@ -131,7 +134,31 @@ def process_workflow_job():
131134
return True
132135

133136

137+
@scheduler.task('interval', id='monitor_queued', seconds=30)
138+
def monitor_queued_jobs():
139+
""" Return the job that has been queued and not starting for long time. """
140+
app.logger.debug("Starting monitor_queued_jobs")
141+
if not jobs:
142+
return
143+
144+
job_id, time_start = min(jobs.items(), key=lambda x: x[1])
145+
delay = datetime.now().timestamp() - time_start
146+
147+
if delay <= int(os.getenv("QUEUED_JOBS_DELAY_THRESHOLD", 150)):
148+
return
149+
150+
context_details = {
151+
"action": "monitor_queued",
152+
"job_id": job_id,
153+
"started_at": time_start,
154+
"delay": delay,
155+
}
156+
157+
app.logger.info(dict_to_logfmt(context_details))
158+
159+
134160
allowed_events = {"workflow_job": process_workflow_job}
161+
scheduler.start()
135162

136163

137164
@app.route("/github-webhook", methods=["POST"])

tests/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Flask
2+
Flask-APScheduler==1.13.1
23
pytest
34
pytest-cov
45
flake8

0 commit comments

Comments
 (0)