INTRO
The n2o_pi module dedicated for creating and tracking supervised processes across all applications, using any ETS tables. Any supervised process in N2O is created using n2o_pi, such as: ring vnodes, timers, authorization, web page processes, test processes and other services. Loop process inside info/2 protocol handlers should spawn new async processes proc/2 in case of time consuming operations, because protocol handler is a critical path and it should be handled as soon as possible.
CALLBACK
The proc/2 is a callback that will be called on each gen_server's calls: handle_call, handle_cast and handle_info, its init and terminate. It returns either #ok as initial state of the process (which is the #pi{} too) or its response to gen_server:call/2 with new state included in #reply.
EXAMPLE
Here is literal implementation of N2O Timer which invalidates the caching table used for session variables.
Main purpose of n2o_pi is to create such processes from single proc/2 function and track pid in ETS table which is specified during process #pi{} initialization.
RECORDS
Each process is driven by its protocol which in fact a sum of protocol messages. Though n2o_pi as being generic don't limit the protocol messages, however it defines the type of process state, the #pi{} record.
According to N2O agreement each protocol message field should include [] in its type as default nil.
- name — process name, key in supervised chain.
- module — the module name where proc/2 is placed.
- table — ETS table name where cached pids are stored.
- sup — the application, where supervised processes will be created.
- state — the state of the running supervised process.
API
Spawns proc/2 function inside a process under supervision.
Kills the process and remove from supervision.
Tries to stop the process. On success it starts the new one, else return error.
Sends gen_call message to process, taken from Class table with Name key. Returns the response from gen_server:call.
Sends gen_cast message to process, taken from Class table with Name key.
Returns pid that was stored during process initialization in Class table with Name key.