Code Monkey home page Code Monkey logo

Comments (5)

shrickus avatar shrickus commented on August 13, 2024

No, this is not normal... I'm seeing the same thing when using the aws lambda node inside a split/join flow. In order for the join to work, the msg.parts object must remain intact. Instead, the last one that was received is being returned by all the callbacks.

In my tests, I have an array of 3 msg objects, representing 3 sets of args to pass to Lambda. After the split node, the first msg.parts looks like this:

{"id":"52f290c1.afbc","type":"array","count":3,"len":1,"index":0}

but when returned from aws, the output msg.parts index is set to the last input msg index value:

{"id":"52f290c1.afbc","type":"array","count":3,"len":1,"index":2}`

So after the join, all of the results that were calculated in Lambda are placed into a 3-element array -- but since they all have index = 2, the output array is full of nulls, except for the last element:

[null, null, {... results of the last aws call ...}]

from node-red-contrib-aws.

Daniel-t avatar Daniel-t commented on August 13, 2024

Sorry I'm not following.
Can one of you provide a test scenario? With an example lambda script or dynamo table, and flow.

from node-red-contrib-aws.

shrickus avatar shrickus commented on August 13, 2024

Here is a thread on slack that highlights what I found:
https://node-red.slack.com/archives/C03M2TAQ8/p1544543818421100

Basically, the problem is within this code: (## comments are mine)

this.on('input', function(msg) {    ## called with every new msg object
    node.cb = function(err, data) {    ## these args do not have a reference to the incoming object...
        if (err) {
            node.status({fill:"red",shape:"ring",text:"error"});
            node.error("failed: " + err.toString(),msg);
            return;
        } else {
            msg.payload = data;    ## the scope of this msg object is outside of this callback function
            node.status({});
        }
        node.send(msg);    ## when the cb finally gets called, it uses the last msg that arrived
    };
    // pass 'msg' to lambda, and invoke callback...
    ## when multiple msgs arrive, they all get passed to AWS before any node.cb function is invoked
    ec2.invoke(params, node.cb);    
});

This is not a problem only with AWS, but with any async call that invokes a callback function when it returns its data. I'll see if I can find an example of how this is handled in other async service nodes.

from node-red-contrib-aws.

shrickus avatar shrickus commented on August 13, 2024

Ok, it looks like one technique is to pass the incoming msg object to a function that returns another function, which has the signature expected by the AWS callback logic -- something like this (untested):

this.on('input', function(msg) {
    function aws_cb(msg_in) {
        return function(err, data) {
            if (err) {
                node.status({fill:"red",shape:"ring",text:"error"});
                node.error("failed: " + err.toString(), msg_in);
                return;
            } else {
                msg_in.payload = data;
                node.status({});
            }
            node.send(msg_in);
        };
    };
    // pass incoming 'msg' to lambda, and invoke callback with original msg...
    ec2.invoke(params, aws_cb(msg));
});

So each time a new msg arrives, we create a callback function by passing in the msg object:
aws_cb(msg)
That callback function contains the original msg object (and its "parts" property for the join node to work correctly).

from node-red-contrib-aws.

Daniel-t avatar Daniel-t commented on August 13, 2024

I expect this to be resolved in the recent update. (which changed the scope of the msg object)

from node-red-contrib-aws.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.