Make stop work

This commit is contained in:
allburov committed 2022-08-22 11:45:16 +03:00
1 parent 23fb543776
commit 6f2cf83425
2 files changed
+20 -10

No files matched your search

+2 -3
View File
@@ -13,14 +13,13 @@ export class SessionsController {
@Post('/start/')
async start(@Body() request: SessionRequest) {
// Do not wait it, because we get venom instance only after scanning QR code
this.whatsappSessionManager.startSession(request.sessionName)
return
}
@Post('/stop/')
async stop(@Body() request: SessionRequest) {
this.whatsappSessionManager.stopSession(request.sessionName)
return
await this.whatsappSessionManager.stopSession(request.sessionName)
}
@Get('/')
+18 -7
View File
@@ -34,7 +34,7 @@ enum WhatsappStatus {
}
export class WhatsappService {
private status: WhatsappStatus;
public status: WhatsappStatus;
private qrCodeBase64: string;
readonly filesFolder: string
readonly mimetypes: string[] | null
@@ -42,7 +42,7 @@ export class WhatsappService {
private RETRY_DELAY = 15
private RETRY_ATTEMPTS = 3;
private log: ConsoleLogger;
private whatsapp: Whatsapp;
public whatsapp: Whatsapp;
constructor(
private config: WhatsappConfigService,
@@ -158,6 +158,9 @@ export class WhatsappService {
}), this.filesLifetime)
}
/**
* Get venom instance if it's working (with no QR code required)
*/
public getWhatsapp() {
if (this.status != WhatsappStatus.WORKING) {
throw new UnprocessableEntityException(
@@ -233,18 +236,26 @@ export class WhatsappSessionManager implements OnApplicationShutdown {
return service.getWhatsapp()
}
stopSession(name: string) {
async stopSession(name: string) {
this.log.log(`Stopping ${name} session...`)
const service = this.getService(name)
await service.getWhatsapp().close()
this.log.log(`"${name}" has been stopped.`)
delete this.sessions[name]
}
getAllSessions() {
return Object.keys(this.sessions) as Array<string>
const result = Object.values(this.sessions).map((session) => {
return {name: session.name, status: session.status}
})
return result
}
onApplicationShutdown(signal ?: string): any {
this.log.log('Close a browser...')
// TODO: Stop all sessions
async onApplicationShutdown(signal ?: string) {
this.log.log('Stop all sessions...')
for (const sessionName of Object.keys(this.sessions)) {
await this.stopSession(sessionName)
}
}
private cleanDownloadsFolder(filesFolder) {