这些worker函数是RabbitMQ消费者,处理传入的请求并回复响应。
我正在寻找关于如何在部署Lambda函数后保持这些worker函数始终处于活动状态的指导。什么样的配置或做法可以帮助实现这一点?
// app.js
import e from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import 'dotenv/config.js';
import mongoose from 'mongoose';
import SwaggerRouter from './swagger.js';
import logger from './logger.js';
import StartUpRouter from './routes/user_founder.js'
import VCRouter from './routes/user_vc.js';
import SuperAdminRouter from './routes/superadmin.js';
import { handleVCListRequest, startTokenValidationWorker, startUserValidationWorker, startVCDataWorker } from './workers/messageQueueWorker.js';
import { startWorker } from './services/rabbitMQService.js';
import { processMessage } from './workers/emailWorker.js';
const app = e();
app.use(cors());
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(SwaggerRouter);
// ...other routes and middleware...
mongoose.connect(process.env.MONGO_URL, { useUnifiedTopology: true, useNewUrlParser: true })
.then(() => {
app.listen(process.env.PORT || 4001);
logger.info({listening: `Server is Processing on ${process.env.PORT}.`});
logger.info({success: "Connected to AuthenticationService Database."});
console.log({listening: `Server is Processing on ${process.env.PORT}.`});
console.log({success: "Connected to Database."});
startWorker(processMessage).catch((error) => {
console.error('Error while sending email:', error.message);
});
startTokenValidationWorker().catch((error) => {
logger.error('Error starting token validation worker:', error.message);
console.error('Error starting token validation worker:', error.message);
});
// ...other worker functions...
handleVCListRequest().catch((error) => {
logger.error('Error starting vc list worker:', error.message)
console.error('Error starting vc list worker:', error.message);
});
})
.catch((err) => {
logger.error({error: err.message});
console.error({error: err});
});
export default app;
// lambda.js
import ServerlessHttp from "serverless-http";
import app from "./app.js";
export const handler = ServerlessHttp(app);
1条答案
按热度按时间wvyml7n51#
单个AWS Lambda调用(示例)不能运行超过15分钟。您目前设计系统的方式根本无法在AWS Lambda环境中工作。
如果您通过Amazon MQ运行RabbitMQ,则当Amazon MQ RabbitMQ示例中有消息时,您可以使用Amazon automatically invoke your Lambda function。在这种设置中,Lambda函数不会连接到RabbitMQ,而是会在
event
对象内部调用要处理的消息。如果您不使用Amazon MQ来运行RabbitMQ服务器,那么AWS Lambda将无法正常工作,您需要使用像EC2或ECS这样的服务来始终运行您的事件处理服务。