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

时间:2016-01-23 17:00:45

标签: python django amazon-web-services amazon-ec2 worker

我正在尝试设置工作线程来运行后台任务。我在两个环境中运行相同的应用程序版本,一个是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()

cron.yaml 文件:

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

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

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

1 个答案:

答案 0 :(得分:1)

创建POST请求的内部SQS守护程序不包含CSRF令牌,这可能导致“禁止”#39; 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")
相关问题