Skip to content

Commit

Permalink
Fix a race condition when fetching telemetry data (fixes DEV-37)
Browse files Browse the repository at this point in the history
  • Loading branch information
aptiko committed Nov 17, 2024
1 parent dc10607 commit aecd9f6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion enhydris/telemetry/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ def fetch_all_telemetry_data():

@app.task(bind=True, soft_time_limit=FETCH_TIMEOUT, time_limit=FETCH_TIMEOUT + 10)
def fetch_telemetry_data(self, telemetry_id):
telemetry = Telemetry.objects.get(id=telemetry_id)
try:
telemetry = Telemetry.objects.get(id=telemetry_id)
except Telemetry.DoesNotExist:
return
lock_id = f"telemetry-{telemetry_id}"
acquired_lock = cache.add(lock_id, self.app.oid, LOCK_TIMEOUT)
if acquired_lock:
Expand Down
8 changes: 8 additions & 0 deletions enhydris/telemetry/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def test_logs_error_if_locked(self, mock_logger, mock_fetch, mock_cache):
tasks.fetch_telemetry_data(self.telemetry.id)
mock_logger.error.assert_called_once()

def test_deleted_telemetry(self, mock_fetch, mock_cache):
# Sometimes a telemetry may get deleted between queuing the fetching of
# data and actually starting executing it. We test that in that case there's
# no exception and the fetching is merely ignored.
telemetry_id = self.telemetry.id
self.telemetry.delete()
tasks.fetch_telemetry_data(telemetry_id) # Should not raise exception


@patch("enhydris.telemetry.tasks.fetch_telemetry_data.delay")
class FetchAllTelemetryDataTestCase(TestCase):
Expand Down

0 comments on commit aecd9f6

Please sign in to comment.