Skip to content

Commit f164a80

Browse files
committed
[fix] Fixed create_ping and create_config migrations #205
Fixes #205
1 parent f2c4e34 commit f164a80

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

openwisp_monitoring/check/migrations/0003_create_ping.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55

66
def create_device_ping(apps, schema_editor):
77
if AUTO_PING:
8+
ContentType = apps.get_model('contenttypes', 'ContentType')
9+
Check = apps.get_model('check', 'Check')
810
Device = apps.get_model('config', 'Device')
911
for device in Device.objects.all():
1012
auto_create_ping(
1113
model=Device.__name__.lower(),
1214
app_label=Device._meta.app_label,
1315
object_id=str(device.pk),
16+
check_model=Check,
17+
content_type_model=ContentType,
1418
)
1519

1620

openwisp_monitoring/check/migrations/0005_create_config_applied.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
def add_config_applied_checks(apps, schema_editor):
77
if not AUTO_CONFIG_CHECK:
88
return
9+
ContentType = apps.get_model('contenttypes', 'ContentType')
10+
Check = apps.get_model('check', 'Check')
911
Device = apps.get_model('config', 'Device')
1012
for device in Device.objects.all():
1113
auto_create_config_check(
1214
model=Device.__name__.lower(),
1315
app_label=Device._meta.app_label,
1416
object_id=str(device.pk),
17+
check_model=Check,
18+
content_type_model=ContentType,
1519
)
1620

1721

openwisp_monitoring/check/tasks.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
logger = logging.getLogger(__name__)
1111

1212

13+
def get_check_model():
14+
return load_model('check', 'Check')
15+
16+
1317
@shared_task
1418
def run_checks():
1519
"""
@@ -19,8 +23,13 @@ def run_checks():
1923
This allows to enqueue all the checks that need to be performed
2024
and execute them in parallel with multiple workers if needed.
2125
"""
22-
Check = load_model('check', 'Check')
23-
iterator = Check.objects.filter(is_active=True).only('id').values('id').iterator()
26+
iterator = (
27+
get_check_model()
28+
.objects.filter(is_active=True)
29+
.only('id')
30+
.values('id')
31+
.iterator()
32+
)
2433
for check in iterator:
2534
perform_check.delay(check['id'])
2635

@@ -31,9 +40,8 @@ def perform_check(uuid):
3140
Retrieves check according to the passed UUID
3241
and calls ``check.perform_check()``
3342
"""
34-
Check = load_model('check', 'Check')
3543
try:
36-
check = Check.objects.get(pk=uuid)
44+
check = get_check_model().objects.get(pk=uuid)
3745
except ObjectDoesNotExist:
3846
logger.warning(f'The check with uuid {uuid} has been deleted')
3947
return
@@ -43,39 +51,45 @@ def perform_check(uuid):
4351

4452

4553
@shared_task
46-
def auto_create_ping(model, app_label, object_id):
54+
def auto_create_ping(
55+
model, app_label, object_id, check_model=None, content_type_model=None
56+
):
4757
"""
4858
Called by django signal (dispatch_uid: auto_ping)
4959
registered in check app's apps.py file.
5060
"""
51-
Check = load_model('check', 'Check')
61+
Check = check_model or get_check_model()
5262
ping_path = 'openwisp_monitoring.check.classes.Ping'
5363
has_check = Check.objects.filter(
5464
object_id=object_id, content_type__model='device', check=ping_path
5565
).exists()
5666
# create new check only if necessary
5767
if has_check:
5868
return
59-
ct = ContentType.objects.get(app_label=app_label, model=model)
69+
content_type_model = content_type_model or ContentType
70+
ct = content_type_model.objects.get(app_label=app_label, model=model)
6071
check = Check(name='Ping', check=ping_path, content_type=ct, object_id=object_id)
6172
check.full_clean()
6273
check.save()
6374

6475

6576
@shared_task
66-
def auto_create_config_check(model, app_label, object_id):
77+
def auto_create_config_check(
78+
model, app_label, object_id, check_model=None, content_type_model=None
79+
):
6780
"""
6881
Called by openwisp_monitoring.check.models.auto_config_check_receiver
6982
"""
70-
Check = load_model('check', 'Check')
83+
Check = check_model or get_check_model()
7184
config_check_path = 'openwisp_monitoring.check.classes.ConfigApplied'
7285
has_check = Check.objects.filter(
7386
object_id=object_id, content_type__model='device', check=config_check_path,
7487
).exists()
7588
# create new check only if necessary
7689
if has_check:
7790
return
78-
ct = ContentType.objects.get(app_label=app_label, model=model)
91+
content_type_model = content_type_model or ContentType
92+
ct = content_type_model.objects.get(app_label=app_label, model=model)
7993
check = Check(
8094
name='Configuration Applied',
8195
check=config_check_path,

0 commit comments

Comments
 (0)