Skip to content
Open
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
28 changes: 22 additions & 6 deletions packages/provider-sherpa/src/sherpa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class SherpaProvider extends ProviderClass<WASocket> {
private healthCheckInterval?: ReturnType<typeof setInterval>
private presenceInterval?: ReturnType<typeof setTimeout>
private periodicCleanupInterval?: ReturnType<typeof setInterval>
private reconnectTimer?: ReturnType<typeof setTimeout>
private releaseTmpTimer?: NodeJS.Timeout
private badSessionCount = 0

msgRetryCounterCache?: NodeCache
Expand Down Expand Up @@ -183,6 +185,14 @@ class SherpaProvider extends ProviderClass<WASocket> {
private cleanup() {
try {
this.stopHealthCheck()
if (this.reconnectTimer) {
clearTimeout(this.reconnectTimer)
this.reconnectTimer = undefined
}
if (this.releaseTmpTimer) {
clearInterval(this.releaseTmpTimer)
this.releaseTmpTimer = undefined
}
if (this.periodicCleanupInterval) {
clearInterval(this.periodicCleanupInterval)
this.periodicCleanupInterval = undefined
Expand All @@ -200,11 +210,12 @@ class SherpaProvider extends ProviderClass<WASocket> {
this.mapSet.clear()
this.idsDuplicates.length = 0

// Log before closing the stream
this.logger.log(`[${new Date().toISOString()}] Recursos limpiados correctamente`)

if (this.logStream && typeof this.logStream.end === 'function') {
this.logStream.end()
}

this.logger.log(`[${new Date().toISOString()}] Recursos limpiados correctamente`)
} catch (error) {
console.error('Error durante cleanup:', error)
}
Expand Down Expand Up @@ -271,12 +282,16 @@ class SherpaProvider extends ProviderClass<WASocket> {
try {
if (this.globalVendorArgs.useBaileysStore) {
if (this.globalVendorArgs.timeRelease > 0) {
await releaseTmp(NAME_DIR_SESSION, this.globalVendorArgs.timeRelease)
// Clear previous releaseTmp interval to prevent accumulation on reconnects
if (this.releaseTmpTimer) {
clearInterval(this.releaseTmpTimer)
this.releaseTmpTimer = undefined
}
this.releaseTmpTimer = await releaseTmp(NAME_DIR_SESSION, this.globalVendorArgs.timeRelease)
}
}
} catch (e) {
this.logger.log(e)
this.initVendor().then((v) => this.listenOnEvents(v))
this.logger.log(`[${new Date().toISOString()}] releaseTmp error, continuing without cleanup:`, e)
}

try {
Expand Down Expand Up @@ -1259,7 +1274,8 @@ class SherpaProvider extends ProviderClass<WASocket> {
} in ${delay}ms`
)

setTimeout(() => {
this.reconnectTimer = setTimeout(() => {
this.reconnectTimer = undefined
this.initVendor()
.then((v) => this.listenOnEvents(v))
.catch((error) => {
Expand Down
Loading