以前说明browserless 的hooks,实际上browserless还支持webhooks 也是一个很不错的功能,可以进行统计分析
而且webhooks 的地址都是可以通过环境变量配置的
参考使用
- docker 启动
内部处理
- webhooks 的定义
可以看到提供了不少,而且都是可配置的
import { Config, fetchTimeout, noop } from '@browserless.io/browserless';
import { EventEmitter } from 'events';
export class WebHooks extends EventEmitter {
constructor(protected config: Config) {
super();
}
protected callURL(url: string | null) {
if (url) {
return fetchTimeout(url, {
method: 'GET',
timeout: 10000,
}).catch(noop);
}
return;
}
public callFailedHealthURL() {
const url = this.config.getFailedHealthURL();
return this.callURL(url);
}
public callQueueAlertURL() {
const url = this.config.getQueueAlertURL();
return this.callURL(url);
}
public callRejectAlertURL() {
const url = this.config.getRejectAlertURL();
return this.callURL(url);
}
public callTimeoutAlertURL() {
const url = this.config.getTimeoutAlertURL();
return this.callURL(url);
}
public callErrorAlertURL(message: string) {
const url = this.config.getErrorAlertURL();
try {
const fullURL = new URL(url ?? '');
fullURL?.searchParams.set('error', message);
return this.callURL(fullURL.href);
} catch (err) {
return console.error(
`Issue calling error hook: "${err}". Did you set a working ERROR_ALERT_URL env variable?`,
);
}
}
/**
* Implement any browserless-core-specific shutdown logic here.
* Calls the empty-SDK stop method for downstream implementations.
*/
public shutdown = async () => {
await this.stop();
};
/**
* Left blank for downstream SDK modules to optionally implement.
*/
public stop = () => {};
}
内部使用
包含了browserless 以及limiter 部分,以下说明limiter 的处理, 包含了请求队列的参数
public limit = <TArgs extends unknown[], TResult>(
limitFn: LimitFn<TArgs, TResult>,
overCapacityFn: ErrorFn<TArgs>,
onTimeoutFn: ErrorFn<TArgs>,
timeoutOverrideFn: (...args: TArgs) => number | undefined,
): LimitFn<TArgs, unknown> => {
return (...args: TArgs) =>
new Promise(async (res, rej) => {
const timeout = timeoutOverrideFn(...args) ?? this.timeout;
this.logQueue(
`Adding to queue, max time allowed is ${timeout.toLocaleString()}ms`,
);
if (this.config.getHealthChecksEnabled()) {
const { cpuOverloaded, memoryOverloaded } =
await this.monitor.overloaded();
if (cpuOverloaded || memoryOverloaded) {
this.logQueue(`Health checks have failed, rejecting`);
this.webhooks.callFailedHealthURL();
this.metrics.addRejected();
overCapacityFn(...args);
return rej(new Error(`Health checks have failed, rejecting`));
}
}
if (!this.hasCapacity) {
this.logQueue(`Concurrency and queue is at capacity`);
this.webhooks.callRejectAlertURL();
this.metrics.addRejected();
overCapacityFn(...args);
return rej(
new TooManyRequests(`Concurrency and queue is at capacity`),
);
}
if (this.willQueue) {
this.logQueue(`Concurrency is at capacity, queueing`);
this.webhooks.callQueueAlertURL();
this.metrics.addQueued();
}
const bound: () => Promise<TResult | unknown> = async () => {
this.logQueue(`Starting new job`);
this.metrics.addRunning();
try {
const result = await limitFn(...args);
res(result);
return;
} catch (err) {
rej(err);
throw err;
}
};
const job: Job = Object.assign(bound, {
args,
onTimeoutFn: () => onTimeoutFn(...args),
start: Date.now(),
timeout,
});
this.push(job);
return bound;
});
};
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
说明
hooks+ webhooks 都是很不错的browserless 统计分析或者扩展地方,值得试用下
参考资料
src/webhooks.ts
src/browserless.ts
src/limiter.ts
https://docs.browserless.io/Docker/webhooks
https://github.com/browserless/browserless