Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ LOG_BAILEYS=error
# Set the maximum number of listeners that can be registered for an event
EVENT_EMITTER_MAX_LISTENERS=50

# Keep-alive - Periodically ping URLs to prevent PaaS services (Render, etc.) from sleeping.
# KEEP_ALIVE_URLS accepts a comma-separated list of external URLs to ping (e.g. your BotWave URL).
# SERVER_URL is always pinged automatically when keep-alive is enabled.
KEEP_ALIVE_ENABLED=false
KEEP_ALIVE_INTERVAL=10
KEEP_ALIVE_URLS=

# Determine how long the instance should be deleted from memory in case of no connection.
# Default time: 5 minutes
# If you don't even want an expiration, enter the value false
Expand Down
15 changes: 15 additions & 0 deletions src/config/env.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,12 @@ export type EventEmitter = {
MAX_LISTENERS: number;
};

export type KeepAlive = {
ENABLED: boolean;
INTERVAL: number;
URLS: string[];
};

export type Production = boolean;

export interface Env {
Expand Down Expand Up @@ -428,6 +434,7 @@ export interface Env {
FACEBOOK: Facebook;
SENTRY: Sentry;
EVENT_EMITTER: EventEmitter;
KEEP_ALIVE: KeepAlive;
PRODUCTION?: Production;
}

Expand Down Expand Up @@ -903,6 +910,14 @@ export class ConfigService {
EVENT_EMITTER: {
MAX_LISTENERS: Number.parseInt(process.env?.EVENT_EMITTER_MAX_LISTENERS) || 50,
},
KEEP_ALIVE: {
ENABLED: process.env?.KEEP_ALIVE_ENABLED === 'true',
INTERVAL: Number.parseInt(process.env?.KEEP_ALIVE_INTERVAL) || 10,
URLS:
process.env?.KEEP_ALIVE_URLS?.split(',')
.map((url) => url.trim())
.filter(Boolean) || [],
},
};
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
configService,
Cors,
HttpServer,
KeepAlive,
ProviderSession,
Sentry as SentryConfig,
Webhook,
Expand Down Expand Up @@ -159,6 +160,29 @@ async function bootstrap() {

server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT));

const keepAlive = configService.get<KeepAlive>('KEEP_ALIVE');
if (keepAlive.ENABLED) {
const intervalMs = keepAlive.INTERVAL * 60 * 1000;
const urls = [...keepAlive.URLS];

if (httpServer.URL) {
urls.unshift(httpServer.URL);
}

if (urls.length > 0) {
logger.info(`Keep-alive pinging ${urls.length} URL(s) every ${keepAlive.INTERVAL}m`);

setInterval(() => {
for (const url of urls) {
axios
.get(url, { timeout: 15000 })
.then(() => logger.verbose(`Keep-alive OK: ${url}`))
.catch((err) => logger.warn(`Keep-alive failed: ${url} - ${err.message}`));
}
}, intervalMs);
}
}

initWA().catch((error) => {
logger.error('Error loading instances: ' + error);
});
Expand Down