Skip to content

Commit e34b489

Browse files
committed
add dashboard controller
1 parent 0dba58b commit e34b489

9 files changed

+131
-5
lines changed

.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
REDIS_HOST=localhost
22
REDIS_PORT=6379
33
REDIS_DB=0
4-
RQ_POLL_INTERVAL=2500
4+
RQ_POLL_INTERVAL=2500
5+
RQ_QUEUES=low,medium,high

app/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
app = Flask(__name__, static_folder='static', template_folder='templates')
88
app.config.from_object(Config)
99

10+
from app import dashboard_controller
1011
from app import routes

app/dashboard_controller.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from flask import current_app, url_for
2+
3+
from rq import (Queue, Worker, cancel_job, get_failed_queue, pop_connection, push_connection, requeue_job)
4+
from rq.job import Job
5+
from rq_scheduler import Scheduler
6+
7+
8+
from app import app
9+
10+
11+
@app.before_first_request
12+
def setup_rq_connection():
13+
redis_host = current_app.config.get("REDIS_HOST")
14+
redis_port = current_app.config.get("REDIS_PORT")
15+
redis_db = current_app.config.get("REDIS_DB")
16+
queue_list = current_app.config.get("RQ_QUEUES")
17+
18+
print(f'connecting to Redis {redis_host}:{redis_port}/{redis_db}')
19+
print(f'monitoring a list of queue: {queue_list}')
20+
21+
22+
@app.before_request
23+
def prepare_rq_connection():
24+
pass
25+
26+
27+
@app.teardown_request
28+
def release_rq_connection(exception=None):
29+
pass
30+
31+

app/routes.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
1-
from flask import render_template, redirect, url_for
1+
from flask import current_app, render_template, redirect, url_for
22

33
from app import app
44

55

6+
def _get_queue_list():
7+
rq_queues = current_app.config.get("RQ_QUEUES")
8+
9+
if rq_queues.find(",") >= 0:
10+
return rq_queues.split(",")
11+
else:
12+
return [rq_queues]
13+
14+
615
@app.route('/')
716
@app.route('/index')
817
def index():
9-
return render_template('index.html')
18+
rq_queues = _get_queue_list()
19+
rq_workers = _get_queue_list()
20+
return render_template('index.html', rq_queues=rq_queues, rq_workers=rq_workers)
1021

1122

1223
@app.route('/queues')
1324
def queues():
14-
return render_template('queues.html')
25+
rq_queues = _get_queue_list()
26+
return render_template('queues.html', rq_queues=rq_queues)
1527

1628

1729
@app.route('/workers')
1830
def workers():
19-
return render_template('workers.html')
31+
rq_workers = _get_queue_list()
32+
return render_template('workers.html', rq_workers=rq_workers)
2033

2134

2235
@app.route('/jobs')

app/templates/index.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ <h3 class="card-title">Queues</h3>
3535
<th>Jobs</th>
3636
</tr>
3737
</thead>
38+
<tbody>
39+
{% for rq_queue in rq_queues %}
40+
<tr>
41+
<td>{{ rq_queue }}</td>
42+
<td>0</td>
43+
</tr>
44+
{% endfor %}
45+
</tbody>
3846
</table>
3947
</div>
4048
</div>
@@ -53,6 +61,15 @@ <h3 class="card-title">Workers</h3>
5361
<th>State</th>
5462
</tr>
5563
</thead>
64+
<tbody>
65+
{% for rq_worker in rq_workers %}
66+
<tr>
67+
<td>{{ rq_worker }}</td>
68+
<td>{{ rq_worker }}</td>
69+
<td>&nbsp;</td>
70+
</tr>
71+
{% endfor %}
72+
</tbody>
5673
</table>
5774
</div>
5875
</div>

app/templates/queues.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ <h3 class="card-title">Queues</h3>
3535
<th>Jobs</th>
3636
</tr>
3737
</thead>
38+
<tbody>
39+
{% for rq_queue in rq_queues %}
40+
<tr>
41+
<td>{{ rq_queue }}</td>
42+
<td>0</td>
43+
</tr>
44+
{% endfor %}
45+
</tbody>
3846
</table>
3947
</div>
4048
</div>

app/templates/workers.html

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{% extends "base.html" %}
2+
3+
{% block nav_items %}
4+
<ul class="navbar-nav">
5+
<li class="nav-item">
6+
<a class="nav-link" href="/">Home</a>
7+
</li>
8+
<li class="nav-item active">
9+
<a class="nav-link" href="/workers">Workers <span class="sr-only">(current)</span></a>
10+
</li>
11+
<li class="nav-item">
12+
<a class="nav-link" href="/queues">Queues</a>
13+
</li>
14+
<li class="nav-item">
15+
<a class="nav-link" href="/jobs">Instant Jobs</a>
16+
</li>
17+
<li class="nav-item">
18+
<a class="nav-link" href="/schedulers">Scheduled Jobs</a>
19+
</li>
20+
</ul>
21+
{% endblock %}
22+
23+
{% block main_section %}
24+
<div class="row mb-3">
25+
<div class="col-10 offset-1">
26+
<div class="card">
27+
<div class="card-header">
28+
<h3 class="card-title">Workers</h3>
29+
</div>
30+
<div class="card-body">
31+
<table class="table table-hover table-bordered">
32+
<thead class="thead-light">
33+
<tr>
34+
<th>Worker</th>
35+
<th>Queue</th>
36+
<th>State</th>
37+
</tr>
38+
</thead>
39+
<tbody>
40+
{% for rq_worker in rq_workers %}
41+
<tr>
42+
<td>{{ rq_worker }}</td>
43+
<td>{{ rq_worker }}</td>
44+
<td>&nbsp;</td>
45+
</tr>
46+
{% endfor %}
47+
</tbody>
48+
</table>
49+
</div>
50+
</div>
51+
</div>
52+
</div>
53+
{% endblock %}

config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ class Config(object):
1616
REDIS_HOST = os.environ.get('REDIS_HOST') or "localhost"
1717
REDIS_PORT = os.environ.get('REDIS_PORT') or "6379"
1818
REDIS_DB = os.environ.get('REDIS_DB') or "0"
19+
RQ_QUEUES = os.environ.get('RQ_QUEUES') or "default"
1920
RQ_POLL_INTERVAL = os.environ.get('RQ_POLL_INTERVAL') or "2500"

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ python-dateutil # via arrow
99
python-dotenv
1010
redis
1111
rq
12+
rq-scheduler
1213
six
1314
werkzeug # via flask
1415
gunicorn

0 commit comments

Comments
 (0)