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

Commit

Permalink
Splits worker all-in-one function into pieces
Browse files Browse the repository at this point in the history
  • Loading branch information
alexindigo authored and goatslacker committed Jul 18, 2016
1 parent 3f7d0d7 commit 864fc99
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ const defaultConfig = {
limit: 1024 * 1000,
},
devMode: false,
endpoint: '/batch',
files: [],
logger: {},
plugins: [],
port: 8080,
endpoint: '/batch',
};

export default function hypernova(userConfig, onServer) {
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(config.endpoint, 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 864fc99

Please sign in to comment.