基于脚本的组件中的线程不起作用

thread in script based component not working

当我注释掉下面与线程相关的代码时,向 slack API 的提交按预期工作。但是,当我尝试在线程命令中处理提交时,提交永远不会通过。

我是不是做错了什么? 运行 在铁路 4

// push to slack!
public function push(options = {}) {

    var http = new http();
    var data = {
        "text": "Default message",
        "channel": "activity"
    };

    structAppend(data, options);

    // thread
    //  action="run" {

        sleep(5000);

        http.setMethod("post");
        http.setUrl(variables.slackWebhookUrl);
        http.addParam(
            type = "formField",
            name = "payload",
            value = serializeJson(data)
        );

        http.send();

    // }

}

感谢 Adam Cameron 为我指明了正确的方向。事实证明,scoping is quite tricky with cfthreads.

为了简洁起见,我只说以后使用线程时会遵循这些规则:

  1. 不要引用 "thread block"
  2. 范围之外的任何变量
  3. 显式将任何需要引用的"parent scope"数据传递给线程
  4. 基本上将 cfthread 视为 cfmodule(通过属性传递数据)

这是工作代码:

    // push to slack!   
    public function push(options = {}) {

        var data = {
            "text": "Default message",
            "channel": "activity"
        };

        structAppend(data, options);

        thread
            action="run"
            data="#data#"
            slackUrl="#variables.slackWebhookUrl#" {

            var http = new http();

            http.setMethod("post");
            http.setUrl(attributes.slackUrl);
            http.addParam(
                type = "formField",
                name = "payload",
                value = serializeJson(attributes.data)
            );

            http.send();

        }

    }