misc: aggregate environment change signals (#11308)

This commit is contained in:
Frédéric Péters 2016-06-12 10:49:45 +02:00
parent 7a9c37fbf9
commit 5cf4e96747
1 changed files with 18 additions and 1 deletions

View File

@ -14,10 +14,13 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import threading
from celery import Celery
from kombu.common import Broadcast
from django.conf import settings
from django.core.signals import request_finished
from django.db.models.signals import post_save
from django.dispatch import receiver
@ -27,10 +30,24 @@ from hobo.environment.models import Variable, AVAILABLE_SERVICES
from hobo.profile.models import AttributeDefinition
class Local(threading.local):
MUST_NOTIFY = False
tls = Local()
@receiver(post_save)
def notify_agents(sender, instance, **kwargs):
def post_environment_save(sender, instance, **kwargs):
if not sender in [Variable, AttributeDefinition] + AVAILABLE_SERVICES:
return
tls.MUST_NOTIFY = True
@receiver(request_finished)
def notify_agents(sender, **kwargs):
if not tls.MUST_NOTIFY:
return
tls.MUST_NOTIFY = False
with Celery('hobo', broker=settings.BROKER_URL) as app:
app.conf.update(
CELERY_TASK_SERIALIZER='json',