Skip to content

Commit 467bef4

Browse files
committed
Add resubscribeInvoices call and refactorize code
- Async functions, like `resubscribeInvoices` cannot be called inside a synchronous function, as the former server loop.
1 parent 6b12357 commit 467bef4

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

app.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
require('dotenv').config();
22
const { start } = require('./bot');
33
const mongoConnect = require('./db_connect');
4+
const { resubscribeInvoices } = require('./ln');
45

5-
process.on('unhandledRejection', e => { console.log (e); });
6+
(async () => {
7+
process.on('unhandledRejection', e => { console.log (e); });
68

7-
process.on('uncaughtException', e => { console.log (e); });
9+
process.on('uncaughtException', e => { console.log (e); });
810

9-
mongoConnect();
10-
start(process.env.BOT_TOKEN);
11+
mongoose = mongoConnect();
12+
mongoose.connection
13+
.once('open', async () => {
14+
console.log('Connected to Mongo instance.');
15+
const bot = start(process.env.BOT_TOKEN);
16+
await resubscribeInvoices(bot);
17+
})
18+
.on('error', error => console.log('Error connecting to Mongo:', error))
19+
})();

bot/start.js

+4
Original file line numberDiff line numberDiff line change
@@ -722,9 +722,13 @@ const start = (botToken) => {
722722

723723
bot.launch();
724724

725+
console.log('Bot launched.');
726+
725727
// Enable graceful stop
726728
process.once('SIGINT', () => bot.stop('SIGINT'));
727729
process.once('SIGTERM', () => bot.stop('SIGTERM'));
730+
731+
return bot;
728732
};
729733

730734
module.exports = { initialize, start };

db_connect.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ const connect = () => {
1212
useNewUrlParser: true,
1313
useUnifiedTopology: true,
1414
});
15-
mongoose.connection
16-
.once('open', () => console.log('Connected to Mongo instance.'))
17-
.on('error', error => console.log('Error connecting to Mongo:', error));
15+
return mongoose
1816
};
1917

2018
module.exports = connect;

0 commit comments

Comments
 (0)