繁体   English   中英

增加python“operation_timeout”中slack slash命令的超时时间

[英]Increase timeout from slack slash command in python "operation_timeout"

我在 python 脚本下运行,它最终运行 shell 脚本,它为我提供了 k8s 命名空间中运行版本的列表。 得到结果,但需要时间> 3 秒。 因此,这会导致松弛导致“operation_timeout”。 我是 python 的新手,已经阅读了有关延迟的各种文档,但这并没有帮助,因为这些文档非常复杂。

from subprocess import Popen, PIPE
from subprocess import check_output
from flask import Flask

def get_shell_script_output_using_communicate():
    session = subprocess.Popen(['./version.sh'], stdout=PIPE, stderr=PIPE)
    stdout, stderr = session.communicate()
    if stderr:
        raise Exception("Error "+str(stderr))
    return stdout.decode('utf-8')

def get_shell_script_output_using_check_output():
    stdout = check_output(['./version.sh']).decode('utf-8')
    return stdout

app = Flask(__name__)

@app.route('/test',methods=['POST'])
def home():
    return '`Version List` ```'+get_shell_script_output_using_check_output()+'```'

app.run(host='0.0.0.0', port=5002, debug=True)

即使命令超过 10 秒,有没有办法获得响应? 谢谢!

无法将默认超时从 Slack 增加到斜线命令。 总是3秒。 但最多可以发送 30 分钟的延迟响应。

为此,您首先需要在 3 秒内通过发回 HTTP 200 OK 来确认初始请求。 由于这需要您完成当前请求并终止主脚本,因此您需要并行运行延迟响应函数。 这可以在进程、线程中,通过调用 celery 任务或任何其他方式允许您生成并行运行的 python 函数。

然后,并行函数可以通过将消息发送到 Slack 请求中response_url提供的 URL 来response_url Slack。

这是一个使用线程的示例实现:

import threading
from time import sleep
from flask import Flask, json, request
import requests

app = Flask(__name__) #create the Flask app

@app.route('/slash', methods=['POST'])
def slash_response():                
    """endpoint for receiving all slash command requests from Slack"""

    # get the full request from Slack
    slack_request = request.form

    # starting a new thread for doing the actual processing    
    x = threading.Thread(
            target=some_processing,
            args=(slack_request,)
        )
    x.start()

    ## respond to Slack with quick message
    # and end the main thread for this request
    return "Processing information.... please wait"


def some_processing(slack_request):
    """function for doing the actual work in a thread"""

    # lets simulate heavy processing by waiting 7 seconds
    sleep(7)

    # response to Slack after processing is finished
    response_url = slack_request["response_url"]    
    message = {        
        "text": "We found a result!"
    }
    res = requests.post(response_url, json=message)

if __name__ == '__main__':
    app.run(debug=True, port=8000) #run app in debug mode on port 8000

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM