我想运行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总是返回Hook execution failed: Net::ReadTimeout
,我们知道npm install
和npm run build
需要很长时间。
因此,我想在后台服务中继续该过程,几秒钟后返回"pull & build welldone!"
。谢谢你。
答案 0 :(得分:2)
您可以为此使用celery:
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 here设置芹菜的说明。