Lazy connect datasources on app start
In order to generate swagger spec, e.g. inside apic, we need to load the app. At the moment, all datasources open connection to databases at the start time and if there is any problem, the whole app crashes, even though we only wanted to query metadata. The proposal is to modify all connectors to correctly implement a lazy connect method and then modify juggler to allow the user to defer initialization of connections until later.
This issue is created to track the implementation and any feedback are welcomed and appreciated!
spike story: https://github.com/strongloop/loopback/issues/2176
Now when we run apic loopback:refresh, the connection error is not shown since the error check takes longer time than swagger generator finished. But it turns out that the swagger update still triggers connection.
As a solution:
[1]. Since the boot is called when we require('app_path'), I think we can add an env variable as a flag variable, which can be read in "loopback-boot".
[2]. In connectors, add a property lazyConnect in datasource config as shown in this pr in loopback-connector-mongodb
[3]. And in loopback-boot, set dsConfig.lazyConnect = process.env.LB_LAZYCONNECT as shown in this pr in loopback-boot. Then when the app boot calls connector initialize, connector will know if lazyConnect is needed.
CHECKLIST
- [x] Add flag in apiconnect-cli-loopback https://github.ibm.com/apimesh/apiconnect-cli-loopback/pull/60
- [x] Read flag in loopback-boot https://github.com/strongloop/loopback-boot/pull/185
Only trigger connect phase in connectors
- [x] db2 https://github.com/strongloop/loopback-connector-db2/pull/31
- [x] cloudant https://github.com/strongloop/loopback-connector-cloudant/pull/16
- [x] mysql https://github.com/strongloop/loopback-connector-mysql/pull/166
- [x] mongodb https://github.com/strongloop/loopback-connector-mongodb/pull/241
- [x] mssql https://github.com/strongloop/loopback-connector-mssql/pull/80
- [x] postgresql https://github.com/strongloop/loopback-connector-postgresql/pull/140
- [x] oracle https://github.com/strongloop/loopback-connector-oracle/pull/64
Will update verification steps here soon.
This feature are supported in nine repos:
- apiconnect-cli-loopback
- loopback-boot
- loopback-connector-db_name (db_name = [mysql, mongodb, db2, cloudant, oracle, mssql, postgresql])
To verify this feature, please make sure all repos are updated with the latest change.
Verify Steps: [1]. apic loopback [2]. Go to apiconnect-cli-loopback and open the code, comment this line: https://github.ibm.com/apimesh/apiconnect-cli-loopback/blob/master/lib/swaggerGenProc.js#L29
// process.exit(0);
[3]. apic create --type datasource
- create 7 datasources with invalid config
- example of invalid config:
"mysqlDS": {
"host": "invalid",
"port": 4,
"database": "invalid",
"password": "invalid",
"name": "mysqlDS",
"user": "invalid",
"connector": "mysql"
}
[4]. verify the generator exit without connection error when you create each datasource, and verify apic loopback:refresh exit without error after you create all datasource.
[5]. delete all invalid datasources in server/datasource.json
[6]. create 7 valid datasources
[7]. verify the same thing as step 4
@cgole This story is ready for verification, please assign it to people in qa-team to verify it, thank you.
This story is ready for verification, please assign it to people in qa-team to verify it, thank you.
@cgole ^^^
lol, Sorry I mentioned the wrong id. Corrected.
@ssh24 We need to force the process exit. But the boot functions are executed async so the swagger generator would exit before we see connection error.
So we have to comment process.exit(0) so if this feature has any bug, it's observable.
Verified.
@jannyHou How does one specify lazy connection for a datasource? Raymond said in Slack:
data sources can have
lazyConnectconfig property that defers connection
So, does that mean something like:
"myDB": {
"name": "myDB",
"connector": "mysql",
"host": "demo.strongloop.com",
"port": 3306,
"database": "demo",
"username": "demo",
"password": "L00pBack"
"lazyConnect" : true
}
?? Just a guess.
@crandmck If you want to defer the connection when booting the app with running node ., then the config you provided above will defer it for you.
And if you are going to generate swagger file by something like apic loopback:refresh, then you don't have to specify it in your config, the flag lazyConnect in apic is set true to defer the connection.
Added to http://loopback.io/doc/en/lb2/datasources.json.html
Hi guys, I'm following this example but i have a problem when I try to generate the swagger with apic loopback:refresh.
I think my problem has relations with the Lazy connections @jannyHou, because in the code base of the example, there is a boot script of automigrate with MySql connector. Then, When I try to generate the swagger file, apic set the "LB_LAZYCONNECT_DATASOURCES" enviroment variable, so the connection is skipped and when the boot script try automigrate and therefore drop the table, it crashed.
Another person had the same problem.
Error: The LoopBack application is not loaded. Cannot load the LoopBack application: Cannot read property 'escapeId' of undefined Please fix the problem and run
apic loopback:refresh
So, I think that if there isn't a connection to datasource, it isn't necessary to perform any action in the datasource to get the swagger file.
I'm going to comment about this problem in the repository of the loopback-connector-mysql with more information.
Thanks, have nice day. - Excuse for my english =D
@andrs1294 With the current design, boot scripts are executed when you refresh to generate the swagger file, so you can attach your model to a datasource but could not establish the connection. A work around would be splitting your model declaration and database interaction logic into two files, only enable the model declaration file when generates the swagger file.
@jannyHou Ok I understand you. But the example in the web page of the Loopback needs to be changed, because All the people who want to follow it, They are going to find with this problem.. Thanks for your time =) Have a nice day
Thanks @andrs1294
I'm a bit confused because the "Getting Started" example doesn't say to run apic refresh.
@jannyHou Can you please advise what should be changed in the docs?
@andrs1294 I looked into the example in your comment: http://loopback.io/doc/en/lb3/Introducing-the-Coffee-Shop-Reviews-app.html while it doesn't contain anything about lazyconnect, did you read any apic document that linked to that page? I am trying to figure out what document navigate you to the coffeeshop turotial, thanks.