-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.ts
95 lines (86 loc) · 3.1 KB
/
index.ts
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
import * as shotPool from './lib/shot-pool';
import * as Logger from 'log4js';
import * as express from 'express';
import * as internalIp from 'internal-ip';
process.env.loglevel = process.env.loglevel || 'INFO';
process.env.webshotDebugPort = process.env.webshotDebugPort || '3030';
Logger.setGlobalLogLevel(process.env.loglevel);
let app = express();
let ip;
let _logger = Logger.getLogger("index");
internalIp.v4().then(_ip => ip = _ip);
export interface Config extends shotPool.PoolConfig {
webshotDebugPort?: string;
}
let defaultConfig: Config = {
concurrency: 10,
callbackName: '',
warmerUrl: '',
width: 800,
height: 600,
timeout: 60000,
webshotDebugPort: process.env.webshotDebugPort
};
let passedConfig : Config;
export async function init(options: Config) {
options = Object.assign({}, defaultConfig, options);
passedConfig = options;
await shotPool.create(
options
);
app.listen(parseInt(passedConfig.webshotDebugPort), ip);
_logger.info('Listening on debug port', passedConfig.webshotDebugPort, ip);
return shotPool;
}
export function getShot(url: string): PromiseLike<Buffer> {
return shotPool.getShot(url);
}
app.get('/status', (req, res) => {
let status = shotPool.getStatus();
let workerDetails = status.allWorkers.map(worker => {
let workerStatus = worker.getStatus();
let link = `http://${ip}:${workerStatus.debugPort}`;
return `
<div class="worker ${workerStatus.isBusy ? 'busy': 'free'}">
<div>Worker Id: <b>#${workerStatus.id}</b></div>
<div>ConnectWS: ${workerStatus.browser.wsEndpoint()}</div>
<div>DebugLink:
<a href='${link}'>
${link}
</a>
</div>
</div>
`;
}).join('');
res.end(`
<html>
<style>
.worker {
margin: 10px;
width: 700px;
padding: 10px;
border-radius: 5px;
box-shadow: 2px 2px #d3d3d8;
}
.busy {
background-color: #fff3d4;
}
.free {
background-color: #5fba7d;
}
</style>
<title>Webshot Factory Status</title>
<body>
<h1>Webshot Factory Status</h1>
<h3>Job Queue</h3>
<div>Number of Jobs in queue: ${status.jobQueue.getStatus().jobs.length}</div>
<div>Total Jobs processed: ${status.jobQueue.getStatus().total}</div>
<h3>Workers</h3>
<div>Total Workers: ${status.allWorkers.length}</div>
<div>Idle Workers: ${status.idleWorkers.length}</div>
<h4>Worker details</h4>
${workerDetails}
</body>
</html>
`)
});