|
2 | 2 |
|
3 | 3 | from django.contrib.contenttypes.fields import GenericForeignKey |
4 | 4 | from django.contrib.contenttypes.models import ContentType |
5 | | -from django.db import models |
| 5 | +from django.db import models, transaction |
6 | 6 | from django.db.models.signals import post_save |
| 7 | +from django.dispatch import receiver |
7 | 8 | from django.utils.functional import cached_property |
8 | 9 | from django.utils.module_loading import import_string |
9 | 10 | from django.utils.translation import ugettext_lazy as _ |
@@ -75,27 +76,45 @@ def perform_check(self, store=True): |
75 | 76 | return self.check_instance.check(store=True) |
76 | 77 |
|
77 | 78 |
|
78 | | -if app_settings.AUTO_PING: |
79 | | - from django.db import transaction |
80 | | - from django.dispatch import receiver |
| 79 | +@receiver(post_save, sender=Device, dispatch_uid='auto_ping') |
| 80 | +def auto_ping_receiver(sender, instance, created, **kwargs): |
| 81 | + """ |
| 82 | + Implements OPENWISP_MONITORING_AUTO_PING |
| 83 | + The creation step is executed in the background |
| 84 | + """ |
| 85 | + # we need to skip this otherwise this task will be executed |
| 86 | + # every time the configuration is requested via checksum |
81 | 87 | from openwisp_monitoring.check.tasks import auto_create_ping |
82 | 88 |
|
83 | | - @receiver(post_save, sender=Device, dispatch_uid='auto_ping') |
84 | | - def auto_ping_receiver(sender, instance, created, **kwargs): |
85 | | - """ |
86 | | - Implements OPENWISP_MONITORING_AUTO_PING |
87 | | - The creation step is executed in the backround |
88 | | - """ |
89 | | - # we need to skip this otherwise this task will be executed |
90 | | - # every time the configuration is requested via checksum |
91 | | - if not created: |
92 | | - return |
93 | | - with transaction.atomic(): |
94 | | - transaction.on_commit( |
95 | | - lambda: auto_create_ping.delay( |
96 | | - model=sender.__name__.lower(), |
97 | | - app_label=sender._meta.app_label, |
98 | | - object_id=str(instance.pk), |
99 | | - created=created, |
100 | | - ) |
| 89 | + if not app_settings.AUTO_PING or not created: |
| 90 | + return |
| 91 | + with transaction.atomic(): |
| 92 | + transaction.on_commit( |
| 93 | + lambda: auto_create_ping.delay( |
| 94 | + model=sender.__name__.lower(), |
| 95 | + app_label=sender._meta.app_label, |
| 96 | + object_id=str(instance.pk), |
| 97 | + ) |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +@receiver(post_save, sender=Device, dispatch_uid='auto_config_status') |
| 102 | +def auto_config_status_receiver(sender, instance, created, **kwargs): |
| 103 | + """ |
| 104 | + Implements OPENWISP_MONITORING_AUTO_CONFIG_STATUS |
| 105 | + The creation step is executed in the background |
| 106 | + """ |
| 107 | + # we need to skip this otherwise this task will be executed |
| 108 | + # every time the configuration is requested via checksum |
| 109 | + from openwisp_monitoring.check.tasks import auto_create_config_status |
| 110 | + |
| 111 | + if not app_settings.AUTO_CONFIG_STATUS or not created: |
| 112 | + return |
| 113 | + with transaction.atomic(): |
| 114 | + transaction.on_commit( |
| 115 | + lambda: auto_create_config_status.delay( |
| 116 | + model=sender.__name__.lower(), |
| 117 | + app_label=sender._meta.app_label, |
| 118 | + object_id=str(instance.pk), |
101 | 119 | ) |
| 120 | + ) |
0 commit comments