参考事件概要
const eventbus = require('cf-eventbus');
eventbus.init({
bus: {
url: 'amqp://codefresh.dev', // rabbitmq server
reconnectInterval: 5
},
store: {
host: 'codefresh.dev', // postgres host
database: 'postgres', // postgres database
user: 'postgres', // postgres user
password: 'postgres' // postgres password
},
microServiceName: 'service-name' // client name
});
伟大的关闭
quit() 通过取消所有消费者,等待所有进行中的消费者完成,然后关闭连接,可以使用方法来优雅地终止总线连接.
不想要的话就用吧 quit({ force: true }) 立即终止连接.
const eventbus = require('cf-eventbus');
process.on('SIGTERM', async () => {
try {
await eventbus.quit();
// ...
} catch (err) {
// ...
}
});
可能需要在所有消费者取消后和连接关闭前进行一些额外的清理. 在这种情况下,使用下列模式:
const eventbus = require('cf-eventbus');
process.on('SIGTERM', async () => {
try {
await eventbus.cancelAllConsumers();
await eventbus.waitForConsumersToFinish();
// perform additional cleanup here, such as waiting for HTTP requests to finish
await eventbus.quit();
// ...
} catch (err) {
// ...
}
});
进行整合测试
通过Docker 编译、运行,在RabbitMQ和PostgreSQL上启动集成测试 test/integration.js 然后把容器关了 Docker (带 docker compose 插件)必须是在本地运行 .
yarn install
yarn test-integration