Skip to content

Commit

Permalink
Recreate queue action 🙅🏽‍♀️
Browse files Browse the repository at this point in the history
  • Loading branch information
Sibyx committed Mar 4, 2024
1 parent fa151f1 commit c131348
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Changelog

## 1.11.2 : 2023-03-04
## 1.12.0 : 2023-03-04

- **Added**: `Recreate queue` in the administration
- **Changed**: Use `enqueue_many` in `Evaluation`
- **Fixed**: Evaluation reports compatible with new

## 1.11.1 : 2024-02-25
Expand Down
21 changes: 20 additions & 1 deletion apps/core/admin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import django_rq
from admin_extra_buttons.decorators import button
from admin_extra_buttons.mixins import ExtraButtonsMixin
from admin_extra_buttons.utils import HttpResponseRedirectToReferrer
from django.contrib import admin
from django.urls import reverse
from django.utils.html import format_html
from django.utils.translation import gettext as _
from rq import Queue

from apps.core.jobs import basic_job
from apps.core.models import Assignment, Scenario, Task, TaskRecord, Evaluation


Expand Down Expand Up @@ -49,7 +55,7 @@ class TaskRecordAdmin(RelatedFieldAdmin):
show_facets = admin.ShowFacets.ALWAYS


class EvaluationAdmin(RelatedFieldAdmin):
class EvaluationAdmin(ExtraButtonsMixin, RelatedFieldAdmin):
list_display = ("id", "created_at", "assignment", "status", "protocol")
readonly_fields = ("created_at", "tasks", "id")
exclude = ("creator",)
Expand Down Expand Up @@ -86,6 +92,19 @@ def protocol(self, obj):
else:
return ""

@button(html_attrs={'style': 'background-color:#88FF88;color:black'})
def recreate_queue(self, request):
django_rq.get_queue("default").empty()
jobs = []

for task in Task.objects.filter(status=Task.Status.PENDING):
jobs.append(Queue.prepare_data(basic_job, (task.pk, False, )))

django_rq.get_queue("default").enqueue_many(jobs)

self.message_user(request, 'Pending tasks were added back to queue')
return HttpResponseRedirectToReferrer(request)


class AssignmentAdmin(admin.ModelAdmin):
show_facets = admin.ShowFacets.ALWAYS
Expand Down
7 changes: 6 additions & 1 deletion apps/core/models/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from rq import Queue

from apps.core.jobs import basic_job
from apps.core.models.assignment import Assignment
Expand Down Expand Up @@ -37,6 +38,7 @@ def __str__(self) -> str:
@receiver(post_save, sender=Evaluation)
def execute_tasks(sender, instance: Evaluation, created: bool, **kwargs):
if created:
jobs = []
with open(instance.links.path, "r", newline="") as csvfile:
reader = csv.DictReader(csvfile, delimiter=",")
for row in reader:
Expand All @@ -48,7 +50,10 @@ def execute_tasks(sender, instance: Evaluation, created: bool, **kwargs):
additional_information=row,
)
instance.tasks.add(task)
django_rq.enqueue(basic_job, task_id=task.pk, public_only=False)
jobs.append(
Queue.prepare_data(basic_job, (task.pk, False, ))
)
django_rq.get_queue("default").enqueue_many(jobs)


__all__ = ["Evaluation"]
1 change: 1 addition & 0 deletions dbs_tester/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"django.forms",
"django_rq",
"django_bootstrap5",
"admin_extra_buttons",
"apps.core",
"apps.web",
"apps.api",
Expand Down
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dbs_tester"
version = "1.11.2"
version = "1.12.0"
description = "DBS tester application"
authors = [
"Jakub Dubec <[email protected]>"
Expand All @@ -25,6 +25,7 @@ docker = "^7.0"
python-crontab = "^3.0"
porcupine-python = "^0.5.0"
django-api-forms = "1.0.0rc9"
django-admin-extra-buttons = "^1.5.7"

[tool.poetry.dev-dependencies]
black = "^24.0"
Expand Down

0 comments on commit c131348

Please sign in to comment.