npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hoplin/puppeteer-pool

v1.1.5

Published

Puppeteer Pool Manager for worker server, process daemon, commands etc...

Downloads

872

Readme

Puppeteer Pool Manager

NPM Version

Urgent Request! Please update to 1.1.4 or latest version ASAP

There was an critical error in Threshold Watcher Layer that the Process Tree was not recursively inspected. From version 1.1.4, the issue has been resolved. If you see this Readme, please update the package as soon as possible. Also if you're using custom puppeteer-pool-config.json after update package upperthan 1.1.4 version, please re configure your custom config file.

Are you facing issue?

If there's no other related issue that you are facing in this list, please add issue. Then I'll discuss with you as fast as I can

Yes. there are two default setting that maanger will use. If you want to change these default settings, please override these options.

executablePath: puppeteer.executablePath(),
headless: true,

Some servers may have trouble with puppeteer, Like session pool is not initializing or other similar cases. Follow these steps to resolve related issues.

  1. Install system dependencies. Puppeteer requires several dependencies to execute.
sudo apt-get install -y libnss3 libatk1.0-0 libx11-xcb1 libxcomposite1 libasound2 libgtk-3-0
  1. Install Chronium Browser
sudp apt-get install chromium-browser

which chronium-browser
  1. Attach executable path
await bootPoolManager({
  args: ['--no-sandbox', '--disable-gpu', '--disable-setuid-sandbox'],
  executablePath: 'Default is puppteeer built in chromium',
});

You need to install dependencies to start chromium

sudo apt-get install -y \
    gconf-service \
    libasound2 \
    libatk1.0-0 \
    libc6 \
    libcairo2 \
    libcups2 \
    libdbus-1-3 \
    libexpat1 \
    libfontconfig1 \
    libgcc1 \
    libgconf-2-4 \
    libgdk-pixbuf2.0-0 \
    libglib2.0-0 \
    libgtk-3-0 \
    libnspr4 \
    libpango-1.0-0 \
    libpangocairo-1.0-0 \
    libstdc++6 \
    libx11-6 \
    libx11-xcb1 \
    libxcb1 \
    libxcomposite1 \
    libxcursor1 \
    libxdamage1 \
    libxext6 \
    libxfixes3 \
    libxi6 \
    libxrandr2 \
    libxrender1 \
    libxss1 \
    libxtst6 \
    ca-certificates \
    fonts-liberation \
    libappindicator1 \
    libnss3 \
    lsb-release \
    xdg-utils \
    wget \
    libgbm-dev

Install packages

  • npm

    npm i @hoplin/puppeteer-pool
  • yarn

    yarn add @hoplin/puppeteer-pool
  • pnpm

    pnpm install @hoplin/puppeteer-pool

Go to

1. 🔧Pool Manager Config Docuemnt

2. 📖Pool Manager APIs Document

3. 😎Pool Manager Usage with Express.js Framework

Support

  • Pool Managing
    • Puppeteer Level Pooling
    • Session Level Pooling
  • Config
    • Support config customize
  • Threshold Watcher
    • CPU
    • Memory
    • Support safe pool instance reset in runtime
  • Metrics
    • Support Metric by pool
      • CPU usage of pool
      • Memory usage of pool
      • Managing session count in runtime
  • Graceful Shutdown

Puppeteer Pool Manager Config

Default config should be puppeteer-pool-config.json in root directory path.

Default config setting

If config file are not given or invalid path, manager will use default defined configurations. Or if you want to pass config path, you can pass path to bootPoolManager function as parameter.

{
  "browser_pool": {
    "min": 2,
    "max": 5
  },
  "session_pool": {
    "min": 5,
    "max": 10,
    "width": 1080,
    "height": 1024,
    "ignoreResourceLoad": false,
    "enablePageCache": false
  },
  "threshold": {
    "activate": true,
    "interval": 5,
    "cpu": {
      "break": 80,
      "warn": 45,
    },
    "memory": {
      "break": 2048,
      "warn": 800,
    }
  }
}

browser_pool

  • min: Minimum pool instance
  • max: Maximum pool instance
    • Range Validation
      • max should be larger or equal than min
      • min should be larger or equal than 1
      • Both min and max should be integer

session_pool

  • min: Minimum pool instance

  • max: Maximum pool instance

    • Range Validation
      • max should be larger or equal than min
      • min should be larger or equal than 1
      • Both min and max should be integer
      • Both min and max does not allow negative number
  • width: Browser width. Also set browser width and height if you need some acts like capturing screen or else.

    • Inteager Validation
      • width should be integer
      • width should be at least 50
      • width does not allow negative number
  • height: Browser height. Also set browser width and height if you need some acts like capturing screen or else.

    • Inteager Validation
      • height should be integer
      • height should be at least 50
      • height does not allow negative number
  • ignoreResourceLoad: Ignore resource load(This option makes ignore image, stylesheet, font assets request). If you set true, it will ignore resource load. This will increase performance but may occur some unintended behavior.

    • Boolean Validation
      • ignoreResourceLoad should be boolean
  • enablePageCache: Enable page cache. If you set true, it will cache page. This will increase performance but may occur some unintended behavior.

    • Boolean Validation
      • enablePageCache should be boolean

threshold

  • activate: Activate threshold or not
    • Boolean Validation
      • activate should be boolean
  • interval: Interval of checking threshold
  • cpu
    • break: CPU Usage break point. If CPU Usage is over this value, it will log status and reboot session manager puppeteer.
    • warn: CPU Usage warning point. If CPU Usage is over this value, it will log status.
      • Range Validation
        • break should be larger or equal than warn
        • warn should be larger or equal than 1
        • Both break and warn should be integer
        • Both break and warn does not allow negative number
  • memory
    • break: Memory Usage break point. If Memory Usage is over this value, it will log and reboot session manager puppeteer.
    • warn: Memory Usage warning point. If Memory Usage is over this value, it will log status.
      • Range Validation
        • break should be larger or equal than warn
        • warn should be larger or equal than 100
        • Both break and warn should be integer
        • Both break and warn does not allow negative number

Puppeteer Pool Manager APIs

bootPoolManager(puppeteerOptions,poolConfigPath):Promise<void>

Boot pool manager. You need to invoke this function at least once to use another APIs

Parameter

  • puppeteerOptions
    • PuppeteerLaunchOptions from puppeteer(Ref)
  • poolConfigPath
    • string
    • Custom Puppeteer Pool config path. Default is project root's puppeteer-pool-config.json

rebootPoolManager():Promise<void>

Reboot pool manager. This api is not recommended to use. Using this API in runtime may occur unintended session control context break

controlSession(cb):Promise<any>

Return single session from pool. You need to pass callback function as parameter to use in session.

About Callback Function

Manager will pass session, which is Page type of puppeteer to callback function. This API will return result of callback function.

Parameter

  • cb

    • sessionCallback

      // Session Callback type
      import { Page } from 'puppeteer';
      
      type sessionCallback<T> = (page: Page) => Promise<T>;

Return Value

  • Return callback's return value

getPoolMetrics():Promise<Array<PoolMetrics>>

Return pool metrics. This includes pool id, pool CPU Usage, Memory Usage

Both CPU, Memory values returned are the recursive detection of the process tree in each single pool.

Return Value

[
  {
    Id: "ID of pool"
    CPU: "Percentage of CPU that Pool is using (System)",
    Memory: "Memory Usage of Memory that Pool is using. Unit is MB (System)",
    SessionPoolCount: "Session count that Pool is managing"
  }
]

Usage Example

Example of combining pool manager with Express Framework

import {
  bootPoolManager,
  controlSession,
  getPoolMetrics,
} from '@hoplin/puppeteer-pool';
import express, { Application } from 'express';

async function bootstrap() {
  // Initialize pool
  await bootPoolManager({Puppeteer Launch Options},'Puppeteer Pool Config Path');

  const server: Application = express();

  // Control Session example
  server.post('/', async (req, res) => {
    const url = req.body.url;

    // Get single session from pool
    const controlResponse = await controlSession(async (session) => {
      /**
       * Control session here
       */
      return; //(Some values)
    });
    return res.status(200).json({ result: controlResponse });
  });

  server.get('/metrics',async(req,res,) => {
    let puppeteerPoolMetrics = await getPoolMetrics();
    puppeteerPoolMetrics = puppeteerPoolMetrics.map((metrics) => {
        const id = `POOL_${metrics.Id}`;
        const cpu = `${metrics.CPU}%`;
        const memory =
          metrics.Memory > 1024
            ? `${parseFloat((metrics.Memory / 1024).toFixed(2))}GB`
            : `${metrics.Memory}MB`;
        const sessionPoolCount = metrics.SessionPoolCount;
        return { id, cpu, memory, sessionPoolCount };
      })
    return res.status(200).json({ result: puppeteerPoolMetrics })
  })

  server.get('/', async (req, res) => {
    const puppeteerPoolMetrics = await getPoolMetrics();
    return res.status(200).json(puppeteerPoolMetrics);
  });

  server.listen(3000, () => {
    logger.info(`Server listening on port 3000`);
  });
}