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

Commit

Permalink
Merge pull request #24 from airbnb/cherry-pick1
Browse files Browse the repository at this point in the history
Merges parts of #21
  • Loading branch information
goatslacker authored Jul 18, 2016
2 parents e23fb41 + 864fc99 commit 6fd1f90
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ Options, and their defaults
// the port the app will start on
port: 8080,
// whether or not to run in parallel using all available cpus
endpoint: '/batch'
// default endpoint path
}
```

Expand Down
1 change: 1 addition & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const defaultConfig = {
limit: 1024 * 1000,
},
devMode: false,
endpoint: '/batch',
files: [],
logger: {},
plugins: [],
Expand Down
54 changes: 36 additions & 18 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@ import BatchManager from './utils/BatchManager';

let closing = false;

export default (app, config, onServer, workerId) => {
let server;

// ===== Middleware =========================================================
const attachMiddleware = (app, config) => {
app.use(bodyParser.json(config.bodyParser));
};

if (onServer) {
onServer(app, process);
}
const attachEndpoint = (app, config, callback) => {
app.post(config.endpoint, renderBatch(config, callback));
};

// ===== Routes =============================================================
app.post('/batch', renderBatch(config, () => closing));
const initServer = (app, config, callback) => {
let server;

// ===== Exceptions =========================================================
function exit(code) {
return () => process.exit(code);
}
Expand Down Expand Up @@ -93,14 +90,35 @@ export default (app, config, onServer, workerId) => {
// run through the initialize methods of any plugins that define them
runAppLifecycle('initialize', config.plugins, config)
.then(() => {
server = app.listen(config.port, () => {
if (process.send) {
// tell our coordinator that we're ready to start receiving requests
process.send({ workerId, ready: true });
}

logger.info('Connected', { port: config.port });
});
server = app.listen(config.port, callback);
})
.catch(shutDownSequence);
};

const worker = (app, config, onServer, workerId) => {
// ===== Middleware =========================================================
attachMiddleware(app, config);

if (onServer) {
onServer(app, process);
}

// ===== Routes =============================================================
attachEndpoint(app, config, () => closing);

// ===== initialize server's nuts and bolts =================================
initServer(app, config, () => {
if (process.send) {
// tell our coordinator that we're ready to start receiving requests
process.send({ workerId, ready: true });
}

logger.info('Connected', { port: config.port });
});
};

worker.attachMiddleware = attachMiddleware;
worker.attachEndpoint = attachEndpoint;
worker.initServer = initServer;

export default worker;

0 comments on commit 6fd1f90

Please sign in to comment.