Skip to content

Commit

Permalink
Add another retry for _process_record_with_retry
Browse files Browse the repository at this point in the history
Set the delay to 0.5 for that retry
  • Loading branch information
gherceg committed Nov 29, 2023
1 parent 8fb0ab8 commit 41e8dfe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion corehq/apps/users/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def _process_reporting_metadata_staging():
record.delete()


@retry_on(ResourceConflict, delays=[0])
@retry_on(ResourceConflict, delays=[0, 0.5])
def _process_record_with_retry(record):
"""
It is possible that an unrelated user update is saved to the db while we are processing the record
Expand Down
13 changes: 12 additions & 1 deletion corehq/apps/users/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def test_record_is_not_deleted_if_not_processed_successfully(self, mock_process_
self.assertEqual(mock_process_record.call_count, 1)
self.assertTrue(UserReportingMetadataStaging.objects.get(id=record.id))

def test_process_record_is_retried_if_resource_conflict_raised(self, mock_process_record):
def test_process_record_is_retried_successfully_after_resource_conflict_raised(self, mock_process_record):
# Simulate the scenario where the first attempt to process a record raises ResourceConflict
# but the next attempt succeeds
mock_process_record.side_effect = [ResourceConflict, None]
Expand All @@ -220,6 +220,17 @@ def test_process_record_is_retried_if_resource_conflict_raised(self, mock_proces
self.assertEqual(mock_process_record.call_count, 2)
self.assertEqual(UserReportingMetadataStaging.objects.all().count(), 0)

def test_process_record_raises_resource_conflict_after_three_tries(self, mock_process_record):
# ResourceConflict will always be raised when calling mock_process_record
mock_process_record.side_effect = ResourceConflict
UserReportingMetadataStaging.objects.create(user_id=self.user._id, domain='test-domain')

with self.assertRaises(ResourceConflict):
_process_reporting_metadata_staging()

self.assertEqual(mock_process_record.call_count, 3)
self.assertEqual(UserReportingMetadataStaging.objects.all().count(), 1)

def test_subsequent_records_are_not_processed_if_exception_raised(self, mock_process_record):
mock_process_record.side_effect = [Exception, None]
UserReportingMetadataStaging.objects.create(user_id=self.user._id, domain='test-domain')
Expand Down

0 comments on commit 41e8dfe

Please sign in to comment.