thread in script based component not working

142 Views Asked by At

When I comment out the thread related code below, the submission to the slack API works as expected. However, when I attempt to process the submission within a thread command, the submission never goes through.

Am I doing something wrong? Running in Railo 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();

    // }

}
1

There are 1 best solutions below

0
Brian FitzGerald On BEST ANSWER

Credit goes to Adam Cameron for pointing me in the right direction. As it turns out, scoping is quite tricky with cfthreads.

For the sake of brevity, I will just say that I will follow these rules when using threads in the future:

  1. do not reference any variables from scopes outside the "thread block"
  2. explicitly pass into the thread any "parent scope" data you need to reference
  3. basically treat the cfthread as a cfmodule (passing data through attributes)

This is working code:

    // 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();

        }

    }