Skip to content

Error about shift watch in the generate_shifts task #756

@Theophile-Madet

Description

@Theophile-Madet

Every night when the celery task generate_shifts runs, the following error happens.

@crosspolar let me know if you want to investigate it yourself, I can also have a look.

Task tapir.shifts.tasks.generate_shifts with id 2c63129a-248c-4376-9d1e-1034c39d0e98 raised exception: IntegrityError('duplicate key value violates unique constraint "shift_watch_constraint"\nDETAIL:  Key (user_id, shift_id)=(538, 13366) already exists.\n')
                 Task was called with args: [] kwargs: {}.
                 The contents of the full traceback was:Traceback (most recent call last):
  File "/root/.cache/pypoetry/virtualenvs/tapir-9TtSrW0h-py3.13/lib/python3.13/site-packages/django/db/backends/utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "shift_watch_constraint"
DETAIL:  Key (user_id, shift_id)=(538, 13366) already exists.


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/root/.cache/pypoetry/virtualenvs/tapir-9TtSrW0h-py3.13/lib/python3.13/site-packages/celery/app/trace.py", line 479, in trace_task
    R = retval = fun(*args, **kwargs)
                 ~~~^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/tapir-9TtSrW0h-py3.13/lib/python3.13/site-packages/celery/app/trace.py", line 779, in __protected_call__
    return self.run(*args, **kwargs)
           ~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/app/tapir/shifts/tasks.py", line 17, in generate_shifts
    call_command("generate_shifts")
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/tapir-9TtSrW0h-py3.13/lib/python3.13/site-packages/django/core/management/__init__.py", line 194, in call_command
    return command.execute(*args, **defaults)
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/tapir-9TtSrW0h-py3.13/lib/python3.13/site-packages/django/core/management/base.py", line 464, in execute
    output = self.handle(*args, **options)
  File "/app/tapir/shifts/management/commands/generate_shifts.py", line 12, in handle
    ShiftGenerator.generate_shifts_up_to(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        end_date=timezone.now().date() + GENERATE_UP_TO
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/app/tapir/shifts/services/shift_generator.py", line 38, in generate_shifts_up_to
    cls.create_shifts_for_group(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        at_date=current_monday,
        ^^^^^^^^^^^^^^^^^^^^^^^
        group=current_group,
        ^^^^^^^^^^^^^^^^^^^^
        filter_shift_template_ids=filter_shift_template_ids,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/app/tapir/shifts/services/shift_generator.py", line 75, in create_shifts_for_group
    ShiftWatchCreator.create_shift_watches_for_shift_based_on_recurring(shift)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
  File "/app/tapir/shifts/services/shift_watch_creation_service.py", line 202, in create_shift_watches_for_shift_based_on_recurring
    ShiftWatch.objects.bulk_create(new_watches)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/tapir-9TtSrW0h-py3.13/lib/python3.13/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/tapir-9TtSrW0h-py3.13/lib/python3.13/site-packages/django/db/models/query.py", line 825, in bulk_create
    returned_columns = self._batched_insert(
        objs_without_pk,
    ...<4 lines>...
        unique_fields=unique_fields,
    )
  File "/root/.cache/pypoetry/virtualenvs/tapir-9TtSrW0h-py3.13/lib/python3.13/site-packages/django/db/models/query.py", line 1901, in _batched_insert
    self._insert(
    ~~~~~~~~~~~~^
        item,
        ^^^^^
    ...<5 lines>...
        returning_fields=self.model._meta.db_returning_fields,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/root/.cache/pypoetry/virtualenvs/tapir-9TtSrW0h-py3.13/lib/python3.13/site-packages/django/db/models/query.py", line 1873, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/tapir-9TtSrW0h-py3.13/lib/python3.13/site-packages/django/db/models/sql/compiler.py", line 1882, in execute_sql
    cursor.execute(sql, params)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/tapir-9TtSrW0h-py3.13/lib/python3.13/site-packages/django/db/backends/utils.py", line 79, in execute
    return self._execute_with_wrappers(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        sql, params, many=False, executor=self._execute
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/root/.cache/pypoetry/virtualenvs/tapir-9TtSrW0h-py3.13/lib/python3.13/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/root/.cache/pypoetry/virtualenvs/tapir-9TtSrW0h-py3.13/lib/python3.13/site-packages/django/db/backends/utils.py", line 100, in _execute
    with self.db.wrap_database_errors:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/tapir-9TtSrW0h-py3.13/lib/python3.13/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/root/.cache/pypoetry/virtualenvs/tapir-9TtSrW0h-py3.13/lib/python3.13/site-packages/django/db/backends/utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
django.db.utils.IntegrityError: duplicate key value violates unique constraint "shift_watch_constraint"
DETAIL:  Key (user_id, shift_id)=(538, 13366) already exists.

.
              

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions