code hot reload
Node.js code hot reload can bring a lot of benifits, for example, you can hot update code in production, fix an emergency bug, change the logic of code. Especially when in a long connection service, restarting server will make users logout and then reconnect, it is bad for user experiences. However, by default, hot code reload is not supported in Node.js, because when doing hot reload, it is necessary to keep the reference of the objects, which may cause memory leak.
Bearcat provides a way for hot reload code, of course, there are some limits, not all codes updated will be hot reloaded.
Bearcat hot reload is based on bearcat’s powerful IoC container, to watch some events, when hot reload files changed, Bearcat will dynamically replace the updated POJO’s prototype functions. Therefore, because objects are shared with the same prototype object, when dynamically update the prototype object, all objects will be hot updated, without any influence to the objects’ private fields.
That is to say that what bearcat hot reload is actually the prototype functions, when you want to update a private field, it is not supported.
Enable hot reload
pass params to bearcat.createApp
- BEARCAT_HOT: setup ‘on’ to turn on bearcat hot code reload
- BEARCAT_HPATH: setup hot reload path(s), usually it is the scan source directory(app by default)
Bearcat will watch your application runtime source directory by default it is app, when it’is updated, bearcat will do hot reload for the updated files
Because bearcat updates the prototype, the updated files need to provide the updated bean’s id and func, to imply which bean need to be updated and the newest prototype function definitions.
when hot adding files, bearcat will add this file into bearcat IoC container
Add attribute dependency
suppose you want car with a wheel dependency, just add it, bearcat will hot add this dependency for all Car instances
listen to bearcat reload event, when watch codes changed, it will be fired
- To change the default watch directory, you can start your app with hpath arguments to specify the hot reload watch directory
Current verision of bearcat uses chokidar to implement watching directory, therefore, you can update all files in the watch directory
Avoid using gloal var in file, require by relative path when doing hot reload, because all of these are tightly coupled
Some copy actions like bind, concat will keep the reference, it will break the hot reload, for this concern you can listen reload event
- Loosely coupled system makes it easy to hot reload part of codes, Bearcat uses IoC to decouple the dependency of objects