Skip to content

Commit

Permalink
Remove CeleryTask model and mutex (saleor#12695)
Browse files Browse the repository at this point in the history
* Remove CeleryTask model and mutex

* Clarify comment about not seperating state and db in migration
  • Loading branch information
kadewu authored Apr 28, 2023
1 parent 01b78ed commit ef6d403
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 30 deletions.
17 changes: 17 additions & 0 deletions saleor/core/migrations/0007_delete_celerytask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.18 on 2023-04-27 07:56

from django.db import migrations


# No need to seperate state and db, cause CeleryTask model
# is no longer used in code in SaleorCore.
class Migration(migrations.Migration):
dependencies = [
("core", "0006_celerytask"),
]

operations = [
migrations.DeleteModel(
name="CeleryTask",
),
]
4 changes: 0 additions & 4 deletions saleor/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,3 @@ class EventDeliveryAttempt(models.Model):

class Meta:
ordering = ("-created_at",)


class CeleryTask(Job):
name = models.CharField(max_length=255, unique=True)
27 changes: 1 addition & 26 deletions saleor/core/tasks.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,11 @@
from contextlib import contextmanager

from botocore.exceptions import ClientError
from django.conf import settings
from django.core.files.storage import default_storage
from django.db.models import Exists, OuterRef, Q
from django.utils import timezone

from ..celeryconf import app
from .models import CeleryTask, EventDelivery, EventDeliveryAttempt, EventPayload


@contextmanager
def celery_task_lock(task_name: str):
"""Celery task lock.
This contextmanager provides lock on database for executing celery task.
Parameters
----------
task_name : str
Unique name of the celery task.
"""

lock = None
created = False
try:
lock, created = CeleryTask.objects.get_or_create(name=task_name)
yield lock, created
finally:
if lock and created:
lock.delete()
from .models import EventDelivery, EventDeliveryAttempt, EventPayload


@app.task
Expand Down

0 comments on commit ef6d403

Please sign in to comment.