Skip to content

Commit

Permalink
feat: add new option for initials dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Butarelo committed Jan 27, 2025
1 parent b97408e commit 84ff256
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
12 changes: 9 additions & 3 deletions src/runner-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export default class MoleculerRunner {
.option("env", "Load .env file from the current directory")
.option("envfile", "Load a specified .env file")
.option("instances", "Launch [number] instances node (load balanced)")
.option("mask", "Filemask for service loading");
.option("mask", "Filemask for service loading")
.option("dependencies", "List of global dependencies for all services");

this.flags = Args.parse(procArgs, {
mri: {
Expand All @@ -98,13 +99,18 @@ export default class MoleculerRunner {
e: "env",
E: "envfile",
i: "instances",
m: "mask"
m: "mask",
d: "dependencies"
},
boolean: ["repl", "silent", "hot", "env"],
string: ["config", "envfile", "mask"]
string: ["config", "envfile", "mask", "dependencies"]
}
});

if (this.flags.dependencies) {
this.flags.dependencies = this.flags.dependencies.split(",").map(dep => dep.trim());
}

this.servicePaths = Args.sub;
}

Expand Down
14 changes: 11 additions & 3 deletions src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class MoleculerRunner {
.option("env", "Load .env file from the current directory")
.option("envfile", "Load a specified .env file")
.option("instances", "Launch [number] instances node (load balanced)")
.option("mask", "Filemask for service loading");
.option("mask", "Filemask for service loading")
.option("dependencies", "List of global dependencies for all services");

this.flags = Args.parse(procArgs, {
mri: {
Expand All @@ -95,13 +96,18 @@ class MoleculerRunner {
e: "env",
E: "envfile",
i: "instances",
m: "mask"
m: "mask",
d: "dependencies"
},
boolean: ["repl", "silent", "hot", "env"],
string: ["config", "envfile", "mask"]
string: ["config", "envfile", "mask", "dependencies"]
}
});

if (this.flags.dependencies) {
this.flags.dependencies = this.flags.dependencies.split(",").map(dep => dep.trim());
}

this.servicePaths = Args.sub;
}

Expand Down Expand Up @@ -307,6 +313,8 @@ class MoleculerRunner {

if (this.flags.hot) this.config.hotReload = true;

if (this.flags.dependencies) this.config.initialDependencies = this.flags.dependencies;

// console.log("Merged configuration", this.config);
}

Expand Down
2 changes: 2 additions & 0 deletions src/service-broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const defaultOptions = {

errorRegenerator: null,

initialDependencies: [],

requestTimeout: 0 * 1000,
retryPolicy: {
enabled: false,
Expand Down
34 changes: 27 additions & 7 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,33 @@ class Service {
return this.broker.callMiddlewareHook("serviceStarting", [this]);
})
.then(() => {
// Wait for dependent services
if (this.schema.dependencies)
return this.waitForServices(
this.schema.dependencies,
this.settings.$dependencyTimeout || this.broker.options.dependencyTimeout,
this.settings.$dependencyInterval || this.broker.options.dependencyInterval
);
if (this.schema.dependencies) {
const dependencies = [];

if (this.broker.options.initialDependencies)
this.broker.options.initialDependencies.forEach(dependencie => {
if (dependencie !== this.name) dependencies.push(dependencie);
});

if (this.schema.dependencies)
Array.isArray(this.schema.dependencies)
? this.schema.dependencies.forEach(dependencie => {
if (dependencie !== this.name) dependencies.push(dependencie);
})
: dependencies.push(this.schema.dependencies);

const uniqueDependencies = _.uniq(dependencies);

if (uniqueDependencies.length > 0) {
return this.waitForServices(
uniqueDependencies,
this.settings.$dependencyTimeout ||
this.broker.options.dependencyTimeout,
this.settings.$dependencyInterval ||
this.broker.options.dependencyInterval
);
}
}
})
.then(() => {
if (isFunction(this.schema.started))
Expand Down
2 changes: 2 additions & 0 deletions test/unit/service-broker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ describe("Test ServiceBroker constructor", () => {
enabled: true,
retries: 3
},
initialDependencies: [],
contextParamsCloning: true,
maxCallLevel: 10,
requestTimeout: 5000,
Expand Down Expand Up @@ -280,6 +281,7 @@ describe("Test ServiceBroker constructor", () => {
factor: 2,
check: expect.any(Function)
},
initialDependencies: [],
requestTimeout: 5000,
maxCallLevel: 10,
maxSafeObjectSize: null,
Expand Down

0 comments on commit 84ff256

Please sign in to comment.