我对这里发生的事情感到茫然。我有一个简单的django网站,我登录并填写一些基本表格,其中包含一些信息(设备地址,电源插座1/2,开/关)。当我提交表单时,RQ将此任务(使用请求对URL发出HTTP GET请求)提交到队列中并执行。我在函数结束时返回'JOB FINISHED',我看到作业正在完成结果'JOB FINISHED'。因此该函数正在执行但由于某种原因它会跳过所有if / elif块。查看django-rq仪表板和工作台控制台输出,看起来正好正确传递了参数。如果我将函数复制/粘贴到python窗口中,它会在我传递相同的参数时成功运行。
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"
工人输出:
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