fix(startup): Added hooks for async startup

This commit is contained in:
Chris Troutner
2022-06-27 10:09:08 -07:00
parent 0333f77f5a
commit f117fe15df
9 changed files with 57 additions and 8 deletions
+2
View File
@@ -105,6 +105,8 @@ API documentation is written inline and generated by [apidoc](http://apidocjs.co
Visit `http://localhost:5000/docs/` to view docs Visit `http://localhost:5000/docs/` to view docs
There is additional developer documentation in the [dev-docs directory](./dev-docs).
## Dependencies ## Dependencies
- [koa2](https://github.com/koajs/koa/tree/v2.x) - [koa2](https://github.com/koajs/koa/tree/v2.x)
+6
View File
@@ -79,6 +79,12 @@ class Server {
// Dev Note: This line must come BEFORE controllers.attachRESTControllers() // Dev Note: This line must come BEFORE controllers.attachRESTControllers()
app.use(cors({ origin: '*' })) app.use(cors({ origin: '*' }))
// Wait for any adapters to initialize.
await this.controllers.initAdapters()
// Wait for any use-libraries to initialize.
await this.controllers.initUseCases()
// Attach REST API and JSON RPC controllers to the app. // Attach REST API and JSON RPC controllers to the app.
await this.controllers.attachRESTControllers(app) await this.controllers.attachRESTControllers(app)
+10
View File
@@ -0,0 +1,10 @@
# Developer Documentation
This directory needs to be fleshed out with more information. The markdown files in this directory are intended to provide additional documentation for software developers that use this boilerplate to run their own forks.
## Startup
This software requires an IPFS node in order to operate the JSON RPC and ipfs-coord features. There are two ways to incorporate an IPFS node:
- js-ipfs is integrated into this repository. If you simply run `npm start`, then this app will default to the js-ipfs IPFS node. However, js-ipfs is not as full featured as go-ipfs. It has memory leaks and lags considerably behind go-ipfs in terms of features.
- go-ipfs is the preferred way to run a node. It can be run externally by following the guidence on the [IPFS homepage](https://ipfs.io). Once the IPFS node is running, this app can take control of it. Use the [local-external-ipfs-node.sh](../shell-scripts/local-external-ipfs-node.sh) shell script to start this app and attach it to the go-ipfs node.
+6 -1
View File
@@ -46,7 +46,12 @@ class Adapters {
} }
// Start the IPFS node. // Start the IPFS node.
await this.ipfs.start() // Do not start these adapters if this is an e2e test.
if (this.config.env !== 'test') {
await this.ipfs.start()
}
console.log('Async Adapters have been started.')
return true return true
} catch (err) { } catch (err) {
+17 -6
View File
@@ -25,14 +25,14 @@ class Controllers {
this.useCases = new UseCases({ adapters: this.adapters }) this.useCases = new UseCases({ adapters: this.adapters })
} }
async attachControllers (app) { // Spin up any adapter libraries that have async startup needs.
// Wait for any startup processes to complete for the Adapters libraries. async initAdapters () {
await this.adapters.start() await this.adapters.start()
}
// Attach the REST controllers to the Koa app. // Run any Use Cases to startup the app.
// this.attachRESTControllers(app) async initUseCases () {
await this.useCases.start()
this.attachRPCControllers()
} }
// Top-level function for this library. // Top-level function for this library.
@@ -47,6 +47,17 @@ class Controllers {
restControllers.attachRESTControllers(app) restControllers.attachRESTControllers(app)
} }
// Attach any other controllers other than REST API controllers.
async attachControllers (app) {
// Wait for any startup processes to complete for the Adapters libraries.
// await this.adapters.start()
// Attach the REST controllers to the Koa app.
// this.attachRESTControllers(app)
this.attachRPCControllers()
}
// Add the JSON RPC router to the ipfs-coord adapter. // Add the JSON RPC router to the ipfs-coord adapter.
attachRPCControllers () { attachRPCControllers () {
const jsonRpcController = new JSONRPC({ const jsonRpcController = new JSONRPC({
+11
View File
@@ -18,6 +18,17 @@ class UseCases {
// console.log('use-cases/index.js localConfig: ', localConfig) // console.log('use-cases/index.js localConfig: ', localConfig)
this.user = new UserUseCases(localConfig) this.user = new UserUseCases(localConfig)
} }
// Run any startup Use Cases at the start of the app.
async start () {
try {
console.log('Async Use Cases have been started.')
} catch (err) {
console.error('Error in use-cases/index.js/startUseCases()')
console.log(err)
throw err
}
}
} }
module.exports = UseCases module.exports = UseCases
+1
View File
@@ -7,6 +7,7 @@
// Public npm libraries // Public npm libraries
const assert = require('chai').assert const assert = require('chai').assert
const axios = require('axios').default const axios = require('axios').default
// const sinon = require('sinon')
// Local support libraries // Local support libraries
const config = require('../../../config') const config = require('../../../config')
+2 -1
View File
@@ -37,8 +37,9 @@ describe('#adapters', () => {
it('should catch and throw an error', async () => { it('should catch and throw an error', async () => {
try { try {
// Force an error // Force an error
uut.config.getJwtAtStartup = false uut.config.getJwtAtStartup = false
uut.config.env = 'dev'
sandbox.stub(uut.ipfs, 'start').rejects(new Error('test error')) sandbox.stub(uut.ipfs, 'start').rejects(new Error('test error'))
await uut.start() await uut.start()
+2
View File
@@ -24,6 +24,8 @@ describe('#server', () => {
it('should start the server', async () => { it('should start the server', async () => {
// Mock dependencies // Mock dependencies
sandbox.stub(uut.mongoose, 'connect').resolves() sandbox.stub(uut.mongoose, 'connect').resolves()
sandbox.stub(uut.controllers, 'initAdapters').resolves()
sandbox.stub(uut.controllers, 'initUseCases').resolves()
sandbox.stub(uut.controllers, 'attachRESTControllers').resolves() sandbox.stub(uut.controllers, 'attachRESTControllers').resolves()
sandbox.stub(uut.adminLib, 'createSystemUser').resolves(true) sandbox.stub(uut.adminLib, 'createSystemUser').resolves(true)
sandbox.stub(uut.controllers, 'attachControllers').resolves() sandbox.stub(uut.controllers, 'attachControllers').resolves()