pedroslopez / whatsapp-web.js

A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app
https://wwebjs.dev
Apache License 2.0
15.05k stars 3.58k forks source link

High CPU Usage Issue with whatsapp-web.js #2735

Closed amoyano closed 7 months ago

amoyano commented 7 months ago

Is there an existing issue for this?

Describe the bug

Whatsapp-web.js is running multiple Puppeteer processes with whatsapp-web.js results in extremely high CPU consumption. The issue is causing excessive resource utilization and negatively impacting the system's performance.

image

Expected behavior

Puppeteer processes should consume a reasonable amount of CPU resources, ensuring smooth and efficient execution of the application.

Shouldn't be only one processs?

Steps to Reproduce the Bug or Issue

  1. Start the application with the provided code.
  2. Observe the CPU usage while running multiple Puppeteer processes with whatsapp-web.js.

Relevant Code

I'm running singleton so there is only one instance

import { DataBase } from './database/Database'; import { PuppeteerManager } from './shared/PuppeteerManager'; import { WhatsappClient } from './shared/WhatsappClient'; import logger from './utils/logger';

export class Application { private static instance: Application | null = null; private dataBase = new DataBase(); private whatsappClient = new WhatsappClient(); private puppeteerManager = new PuppeteerManager(); private isInitialized = false;

 private constructor() {
   // Prevents multiple initializations
 }

 public static getInstance(): Application {

   if (Application.instance === null) {
     Application.instance = new Application();
     Application.instance.init(); // Calls initialization only when the instance is created
   }
   return Application.instance;
 }

 private init() {
   if (!this.isInitialized) {
     this.dbConnect(); // Calls dbConnect only once
     this.whatsappClient.initialize(); // Calls whatsappClient.init only once
     this.puppeteerManager.initialize(); // Calls initialize of PuppeteerManager only once
     this.isInitialized = true;
   }
 }

 private dbConnect(): void {
   this.dataBase.initConnect();
 }

 getPuppeteerManager(): PuppeteerManager {
   return this.puppeteerManager;
 }

}

function uncaughtErrorHandler() { process.on('uncaughtException', (error: Error) => { logger.error(Uncaught Exception: ${error.message}); });

 process.on('unhandledRejection', (reason: any) => {
   logger.error(`Unhandled Rejection: ${reason}`);
 });

}

uncaughtErrorHandler();

const appInstance = Application.getInstance();

Browser Type

Chromium

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

No, I am not using Multi Device

Environment

Linux Ubuntu 22 node 19.3.0 whatsappweb 1.21.1

Additional context

No response

alechkos commented 7 months ago

Optimize your code then

amoyano commented 7 months ago

@alechkos I'm running singleton in my end whatsappweb is generating thesemultiple proccess?