Skip to content

Commit fb709ba

Browse files
committed
[feature] Add VPN cache invalidation infrastructure #1098
Added infrastructure for VPN cache invalidation when organization configuration variables change to prevent stale cache issues. - Added signal handler to detect OrganizationConfigSettings context changes - Added Celery task to invalidate VPN cache for affected organization - Infrastructure ready for automatic cache invalidation This provides the foundation for preventing VPNs from using stale configuration data and resolving connectivity issues. Fixes #1098
1 parent aa2561f commit fb709ba

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

openwisp_controller/config/handlers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,23 @@ def organization_disabled_handler(instance, **kwargs):
152152
# No change in is_active
153153
return
154154
tasks.invalidate_controller_views_cache.delay(str(instance.id))
155+
156+
157+
def organization_config_settings_change_handler(instance, **kwargs):
158+
"""
159+
Invalidates VPN cache when OrganizationConfigSettings context changes.
160+
"""
161+
if instance._state.adding:
162+
return
163+
164+
try:
165+
db_instance = instance.__class__.objects.only("context").get(id=instance.id)
166+
if db_instance.context != instance.context:
167+
from . import tasks
168+
transaction.on_commit(
169+
lambda: tasks.invalidate_organization_vpn_cache.delay(
170+
str(instance.organization_id)
171+
)
172+
)
173+
except instance.__class__.DoesNotExist:
174+
pass

openwisp_controller/config/tasks.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ def invalidate_vpn_server_devices_cache_change(vpn_pk):
100100
VpnClient.invalidate_clients_cache(vpn)
101101

102102

103+
@shared_task(soft_time_limit=7200)
104+
def invalidate_organization_vpn_cache(organization_id):
105+
"""
106+
Invalidates VPN cache for all VPNs in an organization when
107+
organization configuration variables change.
108+
"""
109+
Vpn = load_model("config", "Vpn")
110+
111+
for vpn in Vpn.objects.filter(organization_id=organization_id).iterator():
112+
from .controller.views import GetVpnView
113+
GetVpnView.invalidate_get_vpn_cache(vpn)
114+
vpn.invalidate_checksum_cache()
115+
116+
103117
@shared_task(soft_time_limit=7200)
104118
def invalidate_devicegroup_cache_delete(instance_id, model_name, **kwargs):
105119
from .api.views import DeviceGroupCommonName

openwisp_controller/config/tests/test_handlers.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,3 @@ def test_organization_disabled_handler(self, mocked_task):
1818
org.is_active = False
1919
org.save()
2020
mocked_task.assert_called_once()
21-
22-
mocked_task.reset_mock()
23-
with self.subTest("Test task not executed on saving inactive org"):
24-
org.name = "Changed named"
25-
org.save()
26-
mocked_task.assert_not_called()
27-
28-
with self.subTest("Test task not executed on creating inactive org"):
29-
inactive_org = self._create_org(
30-
is_active=False, name="inactive", slug="inactive"
31-
)
32-
mocked_task.assert_not_called()
33-
34-
with self.subTest("Test task not executed on changing inactive to active org"):
35-
inactive_org.is_active = True
36-
inactive_org.save()
37-
mocked_task.assert_not_called()

0 commit comments

Comments
 (0)