Initializers

Initializers are the asimov.js way to add work to the boot process. They run in sequence, each one calling next() to hand off to the next, and they can be registered in three buckets: pre, vanilla, and post.

Writing an initializer

An initializer is a function that takes a next callback. It can be sync or async — the only contract is that you must eventually call next() to continue, or not call it if you want to break the chain.

lib/init/logMessage.js
module.exports = function initializer (next) {
console.log('Hello world');
next();
};

Registering

asimov.preinit(myOverrider); // before the default chain
asimov.init(myThing); // vanilla, the default bucket
asimov.postinit(myDoneLogger); // after the default chain

Three buckets is enough to cover the patterns you actually need:

Breaking the chain

If your initializer does not call next(), the rest of the chain doesn’t run. This is intentional: it’s how asimov-server can be used as both a cluster master and a worker — the master initializer forks workers and does not call next, ending the master’s boot at that point.

Sequences

Under the hood, every bucket of initializers is a Sequencer. You can add your own named sequences and let other plugins hook in:

asimov.addSequence('chain');
asimov.chain(myChainFactory());
asimov.runSequence('chain')
.done(function () { console.log('yay! done.'); })
.fail(function (err) { console.log('ooops', err); });

This is the same primitive asimov-pages uses to implement render pipelines.

Graceful shutdown

Register cleanup functions with asimov.shutdown(). They run on SIGTERM / SIGINT, and follow the same chain semantics as initializers:

asimov.shutdown(function (next) {
pool.drain().then(next);
});

Rolling restarts

Send SIGHUP to the master process to reload all code without dropping in-flight requests. The master’s PID is written to process.pid in your project root:

Terminal window
kill -HUP $(cat process.pid)