Skip to content

Commit

Permalink
Merge pull request #447 from KerkhoffTechnologies/3761-databaseerror-…
Browse files Browse the repository at this point in the history
…save-with-update_fields-did-not-affect-any-rows

handle task sync DB error and avoid too-large values
  • Loading branch information
kti-sam authored Oct 24, 2024
2 parents 546bed4 + 36098ef commit f9b23ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion djconnectwise/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
VERSION = (1, 6, 7, 'final')
VERSION = (1, 6, 8, 'final')

# pragma: no cover
if VERSION[-1] != "final":
Expand Down
24 changes: 20 additions & 4 deletions djconnectwise/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from dateutil.parser import parse
from django.core.exceptions import ObjectDoesNotExist
from django.core.files.storage import default_storage
from django.db import transaction, IntegrityError
from django.db import transaction, IntegrityError, DatabaseError
from django.db.models import Q
from django.utils import timezone
from django.utils.text import normalize_newlines
Expand All @@ -26,6 +26,9 @@
SKIPPED = 3
FILE_UMASK = 0o022

MAX_POSITIVE_SMALL_INT = 32767
# See https://docs.djangoproject.com/en/dev/ref/models/fields/#positivesmallintegerfield

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -872,10 +875,23 @@ def sync(self):

def sync_tasks(self, instance):
tasks = self.get(parent=instance.id)
instance.tasks_total = len(tasks)
instance.tasks_completed = sum(task['closed_flag'] for task in tasks)

instance.save(update_fields=["tasks_total", "tasks_completed"])
# When the PSA goes crazy, stay within the bounds of a small int.
instance.tasks_total = min(
MAX_POSITIVE_SMALL_INT, len(tasks)
)
instance.tasks_completed = min(
MAX_POSITIVE_SMALL_INT, sum(task['closed_flag'] for task in tasks)
)

try:
instance.save(update_fields=["tasks_total", "tasks_completed"])
except DatabaseError as e:
# This can happen if the ticket was deleted in the background.
logger.warning(
'DatabaseError while processing tasks on ticket {}: '
'{}'.format(instance, e)
)


class ServiceTicketTaskSynchronizer(TicketTaskSynchronizer, DummySynchronizer):
Expand Down

0 comments on commit f9b23ba

Please sign in to comment.