A lightweight and pure service startup architecture. It provides the most direct access mode through a stable process lifecycle model. You can use it to build a variety of service frameworks.
It has the following features:
- Life cycle
- Process fault tolerance
- IPC communication protocol
- Dynamic Agent Process
- Cluster process support
It is written in TYPESCRIPT and provides a good coding experience. At the same time, it can be used to guard services through any form of process hosting, such as pm2, forever.
For the latest stable version:
$ npm install @typeservice/coreFor our nightly builds:
$ npm install @typeservice/core@nextimport*aspathfrom'path';import*ashttpfrom'http';import{WorkerFactory,IPCException,resolve}from'@typeservice/core';classHttpFrameworkerextendsWorkerFactory{privatereadonlyserver: http.Server;constructor(){super();this.on('setup',()=>newPromise(resolve=>{constserver=http.createServer((req,res)=>res.end('ok'));Object.defineProperty(this,'server',{value: server});console.log('start at 8080');server.listen(8080,resolve);}));this.on('exit',async()=>this.server.close());}}constframeworker=newHttpFrameworker();frameworker.listen()// use ipc communication protocol// get abc method result from master by args: { a: 1, b: 2 }.then(()=>frameworker.messager.invoke('master','abc',{a: 1,b: 2})).then(data=>console.log('result:',data)).catch((e: IPCException)=>console.error(e.message))// create a new agent dynamicly.then(()=>frameworker.messager.create('abc',path.resolve(__dirname,'../agent/abc'),678)).then(()=>newPromise(resolve=>setTimeout(resolve,5000))).then(()=>console.log('start invoke abc.test')).then(()=>frameworker.messager.invoke('abc','test',666,3000)).then((data)=>console.log('end invoke abc.test',data)).catch(e=>console.error(e))You can create a secure service by inheriting from the WorkerFactory.
classHttpFrameworkerextendsWorkerFactory{}It has 3 lifecycle events:
- setup Process initial load event
- exit Process shutdown event
- error Process unexpected error event
Here, we don't know how to use the worker to create a service architecture. You can use the @typeservice/http project to find out how to create it.
Maybe this is our focus. The dynamic agent process associates it with the worker service through the IPC communication protocol to form a communication closed loop based on the primary and secondary processes. It provides a series of annotations to help us build communication channels.
The notes are as follows:
@methodtag it is an IPC communication method@initializeinitialization function@terminateprocess end function@errorerror handler (uncaught error or unexpected error)@middlewaremiddleware
These methods are sufficient for us to use to form plugins to provide various functions, such as setting up task scheduling.
Here is a simple example:
import{AgentFactory,ipc,AgentContext}from'@typeservice/core';exportdefaultclassABCAgentextendsAgentFactory{privatereadonly_max: number;constructor(max: number){super();this._max=max;}
@ipc.method
@ipc.middleware<AgentContext>(async(ctx,next)=>{console.log('ctx',ctx);awaitnext();})asynctest(ctx: AgentContext){returnctx.data+this._max;}
@ipc.initializesetup(){console.log('agent init')}
@ipc.terminateexit(){console.log('agent exit')}}Copyright (c) 2019-present, yunjie (Evio) shen
