Skip to content

Commit

Permalink
Merge pull request #19 from J-Hoplin/fix/fix-2.0.8
Browse files Browse the repository at this point in the history
2.0.8-beta
  • Loading branch information
J-Hoplin authored Feb 8, 2025
2 parents e1300ea + f908a13 commit d068905
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 44 deletions.
109 changes: 80 additions & 29 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,55 @@
```
pnpm install puppeteer @hoplin/puppeteer-pool
```
<details>
<summary><b>Patch Note</b></summary>
<div markdown="1">

### Release 2.0.8
## Release 2.0.8(and it's sub versions)

- `PuppeteerPool.start` required parameter as config type

```typescript
type PuppeteerPoolStartOptions = {
/**
* Number of concurrency,
* Default is 3
*/
concurrencyLevel: number;
/**
* Context mode
* Default is ContextMode.SHARED
*/
contextMode: ContextMode;
/**
* Puppeteer launch options
* Default is {}
*/
options?: puppeteer.LaunchOptions;
/**
* Custom config path
*/
customConfigPath?: string;
/**
* Enable log
* Default is true
*/
enableLog?: boolean;
/**
* Log level
* Default is LogLevel.DEBUG
*/
logLevel?: LogLevel;
};
```

- Remove pino logger dependency and implement custom logger
- You can config `log level` and `enable log` in `PuppeteerPool.start` function
- Enhanced Concurrency Control

### Next Features in 2.0.9
## Next Features in 2.0.9

- Detailed Metrics Monitoring
- Monitor metrics by context
- Support to use Playwright instead of Puppeteer

</div>
</details>

## Fully changed from 2.0.0

The internal implementation is event-based, which significantly improves the stability. In addition, instead of relying
Expand All @@ -58,32 +88,50 @@ After that you can use dispatcher to control pool manager.
- PuppeteePool
- `PuppeteerPool` is singleton class. You can use `PuppeteerPool.start` to initialize pool manager.
- PuppeteerPool.start

- Static Method
- Description: Initialize pool manager. You need to call this function to start puppeteer pool. Even if you invoke
this function multiple times with differenct arguments, it will return the first initialized instance.
- Args
- concurrencyLevel
- Required
- number
- contextMode: ContextMode
- Required
- `ContextMode.ISOLATED` | `ContextMode.SHARED`
- enableLog: boolean
- Optional
- Default: `true`
- logLevel: LogLevel
- Optional
- Default: `LogLevel.DEBUG`
- options
- Optional
- [puppeteer.LaunchOptions](https://pptr.dev/api/puppeteer.launchoptions)
- Default: `{}`
- customConfigPath
- Optional
- string (Default: `puppeteer-pool-config.json` in project root)
- Args: `PuppeteerPoolStartOptions`

```typescript
type PuppeteerPoolStartOptions = {
/**
* Number of concurrency,
* Default is 3
*/
concurrencyLevel: number;
/**
* Context mode
* Default is ContextMode.SHARED
*/
contextMode: ContextMode;
/**
* Puppeteer launch options
* Default is {}
*/
options?: puppeteer.LaunchOptions;
/**
* Custom config path
*/
customConfigPath?: string;
/**
* Enable log
* Default is true
*/
enableLog?: boolean;
/**
* Log level
* Default is LogLevel.DEBUG
*/
logLevel?: LogLevel;
};
```

- Return
- `Promise<PuppeteerPool>`
- Returns PuppeteerPool Instance.

- Instance<PuppeteerPool>.stop
- Description: Stop pool manager. It will close all sessions and terminate pool manager.
- Return
Expand Down Expand Up @@ -114,7 +162,10 @@ After that you can use dispatcher to control pool manager.
import { ContextMode, PuppeteerPool } from '@hoplin/puppeteer-pool';
async function main() {
const poolInstance = await PuppeteerPool.start(2, ContextMode.ISOLATED);
const poolInstance = await PuppeteerPool.start({
concurrencyLevel: 2,
contextMode: ContextMode.ISOLATED,
});
const urls = [
'https://www.google.com',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hoplin/puppeteer-pool",
"version": "2.0.8",
"version": "2.0.8-beta",
"main": "dist/index.js",
"description": "Puppeteer Pool Manager for worker server, process daemon, commands etc...",
"repository": "https://github.com/J-Hoplin/Puppeteer-Pool.git",
Expand Down
57 changes: 43 additions & 14 deletions src/client/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ import { RequestedTask } from '../types';
import * as puppeteer from 'puppeteer';
import { LogLevel } from '../logger';

export type PuppeteerPoolStartOptions = {
/**
* Number of concurrency
*/
concurrencyLevel: number;
/**
* Context mode
*/
contextMode: ContextMode;
/**
* Puppeteer launch options
*/
options?: puppeteer.LaunchOptions;
/**
* Custom config path
*/
customConfigPath?: string;
/**
* Enable log
*/
enableLog?: boolean;
/**
* Log level
*/
logLevel?: LogLevel;
};

export class PuppeteerPool {
private static isInitialized = false;
private static dispatcherInstance: TaskDispatcher;
Expand All @@ -31,24 +58,26 @@ export class PuppeteerPool {
/**
* Invoke this function to start a new Puppeteer Pool
*/
public static async start(
concurrencyLevel: number,
contextMode: ContextMode,
enableLog: boolean = true,
logLevel: LogLevel = LogLevel.DEBUG,
options?: puppeteer.LaunchOptions,
customConfigPath?: string,
) {
public static async start(options: PuppeteerPoolStartOptions) {
const startOptions: PuppeteerPoolStartOptions = {
concurrencyLevel: 3,
contextMode: ContextMode.SHARED,
options: {},
enableLog: true,
logLevel: LogLevel.DEBUG,
...options,
};

if (!PuppeteerPool.isInitialized) {
// Initialize Task Dispatcher
PuppeteerPool.dispatcherInstance = new TaskDispatcher();
await PuppeteerPool.dispatcherInstance.init(
concurrencyLevel,
contextMode,
enableLog,
logLevel,
options,
customConfigPath,
startOptions.concurrencyLevel,
startOptions.contextMode,
startOptions.enableLog,
startOptions.logLevel,
startOptions.options,
startOptions.customConfigPath,
);
// Initialize REST Client Instance
PuppeteerPool.instance = new PuppeteerPool();
Expand Down

0 comments on commit d068905

Please sign in to comment.