Django允许在另一个进程完成之前返回响应
我想运行webhook时遇到问题。在这种情况下,我想运行另一个脚本来构建项目,比如说Django允许在另一个进程完成之前返回响应,django,gitlab,webhooks,Django,Gitlab,Webhooks,我想运行webhook时遇到问题。在这种情况下,我想运行另一个脚本来构建项目,比如说runaway.sh #!/bin/bash cd /home/myuser/envs/project-vue git pull https://username:password@gitlab.com/username/project-vue npm install npm run build 然后在我的views.py中,我尝试调用它命令: @csrf_exempt def gitlab_webhook_
runaway.sh
#!/bin/bash
cd /home/myuser/envs/project-vue
git pull https://username:password@gitlab.com/username/project-vue
npm install
npm run build
然后在我的views.py
中,我尝试调用它命令:
@csrf_exempt
def gitlab_webhook_view(request):
header_signature = request.META.get('HTTP_X_GITLAB_TOKEN')
if header_signature == settings.GITLAB_WEBHOOK_KEY:
subprocess.call(os.path.join(settings.BASE_DIR, 'runaway.sh'))
return HttpResponse('pull & build welldone!')
return HttpResponseForbidden('Permission denied.')
但是gitlab总是返回钩子执行失败:Net::ReadTimeout
,我们知道npm安装
&npm运行构建
需要很长时间
因此,我想在后台服务中继续这个过程,并在几秒钟内返回“pull&buildwelldone!”
。在此之前谢谢。您可以使用:
from celery import Celery
app = Celery('tasks', broker='pyamqp://guest@localhost//')
@app.task
def pull_proc():
subprocess.call(os.path.join(settings.BASE_DIR, 'runaway.sh'))
在视图中,您可以在后台调用此任务,如下所示:
@csrf_exempt
def gitlab_webhook_view(request):
header_signature = request.META.get('HTTP_X_GITLAB_TOKEN')
if header_signature == settings.GITLAB_WEBHOOK_KEY:
pull_proc.delay()
return HttpResponse('pull & build welldone!')
return HttpResponseForbidden('Permission denied.')
您可以找到如何用django设置芹菜的说明