share-research / SHARE

0 stars 0 forks source link

Research spike to understand node-red engine code #21

Closed h-parekh closed 5 years ago

h-parekh commented 5 years ago

What

We'd like to use node-red's core engine code to run the core engine function for running workflows

Why

  1. So we can run it in a distributed network
  2. So we don't need to start up a node-red server for executing flows

How

A basic explanation will suffice for now, until we draw action items based on the analysis.

h-parekh commented 5 years ago

This analysis is based on node-red v0.19.5.

node-red's core engine is in the runtime code

This stack trace shows the execution path of initializing its runtime before the nodes get initialized. I took this by adding a console.trace("In runtime.init") in the runtime's init function

share-red@h-parekh hp_whats_node_red % node red.js
Trace: In runtime.init
    at Object.init (/Users/hparekh/git/share-red@h-parekh/red/runtime/index.js:63:13)
    at Object.init (/Users/hparekh/git/share-red@h-parekh/red/red.js:69:21)
    at Object.<anonymous> (/Users/hparekh/git/share-red@h-parekh/red.js:190:9)

By this time all the most settings have been initialized and the engine moves on to loading the nodes and flows into this runtime. See node initialization method here.

Each runtime instance of node-red is running with an expressjs server, which manages all the routing during the flow execution. Without going into the depths of how expressjs works, my understanding is that we cannot execute a flow without initializing this express server.

With a newer version of node-red 0.20.0 scheduled for final release in Jan'2019, we will be able to leverage a more distributed and lightweight ending.

See https://github.com/share-research/SHARE/issues/20 for details.