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.
module.exports = function initializer (next) { console.log('Hello world'); next();};Registering
asimov.preinit(myOverrider); // before the default chainasimov.init(myThing); // vanilla, the default bucketasimov.postinit(myDoneLogger); // after the default chainThree buckets is enough to cover the patterns you actually need:
- pre — I want to replace what the framework would normally do. Useful for swapping the default server, the default logger, etc.
- vanilla — I want to run alongside the rest. The default.
- post — I want the last word. Useful for “all set, ready to serve” log lines, warmups, etc.
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:
kill -HUP $(cat process.pid)