update post_save signal receiver

This commit is contained in:
Michael Shamoon 2022-05-23 01:52:46 -07:00
parent f88e070455
commit 4bbaf5f89c
6 changed files with 39 additions and 39 deletions

View File

@ -0,0 +1,11 @@
import { ObjectWithId } from './object-with-id'
export interface PaperlessTask extends ObjectWithId {
acknowledged: boolean
task_id: string
name: string
created: Date
}

View File

@ -11,8 +11,8 @@ def init_paperless_tasks(apps, schema_editor):
for task in Task.objects.all():
if not hasattr(task, "paperlesstask"):
paperlesstask = PaperlessTask.objects.create(
task=task,
q_task_id=task.id,
attempted_task=task,
task_id=task.id,
name=task.name,
created=task.started,
acknowledged=False,
@ -41,7 +41,7 @@ class Migration(migrations.Migration):
verbose_name="ID",
),
),
("q_task_id", models.CharField(max_length=128)),
("task_id", models.CharField(max_length=128)),
("name", models.CharField(max_length=256)),
(
"created",
@ -51,12 +51,12 @@ class Migration(migrations.Migration):
),
("acknowledged", models.BooleanField(default=False)),
(
"task",
"attempted_task",
models.OneToOneField(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="task",
related_name="attempted_task",
to="django_q.task",
),
),

View File

@ -505,13 +505,13 @@ class UiSettings(models.Model):
class PaperlessTask(models.Model):
q_task_id = models.CharField(max_length=128)
task_id = models.CharField(max_length=128)
name = models.CharField(max_length=256)
created = models.DateTimeField(_("created"), auto_now=True, db_index=True)
task = models.OneToOneField(
attempted_task = models.OneToOneField(
Task,
on_delete=models.CASCADE,
related_name="task",
related_name="attempted_task",
null=True,
blank=True,
)

View File

@ -2,6 +2,7 @@ import logging
import os
import shutil
import django_q
from django.conf import settings
from django.contrib.admin.models import ADDITION
from django.contrib.admin.models import LogEntry
@ -13,9 +14,6 @@ from django.db.models import Q
from django.dispatch import receiver
from django.utils import termcolors
from django.utils import timezone
from django_q.signals import post_save
from django_q.signals import pre_enqueue
from django_q.tasks import Task
from filelock import FileLock
from .. import matching
@ -505,18 +503,23 @@ def add_to_index(sender, document, **kwargs):
index.add_or_update_document(document)
@receiver(pre_enqueue)
@receiver(django_q.signals.pre_enqueue)
def init_paperless_task(sender, task, **kwargs):
if task["func"] == "documents.tasks.consume_file":
paperless_task = PaperlessTask.objects.get_or_create(q_task_id=task["id"])
paperless_task, created = PaperlessTask.objects.get_or_create(
task_id=task["id"],
)
paperless_task.name = task["name"]
paperless_task.created = task["started"]
paperless_task.save()
@receiver(post_save, sender=Task)
@receiver(models.signals.post_save, sender=django_q.tasks.Task)
def update_paperless_task(sender, instance, **kwargs):
logger.debug(sender, instance)
papeless_task = PaperlessTask.objects.find(q_task_id=instance.id)
if papeless_task:
papeless_task.task = instance
papeless_task.save()
try:
if instance.func == "documents.tasks.consume_file":
paperless_task = PaperlessTask.objects.get(task_id=instance.id)
paperless_task.attempted_task = instance
paperless_task.save()
except PaperlessTask.DoesNotExist:
pass

View File

@ -10,7 +10,6 @@ from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer
from django.conf import settings
from django.db.models.signals import post_save
from django_q.tasks import Task
from documents import index
from documents import sanity_checker
from documents.classifier import DocumentClassifier
@ -360,16 +359,3 @@ def bulk_update_documents(document_ids):
with AsyncWriter(ix) as writer:
for doc in documents:
index.update_document(writer, doc)
def create_paperless_task(sender, instance, created, **kwargs):
if created:
Task.objects.create(thing=instance)
post_save.connect(
create_paperless_task,
sender=Task,
weak=False,
dispatch_uid="models.create_paperless_task",
)

View File

@ -812,26 +812,26 @@ class ConsupmtionTasksView(GenericAPIView):
PaperlessTask.objects.filter(
acknowledged=False,
)
.order_by("task__started")
.order_by("attempted_task__started")
.reverse()
)
incomplete_tasks = consumption_tasks.filter(task=None).values(
"id",
"q_task_id",
"task_id",
"name",
"created",
"acknowledged",
)
failed_tasks = consumption_tasks.filter(task__success=0).values(
failed_tasks = consumption_tasks.filter(attempted_task__success=0).values(
"id",
"q_task_id",
"task_id",
"name",
"created",
"acknowledged",
)
completed_tasks = consumption_tasks.filter(task__success=1).values(
completed_tasks = consumption_tasks.filter(attempted_task__success=1).values(
"id",
"q_task_id",
"task_id",
"name",
"created",
"acknowledged",