10
10
logger = logging .getLogger (__name__ )
11
11
12
12
13
+ def get_check_model ():
14
+ return load_model ('check' , 'Check' )
15
+
16
+
13
17
@shared_task
14
18
def run_checks ():
15
19
"""
@@ -19,8 +23,13 @@ def run_checks():
19
23
This allows to enqueue all the checks that need to be performed
20
24
and execute them in parallel with multiple workers if needed.
21
25
"""
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
+ )
24
33
for check in iterator :
25
34
perform_check .delay (check ['id' ])
26
35
@@ -31,9 +40,8 @@ def perform_check(uuid):
31
40
Retrieves check according to the passed UUID
32
41
and calls ``check.perform_check()``
33
42
"""
34
- Check = load_model ('check' , 'Check' )
35
43
try :
36
- check = Check .objects .get (pk = uuid )
44
+ check = get_check_model () .objects .get (pk = uuid )
37
45
except ObjectDoesNotExist :
38
46
logger .warning (f'The check with uuid { uuid } has been deleted' )
39
47
return
@@ -43,39 +51,45 @@ def perform_check(uuid):
43
51
44
52
45
53
@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
+ ):
47
57
"""
48
58
Called by django signal (dispatch_uid: auto_ping)
49
59
registered in check app's apps.py file.
50
60
"""
51
- Check = load_model ( 'check' , 'Check' )
61
+ Check = check_model or get_check_model ( )
52
62
ping_path = 'openwisp_monitoring.check.classes.Ping'
53
63
has_check = Check .objects .filter (
54
64
object_id = object_id , content_type__model = 'device' , check = ping_path
55
65
).exists ()
56
66
# create new check only if necessary
57
67
if has_check :
58
68
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 )
60
71
check = Check (name = 'Ping' , check = ping_path , content_type = ct , object_id = object_id )
61
72
check .full_clean ()
62
73
check .save ()
63
74
64
75
65
76
@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
+ ):
67
80
"""
68
81
Called by openwisp_monitoring.check.models.auto_config_check_receiver
69
82
"""
70
- Check = load_model ( 'check' , 'Check' )
83
+ Check = check_model or get_check_model ( )
71
84
config_check_path = 'openwisp_monitoring.check.classes.ConfigApplied'
72
85
has_check = Check .objects .filter (
73
86
object_id = object_id , content_type__model = 'device' , check = config_check_path ,
74
87
).exists ()
75
88
# create new check only if necessary
76
89
if has_check :
77
90
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 )
79
93
check = Check (
80
94
name = 'Configuration Applied' ,
81
95
check = config_check_path ,
0 commit comments