loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Custom script doesn't seem to affect the ChangeStream

Open GrandmasterGrogu opened this issue 10 years ago • 6 comments

I have a standalone custom script that I run while the loopback server is running, where I do a find and an updateAll to a model, but it does not send the change to the liveset / changestream.

I'm accessing the models, etc, following this example: https://docs.strongloop.com/display/public/LB/Working+with+LoopBack+objects#WorkingwithLoopBackobjects-Fromacustomscript Originally, I posted it to angular-live-set, but realized it really belongs where the changestream code is. https://github.com/strongloop/angular-live-set/issues/18

var server = require("../../server/server");
server.models.Mymodel.find(...
server.models.Mymodel.updateAll(...

// Then, it does not show up in the ChangeStream, and consequently the LiveSet. However, the loopback explorer does affect the changestream and its changes show.



GrandmasterGrogu avatar Mar 11 '16 23:03 GrandmasterGrogu

Hey @alexxgathp, could you fork our sandbox and reproduce the issue then post a link in here?

Model operations should affect the change steam no matter where they are taking place. Once your sandbox is up, I'll dive in and see if I can find the source of the issue.

richardpringle avatar Mar 14 '16 11:03 richardpringle

Okay, here it is: https://github.com/alexxgathp/loopback-sandbox The custom script is under "customscripts". I added instructions of the steps I take to run it too.

GrandmasterGrogu avatar Mar 14 '16 18:03 GrandmasterGrogu

One odd thing I just noticed, the sandbox has another issue. It does not seem to produce the change either, in addition to not showing in the Changestream.

However, with my local project, it is a MySQL datasource, and the change does occur, though it does not show in the Changestream still.

GrandmasterGrogu avatar Mar 14 '16 18:03 GrandmasterGrogu

Hey @alexxgathp, thanks for putting up a sandbox, it makes troubleshooting so much easier for me!

I have figured out the issue. Your custom script is using a different instance of the app object. Everytime you fire up a new node process it is going to require a new app instance.

So, if you want to perform an operation on boot in a custom script, you can put the script in the 'boot' folder.

If you need the script to be available after boot while the app is running, you could try the following:

You could probably create a dumby model with a single remote method to run your script within the same app instance as well.

I haven't tried this so I can't guarantee that it works, but in theory you could make a remote app using loopback-connector-remote. I'm not sure if the example is up to date but you should be able to get the gist of how to set up a remote app from here: loopback-example-remote.

To be honest, the dumby model would probably be the easiest work around but the "proper" way to run custom scripts is to do with with a remote app.

Let me know if you have any questions along the way. Might be good to try it out in your sandbox and I'll be able to help you out there.

Good luck!

richardpringle avatar Mar 14 '16 20:03 richardpringle

Ah, I see. I assumed it was making calls to the REST API, but I see it was processing it as if it was the first instance.

I'll try one of those other ways. Thanks!

GrandmasterGrogu avatar Mar 14 '16 21:03 GrandmasterGrogu

Using loopback-connector-remote as a data-source connector makes REST calls so that should work.

richardpringle avatar Mar 14 '16 21:03 richardpringle