0

I'm at a loss as to what is happening here. I have a simple django web site where I log in and fill out a basic form with some information (device address, electrical socket 1/2, on/off). When I submit the form, RQ submits this task (which uses requests to make a HTTP GET request to a URL) into the queue and it is executed. I put a return 'JOB FINISHED' at the end of the function and I see that the jobs are finishing with result 'JOB FINISHED'. So the function is being executed but for some reason it skips over all of the if/elif blocks. Looking at the django-rq dashboard and the worker console output, it looks like the arguments are being passed correctly. If I copy/paste the function into a python window it runs successfully when I pass it the same arguments.

views.py

@login_required(login_url="/login/")
def index(request):
    if request.method == 'POST':
        form = APIForm(request.POST)
        if form.is_valid():
            queue = django_rq.get_queue('default')
            queue.enqueue(api_call, form.cleaned_data['device'], form.cleaned_data['socket'], form.cleaned_data['change_to'])
            obj = SocketChange()
            obj.device = form.cleaned_data['device']
            obj.socket = form.cleaned_data['socket']
            obj.change_to = form.cleaned_data['change_to']
            obj.submitter = request.user
            obj.save()
            return HttpResponseRedirect(reverse('log'))
        else:
            return render(request, 'api/index.html', {'form': form})
    else:
        form = APIForm()
        return render(request, 'api/index.html', {'form': form})

tasks.py

@job
def api_call(device, socket, change_to):
    basicAuthUser = 'user'
    basicAuthPassword = 'pass'

    #
    # port: socket to be controlled
    # ctrl_kind: 1 = outlet on, 2 = outlet off, 3 = outlet reboot, 4 = outlet reboot
    # status value (Ret): 0 = outlet off, 1 = outlet on, 2 = switching on, 3 = switching off
    #

    if socket == "1" and change_to == 'True':
        url = "https://{0}/out_ctrl.csp?port=1&ctrl_kind=1".format(device)
        response = requests.get(url, auth=HTTPBasicAuth(basicAuthUser, basicAuthPassword), verify=False)
        response = response.json()
        if response['OutCtrl'][0]['Ret'] == 1:
            return "Socket 1 is on!"
        else:
            return "Error!"
    elif socket == "1" and change_to == 'False':
        url = "https://{0}/out_ctrl.csp?port=1&ctrl_kind=2".format(device)
        response = requests.get(url, auth=HTTPBasicAuth(basicAuthUser, basicAuthPassword), verify=False)
        response = response.json()
        if response['OutCtrl'][0]['Ret'] == 0:
            return "Socket 1 is off!"
        else:
            return "Error!"
    elif socket == "2" and change_to == 'True':
        url = "https://{0}/out_ctrl.csp?port=2&ctrl_kind=1".format(device)
        response = requests.get(url, auth=HTTPBasicAuth(basicAuthUser, basicAuthPassword), verify=False)
        response = response.json()
        if response['OutCtrl'][0]['Ret'] == 1:
            return "Socket 2 is on!"
        else:
            return "Error!"
    elif socket == "2" and change_to == 'False':
        url = "https://{0}/out_ctrl.csp?port=2&ctrl_kind=2".format(device)
        response = requests.get(url, auth=HTTPBasicAuth(basicAuthUser, basicAuthPassword), verify=False)
        response = response.json()
        if response['OutCtrl'][0]['Ret'] == 0:
            return "Socket 2 is off!"
        else:
            return "Error!"
    return "JOB FINISHED"

Worker output:

14:22:06 *** Listening on default...
14:22:06 Sent heartbeat to prevent worker timeout. Next one should arrive within 420 seconds.
14:27:09 default: api.tasks.api_call('test-device', '1', 'False') (cb9f4827-d06c-471b-a81b-2960e983dda0)
14:27:09 Sent heartbeat to prevent worker timeout. Next one should arrive within 420 seconds.
14:27:10 Sent heartbeat to prevent worker timeout. Next one should arrive within 420 seconds.
Current job: cb9f4827-d06c-471b-a81b-2960e983dda0
14:27:10 default: Job OK (cb9f4827-d06c-471b-a81b-2960e983dda0)
14:27:10 Result: 'JOB FINISHED'
14:27:10 Result is kept for 500 seconds
0xDECAFBAD
  • 154
  • 1
  • 10
  • your main `if` block that wraps everything else has no `else` only `elif` ... so none of your criteria match. You'll be able to see this if you put an `else` and print something different there – Cfreak Mar 09 '17 at 20:40
  • You probably have a type problem. You're checking `socket == "1"` which is a string. Are you certain the variable isn't an integer? `1` does not equal `"1"` – Cfreak Mar 09 '17 at 20:41
  • I added the else block with some return text to see if that would be matched, restarted django and the rq worker, and now it looks like it's hitting one of the if/elif blocks. I did change the change_to in the if/elif blocks from a boolean to a string True/False but that was before I restarted django and the worker. I guess if I make changes to my task I have to restart the worker? Django automatically reloads the dev server when you make a change so I thought it would take effect immediately but it must have something to do with the RQ worker process... – 0xDECAFBAD Mar 09 '17 at 20:46

0 Answers0