moleculer-web icon indicating copy to clipboard operation
moleculer-web copied to clipboard

Routes with autoAliases not considered for started

Open MichaelErmer opened this issue 1 year ago • 1 comments

If a API gateway has a configuration using autoAliases, the service will be marker as "ready" (started) even if the routes are not registered yet:

name: "xyz",
routes: [{
    path: "/download",
    use: [cookieparser()],
    whitelist: ["xyz.download"],
    autoAliases: true,
    mappingPolicy: "restrict",
    logging: false,
}]
...
actions: {
    xyz: {
        rest: {
            method: "POST",
            path: "/"
        },
       ...
    }
}

If this syntax is used, it works as intended:

name: "xyz",
routes: [{
    path: "/download",
    use: [cookieparser()],
    whitelist: ["xyz.download"],
    aliases: {
        "POST /": "xyz.download"
    }
    mappingPolicy: "restrict",
    logging: false,
}]
...
actions: {
    download: {
        rest: {
            method: "POST",
            path: "/"
        },
       ...
    }
}

this test will randomly fail for the first case:

describe("test", () => {
    beforeAll(async () => {
        broker.createService(xyzService);
        await broker.start();
        await broker.waitForServices(["xyz"]);
    });
    // test to request /download
});

MichaelErmer avatar Jan 08 '24 14:01 MichaelErmer

This is the expected functionality. The reason is, that the auto aliases function executes with a little delay to wait for all services should be started. In the case of a monolith, it looks like no reason to wait, but in a multi-node environment, if API gateway started, it doesn't mean all other services are loaded.

For tests, you should add a delay after the broker started, or set the debounceTime to zero in API gateway settings.

icebob avatar Jan 13 '24 19:01 icebob