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

Fix Empty Queue operation for Registries that has no exists jobs #481

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion rq_dashboard/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ def make_flask_app(config, username, password, url_prefix, compatibility_mode=Tr
hidden=True,
help="[DEPRECATED] Delete jobs instead of cancel",
)
@click.option(
"--disable-delete", is_flag=True, default=False, help="Disable delete jobs, clean up registries"
)
@click.option("--debug/--normal", default=False, help="Enter DEBUG mode")
@click.option(
"-v", "--verbose", is_flag=True, default=False, help="Enable verbose logging"
Expand All @@ -178,6 +181,7 @@ def run(
web_background,
debug,
delete_jobs,
disable_delete,
verbose,
json,
):
Expand Down Expand Up @@ -252,7 +256,9 @@ def run(
url,
)
app.config["RQ_DASHBOARD_REDIS_URL"] = url


app.config["RQ_DASHBOARD_DISABLE_DELETE"] = disable_delete

if json:
service_config.serializer = JSONSerializer

Expand Down
6 changes: 4 additions & 2 deletions rq_dashboard/templates/rq_dashboard/job.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
<span class="col-10">
<h2><strong>Job ID</strong>: {{ id }}</h2>
</span>

<span class="col-2">
<button id="requeue-job-btn" class="btn btn-outline-warning btn-sm" style="display: none">Requeue</button>
{% if enable_delete %}
<button id="delete-job-btn" class="btn btn-outline-danger btn-sm">Delete</button>
{% endif %}
</span>
</div>
<div id="job-data" class="row"></div>


<script name="job-info" type="text/template">

Expand Down
4 changes: 4 additions & 0 deletions rq_dashboard/templates/rq_dashboard/jobs.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
</div>

<p class="intro">
{% if enable_delete %}
<a href="{{ url_for('rq_dashboard.empty_queue', queue_name=queue.name, registry_name=registry_name) }}" id="empty-btn"
class="btn btn-outline-danger btn-sm" style="float: right" data-toggle="tooltip"
title="Remove all jobs from this queue (<b>destructive</b>)" data-html=true>Empty queue</a>
{% endif %}
{% if registry_name == 'queued' %}
<a href="{{ url_for('rq_dashboard.compact_queue', queue_name=queue.name) }}" id="compact-btn"
class="btn btn-outline-success btn-sm" style="float: right; margin-right: 8px;" data-toggle="tooltip"
Expand Down Expand Up @@ -76,7 +78,9 @@
<% if (d.exc_info) { %>
<a href="#" data-role="requeue-job-btn" class="btn btn-outline-warning btn-sm btn-block">Requeue</a>
<% } %>
{% if enable_delete %}
<a href="#" data-role="delete-job-btn" class="btn btn-outline-danger btn-sm">Delete</a>
{% endif %}
</td>
</tr>
</script>
Expand Down
61 changes: 40 additions & 21 deletions rq_dashboard/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
push_connection,
requeue_job,
)
from rq.exceptions import NoSuchJobError
from rq.job import Job
from rq.registry import (
DeferredJobRegistry,
Expand Down Expand Up @@ -113,6 +114,13 @@ def _wrapped(*args, **kwargs):

return _wrapped

def check_delete_enable(f):
@wraps(f)
def wrapper(*args, **kwargs):
if current_app.config.get("RQ_DASHBOARD_DISABLE_DELETE"):
return dict(status="DISABLED")
return f(*args, **kwargs)
return wrapper

def serialize_queues(instance_number, queues):
return [
Expand Down Expand Up @@ -360,6 +368,7 @@ def jobs_overview(instance_number, queue_name, registry_name, per_page, page):
deprecation_options_usage=current_app.config.get(
"DEPRECATED_OPTIONS", False
),
enable_delete=not current_app.config.get("RQ_DASHBOARD_DISABLE_DELETE"),
)
)
r.headers.set("Cache-Control", "no-store")
Expand All @@ -381,17 +390,25 @@ def job_view(instance_number, job_id):
deprecation_options_usage=current_app.config.get(
"DEPRECATED_OPTIONS", False
),
enable_delete=not current_app.config.get("RQ_DASHBOARD_DISABLE_DELETE"),
)
)
r.headers.set("Cache-Control", "no-store")
return r


@blueprint.route("/job/<job_id>/delete", methods=["POST"])
@check_delete_enable
@jsonify
def delete_job_view(job_id):
job = Job.fetch(job_id)
job.delete()
def delete_job_view(job_id, registry=None):
try:
job = Job.fetch(job_id)
job.delete()
except NoSuchJobError:
if registry:
registry.remove(job_id)
return dict(status="ERROR")

return dict(status="OK")


Expand All @@ -414,35 +431,37 @@ def requeue_all(queue_name):


@blueprint.route("/queue/<queue_name>/<registry_name>/empty", methods=["POST"])
@check_delete_enable
@jsonify
def empty_queue(queue_name, registry_name):
if registry_name == "queued":
q = Queue(queue_name, serializer=config.serializer)
q.empty()
elif registry_name == "failed":
ids = FailedJobRegistry(queue_name).get_job_ids()
for id in ids:
delete_job_view(id)
registry = FailedJobRegistry(queue_name)
for id in registry.get_job_ids():
delete_job_view(id, registry)
elif registry_name == "deferred":
ids = DeferredJobRegistry(queue_name).get_job_ids()
for id in ids:
delete_job_view(id)
registry = DeferredJobRegistry(queue_name)
for id in registry.get_job_ids():
delete_job_view(id, registry)
elif registry_name == "started":
ids = StartedJobRegistry(queue_name).get_job_ids()
for id in ids:
delete_job_view(id)
registry = StartedJobRegistry(queue_name)
for id in registry.get_job_ids():
delete_job_view(id, registry)
elif registry_name == "finished":
ids = FinishedJobRegistry(queue_name).get_job_ids()
for id in ids:
delete_job_view(id)
registry = FinishedJobRegistry(queue_name)
for id in registry.get_job_ids():
delete_job_view(id, registry)
elif registry_name == "canceled":
ids = CanceledJobRegistry(queue_name).get_job_ids()
for id in ids:
delete_job_view(id)
registry = CanceledJobRegistry(queue_name)
for id in registry.get_job_ids():
delete_job_view(id, registry)
elif registry_name == "scheduled":
ids = ScheduledJobRegistry(queue_name).get_job_ids()
for id in ids:
delete_job_view(id)
registry = ScheduledJobRegistry(queue_name)
for id in registry.get_job_ids():
delete_job_view(id, registry)

return dict(status="OK")


Expand Down
Loading