阅读背景:

Elastic Beanstalk中的Django POST监听器接收AWS Worker层请求

来源:互联网 

I'm trying to set up a Worker Environment to run a background task. I have the same application version running on two Environments, one is the web server and the other one is the Worker.

我正在尝试设置一个工作环境来运行后台任务。我在两个环境中运行相同的应用程序版本,一个是Web服务器,另一个是Worker。

I need to delete files periodically according to expiration date. I've mapped a view to be the URL on localhost where messages will be forwarded as HTTP POST requests. The task is being scheduled and seems like SQS is running but the messages are all at the WorkerDeadLetterQueue.

我需要根据到期日期定期删除文件。我已将视图映射为localhost上的URL,其中消息将作为HTTP POST请求转发。该任务正在安排,似乎SQS正在运行,但消息都在WorkerDeadLetterQueue。

At the log file I got the requests being made but a 403 error:

在日志文件中我收到了请求但是403错误:

/var/log/httpd/access_log:

"POST /networks_app/delete_expired_files HTTP/1.1" 403 2629 "-" "aws-sqsd/2.0"

“POST / networks_app / delete_expired_files HTTP / 1.1”403 2629“ - ”“aws-sqsd / 2.0”

and this at /var/log/aws-sqsd/default.log:

这个在/var/log/aws-sqsd/default.log:

message: sent to %[https://localhost:80/networks_app/delete_expired_files] 2016-01-23T14:58:05Z http-err: d5f645cf-ce15-40bc-8ee3-34acb79e797b (4) 403 - 0.007

消息:发送到%[https:// localhost:80 / networks_app / delete_expired_files] 2016-01-23T14:58:05Z http-err:d5f645cf-ce15-40bc-8ee3-34acb79e797b(4)403 - 0.007

Here is my views.py code:

这是我的views.py代码:

def delete_expired_files(request):
    if request.method == 'POST':
        users = DemoUser.objects.all()
        for user in users:
            documents = Document.objects.filter(owner=user.id)
            if documents:
                for doc in documents:
                    now = timezone.now()
                    if now >= doc.date_published + timedelta(days = doc.owner.group.valid_time):
                        doc.delete()

The cron.yaml file:

cron.yaml文件:

version: 1
cron:
 - name: "delete_expired_files"
   url: "/networks_app/delete_expired_files"   
   schedule: "* * * * *" 

If I access the URL via browser it works, It shows a GET request at the log_file of my web application server.

如果我通过浏览器访问URL它可以工作,它在我的Web应用程序服务器的log_file中显示一个GET请求。

What should I do to make the Worker Environment execute the task?
Why when the Worker tries to send a message, it returns a 403 error?
Is it related to the role permissions?
Should I code a specific listener in Django?
Is using celery the best way to solve this issue?

我该怎么做才能让Worker Environment执行任务?为什么当Worker尝试发送消息时,它会返回403错误?它与角色权限有关吗?我应该在Django中编写特定的侦听器吗?使用芹菜是解决这个问题的最佳方法吗?

1 个解决方案

#1


1  

The internal SQS daemon that creates the POST request does not include a CSRF token, which can lead to '403 Forbidden' errors.

创建POST请求的内部SQS守护程序不包含CSRF令牌,这可能导致“403 Forbidden”错误。

A potential workaround for this is to mark the method as csrf_exempt:

一个潜在的解决方法是将方法标记为csrf_exempt:

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def index(request):
    return HttpResponse("hello, world")

分享到: