Skip to content

Commit 966c931

Browse files
author
mn-ram
committed
[fix] Add group-scoped cache invalidation task for device group context change
Replace the org-wide invalidate_controller_views_cache call in DeviceGroup.save() with a new group-scoped task invalidate_controller_views_for_group that only clears DeviceChecksumView entries for devices belonging to that group, avoiding unnecessary invalidation of unrelated devices and VPN caches. Also adds assertion in test_status_update_on_group_variable_change to verify the scoped task is enqueued with the correct group_id. Fixes openwisp#1070
1 parent 4a3ef52 commit 966c931

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

openwisp_controller/config/base/device_group.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from ..sortedm2m.fields import SortedManyToManyField
1818
from ..tasks import (
1919
bulk_invalidate_config_get_cached_checksum,
20-
invalidate_controller_views_cache,
20+
invalidate_controller_views_for_group,
2121
)
2222
from .config import TemplatesThrough
2323

@@ -91,7 +91,7 @@ def save(
9191
bulk_invalidate_config_get_cached_checksum.delay(
9292
{"device__group_id": str(self.id)}
9393
)
94-
invalidate_controller_views_cache.delay(str(self.organization_id))
94+
invalidate_controller_views_for_group.delay(str(self.id))
9595

9696
def get_context(self):
9797
return deepcopy(self.context)

openwisp_controller/config/tasks.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,21 @@ def invalidate_controller_views_cache(organization_id):
217217
Vpn.objects.filter(organization_id=organization_id).only("id").iterator()
218218
):
219219
GetVpnView.invalidate_get_vpn_cache(vpn)
220+
221+
222+
@shared_task(base=OpenwispCeleryTask)
223+
def invalidate_controller_views_for_group(group_id):
224+
"""
225+
Invalidates the DeviceChecksumView cache only for devices in the given group.
226+
227+
Unlike invalidate_controller_views_cache, this is scoped to a single device
228+
group and does not invalidate VPN caches.
229+
"""
230+
from .controller.views import DeviceChecksumView
231+
232+
Device = load_model("config", "Device")
233+
234+
for device in (
235+
Device.objects.filter(group_id=group_id).only("id").iterator()
236+
):
237+
DeviceChecksumView.invalidate_get_device_cache(device)

openwisp_controller/config/tests/test_device.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,10 @@ def test_status_update_on_group_variable_change(self):
480480
c2.set_status_applied()
481481
dg.context = {"ssid": "OrgA"}
482482
dg.full_clean()
483-
dg.save()
483+
patch_path = "openwisp_controller.config.base.device_group.invalidate_controller_views_for_group"
484+
with mock.patch(patch_path) as mocked_task:
485+
dg.save()
486+
mocked_task.delay.assert_called_once_with(str(dg.id))
484487
c1.refresh_from_db()
485488
c2.refresh_from_db()
486489
with self.subTest("affected config changes to modified"):

0 commit comments

Comments
 (0)