Skip to content

Commit 7316b83

Browse files
authored
Merge pull request #155 from broldak/queueSize
Add queueSize as option to CLI
2 parents ef764d6 + 3267b48 commit 7316b83

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

README.md

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Convert Highcharts.JS charts to static image files.
44

55
## What & Why
66

7-
This is a node.js application/service that converts [Highcharts.JS](http://highcharts.com) charts to static image files.
7+
This is a node.js application/service that converts [Highcharts.JS](http://highcharts.com) charts to static image files.
88
It supports PNG, JPEG, SVG, and PDF output; and the input can be either SVG, or JSON-formatted chart options.
99

1010
The application can be used either as a CLI (Command Line Interface), as an HTTP server, or as a node.js module.
@@ -22,7 +22,7 @@ The HTTP server can either be ran stand-alone and integrate with your other appl
2222
or it can be ran in such a way that the export buttons on your charts route to your own server.
2323

2424
To do latter, add:
25-
25+
2626
{
2727
exporting: {
2828
url: "<IP to the self-hosted export server>"
@@ -37,14 +37,14 @@ See [here](https://github.com/highcharts/node-export-server#using-as-a-nodejs-mo
3737

3838
## Install
3939

40-
First, make sure you have node.js installed. Go to [nodejs.org](https://nodejs.org/en/download/) and download/install node for your platform.
40+
First, make sure you have node.js installed. Go to [nodejs.org](https://nodejs.org/en/download/) and download/install node for your platform.
4141

4242
After node.js is installed, install the export server by opening a terminal and typing:
43-
43+
4444
npm install highcharts-export-server -g
4545

4646
OR:
47-
47+
4848
git clone https://github.com/highcharts/node-export-server
4949
npm install
5050
npm link
@@ -57,7 +57,7 @@ ln -s `which nodejs` /usr/bin/node
5757
```
5858

5959
## Running
60-
60+
6161
highcharts-export-server <arguments>
6262

6363
## Command Line Arguments
@@ -75,14 +75,15 @@ ln -s `which nodejs` /usr/bin/node
7575
* `--constr`: The constructor to use. Either `Chart`, `Map` (requires that the server was installed with maps support), or `StockChart`.
7676
* `--callback`: File containing JavaScript to call in the constructor of Highcharts.
7777
* `--resources`: Stringified JSON.
78-
* `--batch "input.json=output.png;input2.json=output2.png;.."`: Batch convert
78+
* `--batch "input.json=output.png;input2.json=output2.png;.."`: Batch convert
7979
* `--logDest <path>`: Set path for log files, and enable file logging
8080
* `--logFile <filename>`: Set the name of the log file (without the path). Defaults to `highcharts-export-server.log`. Note that `--logDest` also needs to be set to enable file logging.
8181
* `--logLevel <0..4>`: Set the log level. 0 = off, 1 = errors, 2 = warn, 3 = notice, 4 = verbose
8282
* `--fromFile "options.json"`: Read CLI options from JSON file
8383
* `--tmpdir`: The path to temporary output files.
8484
* `--workers`: Number of workers to spawn
8585
* `--workLimit`: the pieces of work that can be performed before restarting a phantom process
86+
* `--queueSize`: how many request can be stored in overflow count when there are not enough
8687
* `--listenToProcessExits`: set to 0 to skip attaching process.exit handlers. Note that disabling this may cause zombie processes!
8788
* `--globalOptions`: A JSON string with options to be passed to Highcharts.setOptions
8889

@@ -124,7 +125,7 @@ However, if you need to do this manually you can run `node build.js`.
124125

125126
### Using In Automated Deployments
126127

127-
If you're deploying an application/service that depend on the export server
128+
If you're deploying an application/service that depend on the export server
128129
as a node module, you can set the environment variable `ACCEPT_HIGHCHARTS_LICENSE` to `YES`
129130
on your server, and it will automatically agree to the licensing terms when running
130131
`npm install`. You can also use `HIGHCHARTS_VERSION` and `HIGHCHARTS_USE_STYLED`
@@ -145,11 +146,11 @@ depending on your setup, it may be possible to set the env variable in your `pac
145146
## Note about process.exit listeners
146147

147148
The export server attaches event listeners to process.exit. This is to
148-
make sure that all the phantom processes are properly killed off when the
149+
make sure that all the phantom processes are properly killed off when the
149150
application is terminated.
150151

151152
Listeners are also attached to uncaught exceptions - if one appears,
152-
the entire pool is killed, and the application terminated.
153+
the entire pool is killed, and the application terminated.
153154

154155
If you do not want this behavior, start the server with `--listenToProcessExits 0`.
155156

@@ -165,7 +166,7 @@ from which the cli tool was ran, it will use the `resources.json` file.
165166

166167
The server accepts the following arguments:
167168

168-
* `infile`: A string containing JSON or SVG for the chart
169+
* `infile`: A string containing JSON or SVG for the chart
169170
* `options`: Alias for `infile`
170171
* `svg`: A string containing SVG to render
171172
* `type`: The format: `png`, `jpeg`, `pdf`, `svg`. Mimetypes can also be used.
@@ -211,8 +212,8 @@ Note that the certificate files needs to be named as such:
211212

212213
### Worker Count & Work Limit
213214

214-
The export server utilizes a pool of *workers*, where one worker is a
215-
PhantomJS process responsible for converting charts. The pool size
215+
The export server utilizes a pool of *workers*, where one worker is a
216+
PhantomJS process responsible for converting charts. The pool size
216217
can be set with the `--workers` switch, and should be tweaked to fit the hardware
217218
on which you're running the server. It's recommended that you start with the default (8),
218219
and work your way up (or down if 8 is too many for your setup, and things are unstable) gradually. The `tests/http/stress-test.js` script can be used
@@ -222,7 +223,7 @@ server to be running on port 8081.
222223
PhantomJS becomes somewhat unstable the more export requests it has historically handled.
223224
To work around this, each of the workers has a maximum number of requests it can
224225
handle before it restarts itself. This number is 60 by default, and can be tweaked with
225-
`--workLimit`. As with `--workers`, this number should also be tweaked to fit your
226+
`--workLimit`. As with `--workers`, this number should also be tweaked to fit your
226227
use case.
227228

228229
### System Requirements
@@ -267,18 +268,18 @@ Download them, and follow the above instructions for your OS.
267268
## Server Test
268269

269270
Run the below in a terminal after running `highcharts-export-server --enableServer 1`.
270-
271-
# Generate a chart and save it to mychart.png
271+
272+
# Generate a chart and save it to mychart.png
272273
curl -H "Content-Type: application/json" -X POST -d '{"infile":{"title": {"text": "Steep Chart"}, "xAxis": {"categories": ["Jan", "Feb", "Mar"]}, "series": [{"data": [29.9, 71.5, 106.4]}]}}' 127.0.0.1:7801 -o mychart.png
273274

274275
## Using as a Node.js Module
275276

276277
The export server can also be used as a node module to simplify integrations:
277-
278+
278279
//Include the exporter module
279280
const exporter = require('highcharts-export-server');
280281

281-
//Export settings
282+
//Export settings
282283
var exportSettings = {
283284
type: 'png',
284285
options: {
@@ -331,7 +332,7 @@ The export server can also be used as a node module to simplify integrations:
331332
* `enableFileLogging(path, name)`: enable logging to file. `path` is the path to log to, `name` is the filename to log to
332333
* `export(exportOptions, fn)`: do an export. `exportOptions` uses the same attribute names as the CLI switch names. `fn` is called when the export is completed, with an object as the second argument containing the the filename attribute.
333334
* `startServer(port, sslPort, sslPath)`: start an http server on the given port. `sslPath` is the path to the server key/certificate (must be named server.key/server.crt)
334-
* `server` - the server instance
335+
* `server` - the server instance
335336
* `enableRateLimiting(options)` - enable rate limiting on the POST path
336337
* `max` - the maximum amount of requests before rate limiting kicks in
337338
* `window` - the time window in minutes for rate limiting. Example: setting `window` to `1` and `max` to `30` will allow a maximum of 30 requests within one minute.
@@ -342,16 +343,17 @@ The export server can also be used as a node module to simplify integrations:
342343
* `express()` - return the express module instance
343344
* `useFilter(when, fn)` - attach a filter to the POST route. Returning false in the callback will terminate the request.
344345
* `when` - either `beforeRequest` or `afterRequest`
345-
* `fn` - the function to call
346+
* `fn` - the function to call
346347
* `req` - the request object
347348
* `res` - the result object
348349
* `data` - the request data
349350
* `id` - the request ID
350-
* `uniqueid` - the unique id for the request (used for temporary file names)
351+
* `uniqueid` - the unique id for the request (used for temporary file names)
351352
* `initPool(config)`: init the phantom pool - must be done prior to exporting. `config` is an object as such:
352353
* `maxWorkers` (default 25) - max count of worker processes
353354
* `initialWorkers` (default 5) - initial worker process count
354355
* `workLimit` (default 50) - how many task can be performed by a worker process before it's automatically restarted
356+
* `queueSize` (default 5) - how many request can be stored in overflow count when there are not enough workers to handle all requests
355357
* `killPool()`: kill the phantom processes
356358

357359
## Using Ajax in Injected Resources
@@ -360,16 +362,16 @@ If you need to perform Ajax requests inside one of the resource scripts,
360362
set `asyncRendering` to true, and call `highexp.done()` in the Ajax return to process the chart.
361363

362364
Example:
363-
365+
364366
{
365367
asyncRendering: true,
366368
resources: {
367-
files: 'myAjaxScript.js'
369+
files: 'myAjaxScript.js'
368370
}
369371
}
370372

371373
myAjaxScript.js:
372-
374+
373375
jQuery.ajax({
374376
url: 'example.com',
375377
success: function (data) {
@@ -381,16 +383,16 @@ myAjaxScript.js:
381383
}
382384
});
383385

384-
If the Ajax call doesn't call `highexp.done()` within 60 seconds, the
386+
If the Ajax call doesn't call `highexp.done()` within 60 seconds, the
385387
rendering will time out.
386388

387389
## Performance Notice
388390

389391
In cases of batch exports, it's faster to use the HTTP server than the CLI.
390-
This is due to the overhead of starting PhantomJS for each job when using the CLI.
392+
This is due to the overhead of starting PhantomJS for each job when using the CLI.
391393

392-
As a concrete example, running the CLI with [testcharts/basic.json](testcharts/basic.json)
393-
as the input and converting to PNG averages about 449ms.
394+
As a concrete example, running the CLI with [testcharts/basic.json](testcharts/basic.json)
395+
as the input and converting to PNG averages about 449ms.
394396
Posting the same configuration to the HTTP server averages less than 100ms.
395397

396398
So it's better to write a bash script that starts the server and then
@@ -399,7 +401,7 @@ export server as a service.
399401

400402
Alternatively, you can use the `--batch` switch if the output format is the same
401403
for each of the input files to process:
402-
404+
403405
highcharts-export-server --batch "infile1.json=outfile1.png;infile2.json=outfile2.png;.."
404406

405407
Other switches can be combined with this switch.

bin/cli.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ addOption('skipToken', false, '<number|string>: Option to be passed as an argume
9292
addOption('logLevel', 2, '<number>: the log level. 0 = silent, 4 = verbose.');
9393
addOption('workers', false, '<number>: the number of workers to spawn');
9494
addOption('workLimit', 60, '<number>: the pieces of work that can be performed before restarting a phantom process');
95+
addOption('queueSize', 5, '<number>: the size of the request overfow queue');
9596

9697
addOption('logDest', false, '<string>: path to log files. will also enable file logging.');
9798
addOption('logFile', 'highcharts-export-server.log', '<string>: filename to log to.');
@@ -193,7 +194,8 @@ if (options.enableServer || (options.host && options.host.length)) {
193194
listenToProcessExits: options.listenToProcessExits,
194195
initialWorkers: options.workers || 0,
195196
maxWorkers: options.workers || 4,
196-
workLimit: options.workLimit
197+
workLimit: options.workLimit,
198+
queueSize: options.queueSize
197199
});
198200

199201
if (options.rateLimit && options.rateLimit !== 0 && options.rateLimit !== false) {
@@ -235,6 +237,7 @@ if (options.enableServer || (options.host && options.host.length)) {
235237
initialWorkers: options.workers || 5,
236238
maxWorkers: options.workers || 25,
237239
workLimit: options.workLimit,
240+
queueSize: options.queueSize,
238241
reaper: false
239242
});
240243

@@ -279,6 +282,7 @@ if (options.enableServer || (options.host && options.host.length)) {
279282
listenToProcessExits: options.listenToProcessExits,
280283
initialWorkers: options.workers || 1,
281284
maxWorkers: options.workers || 1 ,
285+
queueSize: options.queueSize,
282286
reaper: false
283287
});
284288

0 commit comments

Comments
 (0)