回调后,node.js和AWS Lambda Function继续执行函数 [英] node.js and AWS Lambda Function continue function execution after callback

查看:87
本文介绍了回调后,node.js和AWS Lambda Function继续执行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用lambda函数来更改数据库,然后发送推送通知.

I am trying to use a lambda function to alter a database and then send a push notification.

我不想等待推送通知服务器回复.在推送通知不成功的情况下,不必担心.更重要的是,该功能应及时执行.

I don't want to wait for the push notification server to reply. In the occasional case that the push notification is unsuccessful, that is not a concern. It is more important that the function executes in a timely manner.

当前,我正在使用以下两个功能.一切都按预期工作,除了似乎没有节省时间.即,当没有设备令牌并且不需要推送时,该功能非常快.当需要推动时,它非常慢.这说明我在做什么,这是错误的,该函数仍在等待回调.

Currently I'm using the following two functions. Everything works as expected except that there doesn't seem to be any time saving. ie, when there is no device token and push is not required the function is very fast. When a push is required it is very slow. That tells me what I'm doing is wrong and the function is still waiting for a callback.

我没有太多使用节点,并且知道尝试使用其他语言的异步模型会带来危险.只是想知道如何克服这种情况.

I have not used node much and know there are perils with trying to use asynchronous models from other languages. Just wondering how to overcome this case.

用于数据库插入的功能

const AWS = require('aws-sdk');
var mysql = require('mysql');
var lambda = new AWS.Lambda();

exports.handler = (event, context, callback) => {

    var connection = mysql.createConnection({
        host: "databaseHost",
        user: "databaseUser",
        password: "databasePassword",
        database: "databaseName",
        multipleStatements: true
    });

    var sql = "INSERT INTO someTable SET item_id = ?, item_name = ?"

    var inserts = [event.itemId, event.itemName];

    connection.query(sql, inserts, function (error, results, fields) {
        connection.end();
        // Handle error after the release.
        if (error) {
            callback(error);
        } else {

            if (event.userToken !== null) {

                callback(null, results);

                var pushPayload = { "deviceToken": event.deviceToken };

                var pushParams = {
                    FunctionName: 'sendPushNotification',
                    InvocationType: 'RequestResponse',
                    LogType: 'Tail',
                    Payload: JSON.stringify(pushPayload)
                };

                lambda.invoke(pushParams, function (err, data) {
                    if (err) {
                        context.fail(err);
                    } else {
                        context.succeed(data.Payload);

                    }
                });
            } else {
                //callback(null, results);
                callback(null, results);
            }
        }
    });
};

推送通知功能:

const AWS = require('aws-sdk');
var ssm = new AWS.SSM({ apiVersion: '2014-11-06' });
var apn = require("apn");

exports.handler = function (event, context) {

    var options = {
        token: {
            key: "key",
            keyId: "keyId",
            teamId: "teamId"
        },
        production: true
    };

    var token = event.deviceToken;

    var apnProvider = new apn.Provider(options);


    var notification = new apn.Notification();

    notification.alert = "message";

    notification.topic = "com.example.Example";

    context.callbackWaitsForEmptyEventLoop = false;
    apnProvider.send(notification, [deviceToken]).then((response) => {
        context.succeed(event);
    });
};

推荐答案

在pushParams中,将 InvocationType 的值更改为"Event",以便调用lambda不会等待响应.它将仅调用lambda并返回回调.

In pushParams change value of InvocationType to "Event" so that calling lambda will not wait for the response. It will just invoke lambda and return you the callback.

示例:

var pushParams = {
                    FunctionName: 'sendPushNotification',
                    InvocationType: 'Event',
                    LogType: 'Tail',
                    Payload: JSON.stringify(pushPayload)
                };

这篇关于回调后,node.js和AWS Lambda Function继续执行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆