Skip to content

Commit

Permalink
feat: monitor queued jobs (#18)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
duhow and gerardsegarra authored Apr 23, 2024
1 parent 992bfd8 commit 7a34c92
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
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
27 changes: 27 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,31 @@ 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

job_id, time_start = min(jobs.items(), key=lambda x: x[1])
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

0 comments on commit 7a34c92

Please sign in to comment.