Skip to content
This repository was archived by the owner on Oct 9, 2023. It is now read-only.

Commit 6fd1f90

Browse files
authored
Merge pull request #24 from airbnb/cherry-pick1
Merges parts of #21
2 parents e23fb41 + 864fc99 commit 6fd1f90

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ Options, and their defaults
216216
// the port the app will start on
217217
port: 8080,
218218
// whether or not to run in parallel using all available cpus
219+
endpoint: '/batch'
220+
// default endpoint path
219221
}
220222
```
221223

src/server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const defaultConfig = {
1616
limit: 1024 * 1000,
1717
},
1818
devMode: false,
19+
endpoint: '/batch',
1920
files: [],
2021
logger: {},
2122
plugins: [],

src/worker.js

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,17 @@ import BatchManager from './utils/BatchManager';
77

88
let closing = false;
99

10-
export default (app, config, onServer, workerId) => {
11-
let server;
12-
13-
// ===== Middleware =========================================================
10+
const attachMiddleware = (app, config) => {
1411
app.use(bodyParser.json(config.bodyParser));
12+
};
1513

16-
if (onServer) {
17-
onServer(app, process);
18-
}
14+
const attachEndpoint = (app, config, callback) => {
15+
app.post(config.endpoint, renderBatch(config, callback));
16+
};
1917

20-
// ===== Routes =============================================================
21-
app.post('/batch', renderBatch(config, () => closing));
18+
const initServer = (app, config, callback) => {
19+
let server;
2220

23-
// ===== Exceptions =========================================================
2421
function exit(code) {
2522
return () => process.exit(code);
2623
}
@@ -93,14 +90,35 @@ export default (app, config, onServer, workerId) => {
9390
// run through the initialize methods of any plugins that define them
9491
runAppLifecycle('initialize', config.plugins, config)
9592
.then(() => {
96-
server = app.listen(config.port, () => {
97-
if (process.send) {
98-
// tell our coordinator that we're ready to start receiving requests
99-
process.send({ workerId, ready: true });
100-
}
101-
102-
logger.info('Connected', { port: config.port });
103-
});
93+
server = app.listen(config.port, callback);
10494
})
10595
.catch(shutDownSequence);
10696
};
97+
98+
const worker = (app, config, onServer, workerId) => {
99+
// ===== Middleware =========================================================
100+
attachMiddleware(app, config);
101+
102+
if (onServer) {
103+
onServer(app, process);
104+
}
105+
106+
// ===== Routes =============================================================
107+
attachEndpoint(app, config, () => closing);
108+
109+
// ===== initialize server's nuts and bolts =================================
110+
initServer(app, config, () => {
111+
if (process.send) {
112+
// tell our coordinator that we're ready to start receiving requests
113+
process.send({ workerId, ready: true });
114+
}
115+
116+
logger.info('Connected', { port: config.port });
117+
});
118+
};
119+
120+
worker.attachMiddleware = attachMiddleware;
121+
worker.attachEndpoint = attachEndpoint;
122+
worker.initServer = initServer;
123+
124+
export default worker;

0 commit comments

Comments
 (0)