Skip to content

Commit 2af6899

Browse files
committed
[fix] Fixed the deativation logic
1 parent 16eb6fb commit 2af6899

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

openwisp_controller/config/base/config.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ def update_status_if_checksum_changed(
639639
bool: True if the checksum changed and an update was applied,
640640
False otherwise.
641641
"""
642-
checksum_changed = self._should_update_status_based_on_checksum()
642+
checksum_changed = self._has_configuration_checksum_changed()
643643
if checksum_changed:
644644
self.checksum_db = self.checksum
645645
if self.status != "modified":
@@ -649,18 +649,12 @@ def update_status_if_checksum_changed(
649649
extra_update_fields=["checksum_db"],
650650
)
651651
else:
652-
# Instead of calling the "save()" method, which would
653-
# trigger various signals and checks, we directly update
654-
# the "checksum_db" field in the database.
655-
self._meta.model.objects.filter(pk=self.pk).update(
656-
checksum_db=self.checksum_db
657-
)
652+
self._update_checksum_db(new_checksum=self.checksum_db)
658653
return checksum_changed
659654

660-
def _should_update_status_based_on_checksum(self):
655+
def _has_configuration_checksum_changed(self):
661656
"""
662-
Determines whether the config status should be updated based on
663-
checksum comparison.
657+
Determines whether the config checksum has changed
664658
665659
Returns True if:
666660
- No checksum_db exists (first time)
@@ -675,6 +669,16 @@ def _should_update_status_based_on_checksum(self):
675669
self._invalidate_backend_instance_cache()
676670
return self.checksum_db != self.checksum
677671

672+
def _update_checksum_db(self, new_checksum=None):
673+
"""
674+
Updates checksum_db field in the database
675+
676+
It does not call save() to avoid sending signals
677+
and updating other fields.
678+
"""
679+
new_checksum = new_checksum or self.checksum
680+
self._meta.model.objects.filter(pk=self.pk).update(checksum_db=new_checksum)
681+
678682
def _send_config_modified_signal(self, action):
679683
"""
680684
Emits ``config_modified`` signal.
@@ -780,12 +784,20 @@ def deactivate(self):
780784
"""
781785
# Invalidate cached property before checking checksum.
782786
self._invalidate_backend_instance_cache()
783-
old_checksum = self.checksum
787+
old_checksum = self.checksum_db
788+
# Don't alter the order of the following steps.
789+
# We need to set the status to deactivating before clearing the templates
790+
# otherwise, the "enforce_required_templates" and "add_default_templates"
791+
# methods would re-add required/default templates.
792+
# The "templates_changed" receiver skips post_clear action. Thus,
793+
# we need to update the checksum_db field manually.
784794
self.config = {}
785-
self.set_status_deactivating()
795+
self.set_status_deactivating(save=False)
786796
self.templates.clear()
787-
del self.backend_instance
788-
if old_checksum == self.checksum:
797+
self._invalidate_backend_instance_cache()
798+
self.checksum_db = self.checksum
799+
self.save()
800+
if old_checksum == self.checksum_db:
789801
# Accelerate deactivation if the configuration remains
790802
# unchanged (i.e. empty configuration)
791803
self.set_status_deactivated()

openwisp_controller/config/migrations/0061_config_checksum_db.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@ def populate_checksum_db(apps, schema_editor):
1010
for existing Config objects.
1111
"""
1212
Config = load_model("config", "Config")
13-
batch = []
14-
batch_size = 500
15-
qs = Config.objects.filter(checksum_db__isnull=True).iterator()
13+
qs = (
14+
Config.objects.prefetch_related("vpnclient_set", "templates")
15+
.select_related("device", "device__organization__config_settings")
16+
.filter(checksum_db__isnull=True)
17+
.iterator(chunk_size=100)
18+
)
1619
for config in qs:
17-
config.checksum_db = config.checksum
18-
batch.append(config)
19-
if len(batch) >= batch_size:
20-
Config.objects.bulk_update(batch, ["checksum_db"])
21-
batch = []
22-
if batch:
23-
Config.objects.bulk_update(batch, ["checksum_db"])
20+
config.update_status_if_checksum_changed()
2421

2522

2623
class Migration(migrations.Migration):

openwisp_controller/config/tests/test_controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ def test_ip_fields_not_duplicated(self):
14121412
)
14131413
# triggers more queries because devices with conflicting addresses
14141414
# need to be updated, luckily it does not happen often
1415-
with self.assertNumQueries(9):
1415+
with self.assertNumQueries(8):
14161416
self.client.get(
14171417
reverse("controller:device_checksum", args=[c2.device.pk]),
14181418
{"key": c2.device.key, "management_ip": "192.168.1.99"},

0 commit comments

Comments
 (0)