Initializing after restart



  • Hi,
    I would like to reinitialize RemoteObject after my app running in the engine has been restarted. For some reason sending image fails if app is restarted.

    Here's how to reproduce:

    1. Start testapp at the http://localhost:2015/
    2. Start test.js (see below)
    3. Success.
    4. Stop testapp after few seconds
    5. Start testapp after few seconds
    6. Failed.
    async function test() {
        try {
            const va = typeof window === "object" ?
            VisionAppster :           // Browser
            require("visionappster"); // Node.js
            const inputs = [
            await va.Image.fromUrl('image.jpg')
            ];
            const ctrl = new va.RemoteObject("http://localhost:2015/apis/com.myapps.testapp/1");
            const api = await ctrl.connect();
            await api.sendImage.async(...inputs);
            console.log('Success.');
        }
        catch {
            console.error("Failed.");
        }
    }
    
    setInterval(test, 2000);
    

    node.js v12.18.3
    npm visionappster 0.0.10
    VisionAppster 3.0-beta4.1



  • Hi,
    you can use manager object's appStarted signal to reconnect the API when the app has been restarted:

    const myAppId = 'com.myapps.testapp/1';
    let apiCtrl;
    let api;
    let managerCtrl;
    let manager;
    
    async function connectApi()
    {
      if (apiCtrl)
        apiCtrl.disconnect(); // Note: this is currently needed first
      apiCtrl = new va.RemoteObject('/apis/' + myAppId);
      api = await apiCtrl.connect();
    }
    
    async function connect()
    {
      managerCtrl = new va.RemoteObject('/manager');
      manager = await managerCtrl.connect();
      manager.$appStarted.connect((appId) =>
      {
        if (appId === myAppId)
          connectApi();
      });
      connectApi();
    }
    

    Now, if you add connect() call before setInterval() call in your example, the api object should be always connected and ready to serve whenever the app is running.
    P.S. there seems to be also an issue that the previous remote api object instance needs to be disconnected first, or reconnecting won't work. We'll be looking at
    that also.



  • @mt Thanks.



  • @ms I came back to this when running release tests with our new release.

    The simplest thing you can do is to explicitly disconnect the ctrl object on error:

    async function test() {
        let ctrl; // move declaration here
        try {
            const va = typeof window === "object" ?
            VisionAppster :           // Browser
            require("visionappster"); // Node.js
            const inputs = [
            await va.Image.fromUrl('image.jpg')
            ];
            ctrl = new va.RemoteObject("http://localhost:2015/apis/com.myapps.testapp/1");
            const api = await ctrl.connect();
            await api.sendImage.async(...inputs);
            console.log('Success.');
        }
        catch {
            console.error("Failed.");
            ctrl.disconnect(); // add this
        }
    }
    

    It is hard to do this automatically because JS is a garbage-collected language with no destructors. When the app on the server side is brought down, its control channel remains because no one called disconnect() on the client side. The JS runtime can keep the old instance alive arbitrarily long. Since there is a reference to the WebSocket instance, the connection will remain active as well.

    Now, when you create a new RemoteObject instance, it shares an existing connection by default (you are using the default mechanism). This time however the existing connection is defunct. The old server-side object does not exist any more although there is a new one in the same URL.

    The channel sharing mechanism could be improved to take the remote object ID into account, but that would be a rather invasive change now. The work-around is quite simple and I hope it will work for you for the time being. We'll come back to this during the next release cycle.

    -Topi-


Log in to reply