2

I realize there are many other questions related to custom django signals that don't work, and believe me, I have read all of them several times with no luck for getting my personal situation to work.

Here's the deal: I'm using django-rq to manage a lengthy background process that is set off by a particular http request. When that background process is done, I want it to fire off a custom Django signal so that the django-rq can be checked for any job failure/exceptions.

Two applications, both on the INSTALLED_APPS list, are at the same level. Inside of app1 there is a file: signals.py

import django.dispatch

file_added = django.dispatch.Signal(providing_args=["issueKey", "file"])
fm_job_done = django.dispatch.Signal(providing_args=["jobId"])

and also a file jobs.py

from app1 import signals
from django.conf import settings

jobId = 23
issueKey = "fake"
fileObj = "alsoFake"

try:
    pass
finally:
    signals.file_added.send(sender=settings.SIGNAL_SENDER,issueKey=issueKey,fileName=fileObj)
    signals.fm_job_done.send(sender=settings.SIGNAL_SENDER,jobId=jobId)

then inside of app2, in views.py

from app1.signals import file_added, fm_job_done
from django.conf import settings

#Setup signal handlers
def fm_job_done_callback(sender, **kwargs):
    print "hellooooooooooooooooooooooooooooooooooo"
    logging.info("file manager job done signal fired")

def file_added_callback(sender, **kwargs):
    print "hellooooooooooooooooooooooooooooooooooo"
    logging.info("file added signal fired")

file_added.connect(file_added_callback,sender=settings.SIGNAL_SENDER,weak=False)
fm_job_done.connect(fm_job_done_callback,sender=settings.SIGNAL_SENDER,weak=False)

I don't get any feedback whatsoever though and am at a total loss. I know for fact that jobs.py is executing, and therefore also that the block of code that should be firing the signals is executing as well since it is in a finally block (no the try is not actually empty - I just put pass there for simplicity) Please feel free to ask for more information - I'll respond asap.

pooley1994
  • 723
  • 4
  • 16
  • having the same problem, did you ever find a solution for this? It's definately an issue with django-rq as the signal receiver works fine with `async=False` – SColvin Apr 03 '15 at 11:47
  • @SColvin Hi there. No I'm afraid I never did really find a solution. The below answer certainly did not solve my problem. I have since moved on to a different paradigm relying more heavily on django-rq and http requests. – pooley1994 Apr 04 '15 at 16:36

2 Answers2

0

here is the solution for django > 2.0

  1. settings.py:

change name of your INSTALLED_APPS from 'app2' to 'app2.apps.App2Config'

  1. app2 -> apps.py:

from app1.signals import file_added, fm_job_done

Class App2Config(AppConfig):
    name = 'app2'

    def ready(self):
        from .views import fm_job_done_callback, file_added_callback
        file_added.connect(file_added_callback)
        fm_job_done.connect(fm_job_done_callback)
idirall22
  • 316
  • 2
  • 11
-1

use django receiver decorator

from django.dispatch import receiver
from app1.signals import file_added, fm_job_done

@receiver(fm_job_done)
def fm_job_done_callback(sender, **kwargs):
    print "helloooooooooooooo"


@receiver(file_added)
def file_added_callback(sender, **kwargs):
    print "helloooooooooooooo"

Also, I prefer to handle signals in models.py

levi
  • 22,001
  • 7
  • 73
  • 74