Skip to content

Commit

Permalink
Update database router to deal with failed_identities
Browse files Browse the repository at this point in the history
  • Loading branch information
vkurup committed Jul 24, 2015
1 parent 44f951d commit f3f9603
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions rapidsms/router/db/tasks.py
Expand Up @@ -40,7 +40,8 @@ def send_transmissions(backend_id, message_id, transmission_ids):
from rapidsms.router import get_router
backend = Backend.objects.get(pk=backend_id)
dbm = Message.objects.select_related('in_response_to').get(pk=message_id)
transmissions = Transmission.objects.filter(id__in=transmission_ids)
# this might be a retry, so exclude transmissions which were successful
transmissions = Transmission.objects.filter(id__in=transmission_ids).exclude(status='S')
# set (possibly reset) status to processing
transmissions.update(status='P')
identities = transmissions.values_list('connection__identity', flat=True)
Expand All @@ -53,10 +54,19 @@ def send_transmissions(backend_id, message_id, transmission_ids):
text=dbm.text, identities=list(identities),
context=context)
except MessageSendingError as exc:
# update database statuses, and re-execute this task
logger.warning("Re-trying send_transmissions")
# some or all transmissions failed: mark the Message group as failed
Message.objects.filter(pk=message_id).update(status='E')
if hasattr(exc, 'failed_identities') and exc.failed_identities:
# Backend explicitly said these ids failed, so mark all others sent
transmissions.exclude(
connection__identity__in=exc.failed_identities
).update(status='S', sent=now())
# regenerate the transmissions QS, with only the failed transmissions
transmissions = transmissions.exclude(status='S')
# else:
# Backend didn't provide failed_identities, so we assume all failed
transmissions.update(status='E', updated=now())
# Retry the task
raise send_transmissions.retry(exc=exc)
# no error occured, so mark these transmissions as sent
transmissions.update(status='S', sent=now())
Expand Down

0 comments on commit f3f9603

Please sign in to comment.