1010logger = logging .getLogger (__name__ )
1111
1212
13+ def get_check_model ():
14+ return load_model ('check' , 'Check' )
15+
16+
1317@shared_task
1418def 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