3

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

    // }

}
Brian FitzGerald
  • 3,041
  • 3
  • 28
  • 38
  • don't you need to name your thread? – Matt Busche Jan 08 '16 at 03:40
  • What are you looking at that makes you think the submission does not go through? – Dan Bracuk Jan 08 '16 at 03:53
  • Thanks guys. I've tried it w/ and without a name attribute. According to the cfdocs, the name attribute is not required. I am looking at my Slack channel at myapp.slack.com to confirm whether not submission has been processed. Without the thread, instantly posts, with it, nothing. – Brian FitzGerald Jan 08 '16 at 04:36
  • 2
    Is there no error in your... application log (might be your exception log, can't remember where they go)? I suspect your thread cannot see the argument scope, and you're not passing `data` into it. – Adam Cameron Jan 08 '16 at 06:42
  • 1
    Thanks Adam. I will look at those logs tonight. Now that you mention it, I am remembering that threads can't seem to get to the variables in the context they are created (that always throws me off). I guess my new approach (that I need to remember) will be to treat cfthreads as cfmodules and pass in any data via explicit attributes. I will try this tonight (as a result of your comment) and post an update. – Brian FitzGerald Jan 08 '16 at 14:23

1 Answers1

2

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

        }

    }
Brian FitzGerald
  • 3,041
  • 3
  • 28
  • 38