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

Jaq*_*sos 6 python django worker amazon-ec2 amazon-web-services

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

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

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

在/ var /日志/的httpd /访问日志:

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

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

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

这是我的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()
Run Code Online (Sandbox Code Playgroud)

cron.yaml文件:

version: 1
cron:
 - name: "delete_expired_files"
   url: "/networks_app/delete_expired_files"   
   schedule: "* * * * *" 
Run Code Online (Sandbox Code Playgroud)

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

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

小智 3

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

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

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

@csrf_exempt
def index(request):
    return HttpResponse("hello, world")
Run Code Online (Sandbox Code Playgroud)