loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

Database connect error during app boot should not terminate node process

Open clewisln opened this issue 3 years ago • 3 comments

Describe the bug

Steps to repro:

  1. Scaffold new app (lb4 app)
  2. Scaffold new mysql data source (lb4 datasource). Enter invalid connection info, so that the connection fails during boot.
  3. Build and run the app

Expected:

Server starts successfully, and /ping endpoint may be ran.

Actual:

Server fails to boot and the process terminates

Alternative with lazy connect:

If "lazy connect" is enabled on the data source config, and a manual datasource.connect() is made later on, the error will cause an unhandled exception to crash the node server. Be aware that trying to run datasource.execute() directly without a connected data source will return a promise that will never resolve until a connection is made.

Note that this occurs even if the call is wrapped in a try/catch. Further, normal crud repository fetchers, which internally queue the connect, are unaffected.

example:

{
   ...other datasource props,
   lazyConnect: true
}
@get('/test')
async test() {
  try {
    //execute will only queue, if not connected, causing an infinite wait
    if (!this.dataSource.connected)
      await this.dataSource.connect();

    //execute custom sql
    await this.dataSource.execute("select 1");
    return true;
  } catch(ex) {
    return false;
  }
}

Use Case:

Health check endpoints that check if parts of the application are online, including the database.

It is vital that the api continue to run, even if a dependency is unavailable. If a datasource.execute() or datasource.connect() fails inside a controller method, then it should not tear down the server with it!

Cli Version: 3.2.0

Logs

No response

Additional information

No response

Reproduction

repro provided above

clewisln avatar May 27 '22 19:05 clewisln

Upgraded CLI to 4.0, and the issue is still present.

Package versions: "@loopback/boot": "^5.0.0", "@loopback/core": "^4.0.0", "@loopback/repository": "^5.0.0", "@loopback/rest": "^12.0.0", "@loopback/rest-explorer": "^5.0.0", "@loopback/service-proxy": "^5.0.0", "loopback-connector-mysql": "^5.4.4", "tslib": "^2.0.0"

clewisln avatar May 31 '22 14:05 clewisln

Additional Information:

Sinking an error event handler on the data source emitter seems to suppress the unhandled error.

As to why, this is unclear. However, this explains why the repository functions do not crash the server, if you get past boot with lazy connect, as internally the connection queuing sinks this event temporarily.

Work around:

Forcing at least one error handler globally, allows the server to boot, when there is a mysql connection error. This occurs even if lazy mode is not configured.

This workaround subscribes the event in the setup function. Since setup is ran in the super, and subscribing the event after the super is ran is too late, the setup function is intercepted.

@lifeCycleObserver('datasource')
export class MainDataSource extends juggler.DataSource implements LifeCycleObserver {
  [... other code]

  //Setup interceptor, to sink a permanent error event handler
  setup(dsName: string, settings: object) {
    this.on("error", () => {}); //throw away. error will actually be caught in try/catch now.
    const pt = juggler.DataSource.prototype as any; //eslint-disable-line
    pt.setup.call(this, dsName, settings);
  }
}

It is not clear if this is a Node.JS problem in the VM, however, this is reproducible on the latest LTS, v16.15.0

clewisln avatar May 31 '22 15:05 clewisln

Hello,I come across a Database connection problem too,please listen to me.

I want to connect to the mongodb database by using loopback4,after I using lb4 datasource,

my generated datasource file as follow:

const config = {
  name: 'echo3Dcms',
  connector: 'mongodb',
  url: 'mongodb://localhost:27017/echo3Dcms',
  host: 'localhost',
  port: 27017,
  user: '',
  password: '',
  database: 'echo3Dcms',
  useNewUrlParser: true
};

Expect

I have created a database named echo3Dcms in mongoDB.

Actual

when I check the database list in MongoDB, there is no database named echo3Dcms, I don't know what should I do to solve this problem?

Have Try

I have run the lb4 datasouce twice,and I get the same problem.

I have read the document about Datasource , I don't know why the connector configurartion created by datasource, it can't connect the mongoDB,and create a database named echo3Dcms

please give me a direction.

Barnette-ao avatar Aug 09 '22 07:08 Barnette-ao