【问题标题】:Using a promise to return the lambda handler function使用 promise 返回 lambda 处理函数
【发布时间】:2018-11-27 13:14:20
【问题描述】:

我正在为我的 lambda 处理程序使用 aws-serverless-express,具体如下:

const app = express(); const server = createServer(app, null); export const app = (event, context) => proxy(server, event, context);

这一切都很好,但我想在最后一行导出应用程序之前执行一些异步操作。

我认为我可以返回一个承诺,而不是在我的所有操作完成后解析代理,但这似乎不起作用。

有什么方法可以延迟导出处理程序,直到某些异步代码完成?

更新

虽然我已经有一段时间没有使用 lambda,但我确实做到了这一点,因此现在可能有替代解决方案。我当时所做的工作如下所示

import * as Promise from 'bluebird';
import {Callback, Context, Handler} from 'aws-lambda';
import {createServer, proxy} from 'aws-serverless-express';
import {eventContext} from 'aws-serverless-express/middleware';
import {Application} from 'express';
import {ServerlessApp} from './common/serverless-express-app';
import {startComponents} from './startup';

import * as http from 'http';

const INSTANCE = new ServerlessApp(startComponents());
const getServer = INSTANCE.initCompletePromise
.then((serverLessApp: Application) => {
    serverLessApp.use(eventContext());
    return createServer(serverLessApp);
});

const apiGWExpressApp: Handler = (event: any, context: Context, callback: 
  Callback) => {
  // context.callbackWaitsForEmptyEventLoop = false;
  getServer
    .then((server: http.Server) => {
        proxy(server, event, context);
    })
    .catch(error => {
        CommonHelper.doHandlerCallback(() => {
            INSTANCE.logger.error('ERROR: ', error);
            context.fail(JSON.stringify(error));
        });
    });
};

export {
   apiGWExpressApp
};

它的要点是我的自定义 ServerlessApp 将根据在公开可用的承诺中的构造函数中提供的内容执行各种设置步骤,例如

export class ServerlessApp {
    private _initCompletePromise: Promise<express.Application>;
    private _app: express.Application;
    constructor(startComponents: StartComponent[]) {
        this._app = express();

       this._initCompletePromise = 
           new Promise<express.Application>((resolve, reject) => { 
               ...
               resolve(this._app);
           }
    }
    public get initCompletePromise(): Promise<express.Application> {
        return this._initCompletePromise;
    }
}

通过这种方式,我能够推迟处理程序方法中的主要 createServer 调用,直到我能够完成所有初始化设置。

【问题讨论】:

  • 你能解决这个问题吗?我也面临同样的问题。
  • @Jose 查看添加到问题中的更新
  • 谢谢@sam。我找到了我需要的解决方案。我使用serverless 创建回调函数。我只好把它包起来

标签: node.js aws-lambda


【解决方案1】:

我遇到了这个问题。也许你现在已经解决了。对我来说,这是冷启动,我想让我的应用程序在开始处理请求之前完全初始化,但异步加载或重新加载 aws-serverless 模块不起作用。相反,我在我的 app.js/main js 文件中定义了一个默认路由,并且该路由在系统中的所有其他路由之前执行。我将 next() 参数传递给它,并且在我的初始加载时,它会延迟点击 next 直到完成,因此我的应用程序中的所有请求响应都将保持到系统加载为止。

在我的 app.js 中

app.use((req, res, next) => {
  systemService.initSystemConfiguration().then(() => {
    next()
  })
})

system.services.js

module.exports.initSystemConfiguration = function() {
if(systemInitialized) {
     return Promise.resolve(true)
}
return longComputationReturningPromise()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 2016-08-01
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多