Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle task sync DB error and avoid too-large values #447

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading