Skip to content

Commit

Permalink
Merge pull request #478 from peter4431/master
Browse files Browse the repository at this point in the history
Add option for JSONSerializer
  • Loading branch information
cjlapao authored Jan 5, 2024
2 parents 1bfe511 + 0b6823a commit ab62651
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Options:
--poll-interval, --interval INTEGER
Refresh interval in ms
--extra-path TEXT Append specified directories to sys.path
-j, --json Use JSONSerializer
--debug / --normal Enter DEBUG mode
-v, --verbose Enable verbose logging
--help Show this message and exit.
Expand Down
9 changes: 9 additions & 0 deletions rq_dashboard/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from . import default_settings
from .version import VERSION
from .web import blueprint, setup_rq_connection
from .web import config as service_config
from rq.serializers import JSONSerializer


def add_basic_auth(blueprint, username, password, realm="RQ Dashboard"):
Expand Down Expand Up @@ -154,6 +156,9 @@ def make_flask_app(config, username, password, url_prefix, compatibility_mode=Tr
@click.option(
"-v", "--verbose", is_flag=True, default=False, help="Enable verbose logging"
)
@click.option(
"-j", "--json", is_flag=True, default=False, help="Enable JSONSerializer"
)
def run(
bind,
port,
Expand All @@ -174,6 +179,7 @@ def run(
debug,
delete_jobs,
verbose,
json,
):
"""Run the RQ Dashboard Flask server.
Expand Down Expand Up @@ -246,6 +252,9 @@ def run(
url,
)
app.config["RQ_DASHBOARD_REDIS_URL"] = url

if json:
service_config.serializer = JSONSerializer

setup_rq_connection(app)
app.run(host=bind, port=port, debug=debug)
Expand Down
22 changes: 14 additions & 8 deletions rq_dashboard/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,17 @@
from .legacy_config import upgrade_config
from .version import VERSION as rq_dashboard_version

from rq.serializers import DefaultSerializer


blueprint = Blueprint(
"rq_dashboard", __name__, template_folder="templates", static_folder="static",
)

class Config:
serializer = DefaultSerializer

config: Config = Config()

# @blueprint.before_app_first_request
def setup_rq_connection(current_app):
Expand Down Expand Up @@ -213,7 +219,7 @@ def favicon():


def get_queue_registry_jobs_count(queue_name, registry_name, offset, per_page):
queue = Queue(queue_name)
queue = Queue(queue_name, serializer=config.serializer)
if registry_name != "queued":
if per_page >= 0:
per_page = offset + (per_page - 1)
Expand All @@ -232,7 +238,7 @@ def get_queue_registry_jobs_count(queue_name, registry_name, offset, per_page):

job_ids = current_queue.get_job_ids(offset, per_page)
current_queue_jobs = [queue.fetch_job(job_id) for job_id in job_ids]
jobs = [serialize_job(job) for job in current_queue_jobs]
jobs = [serialize_job(job) for job in current_queue_jobs if job]

return (total_items, jobs)

Expand Down Expand Up @@ -309,9 +315,9 @@ def workers_overview(instance_number):
)
def jobs_overview(instance_number, queue_name, registry_name, per_page, page):
if queue_name is None:
queue = Queue()
queue = Queue(serializer=config.serializer)
else:
queue = Queue(queue_name)
queue = Queue(queue_name, serializer=config.serializer)
r = make_response(
render_template(
"rq_dashboard/jobs.html",
Expand Down Expand Up @@ -374,7 +380,7 @@ def requeue_job_view(job_id):
@blueprint.route("/requeue/<queue_name>", methods=["GET", "POST"])
@jsonify
def requeue_all(queue_name):
fq = Queue(queue_name).failed_job_registry
fq = Queue(queue_name, serializer=config.serializer).failed_job_registry
job_ids = fq.get_job_ids()
count = len(job_ids)
for job_id in job_ids:
Expand All @@ -386,7 +392,7 @@ def requeue_all(queue_name):
@jsonify
def empty_queue(queue_name, registry_name):
if registry_name == "queued":
q = Queue(queue_name)
q = Queue(queue_name, serializer=config.serializer)
q.empty()
elif registry_name == "failed":
ids = FailedJobRegistry(queue_name).get_job_ids()
Expand All @@ -410,7 +416,7 @@ def empty_queue(queue_name, registry_name):
@blueprint.route("/queue/<queue_name>/compact", methods=["POST"])
@jsonify
def compact_queue(queue_name):
q = Queue(queue_name)
q = Queue(queue_name, serializer=config.serializer)
q.compact()
return dict(status="OK")

Expand Down Expand Up @@ -518,7 +524,7 @@ def list_jobs(instance_number, queue_name, registry_name, per_page, page):
@blueprint.route("/<int:instance_number>/data/job/<job_id>.json")
@jsonify
def job_info(instance_number, job_id):
job = Job.fetch(job_id)
job = Job.fetch(job_id, serializer=config.serializer)
return dict(
id=job.id,
created_at=serialize_date(job.created_at),
Expand Down

0 comments on commit ab62651

Please sign in to comment.