Skip to content

Commit 05b5906

Browse files
committed
fast dependency solution
1 parent 075921e commit 05b5906

File tree

7 files changed

+120
-12
lines changed

7 files changed

+120
-12
lines changed

command/NetworkCommand.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const log = require('../logger');
2+
const Command = require('../command');
3+
4+
module.exports = class NetworkCommand extends Command {
5+
constructor(name, options) {
6+
super(name, options)
7+
}
8+
9+
_runInternal() {
10+
return new Promise(resolve => setTimeout(resolve, 1000))
11+
.then(() => log.info(`network created: '${this.name}'`));
12+
}
13+
};

command/ServiceCommand.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const log = require('../logger');
2+
const Command = require('../command');
3+
4+
module.exports = class ServiceCommand extends Command {
5+
constructor(name, options) {
6+
super(name, options)
7+
}
8+
9+
_runInternal() {
10+
return new Promise(resolve => setTimeout(resolve, 1000))
11+
.then(() => log.info(`service created: '${this.name}'`));
12+
}
13+
};

command/VolumeCommand.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const log = require('../logger');
2+
const Command = require('../command');
3+
4+
module.exports = class VolumeCommand extends Command {
5+
constructor(name, options) {
6+
super(name, options)
7+
}
8+
9+
_runInternal() {
10+
return new Promise(resolve => setTimeout(resolve, 1000))
11+
.then(() => log.info(`volume created: '${this.name}'`));
12+
}
13+
};

command/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const log = require('../logger');
2+
3+
module.exports = class Command {
4+
constructor(name, options) {
5+
this.name = name;
6+
this.options = options;
7+
this.dependencies = [];
8+
}
9+
10+
addDependency(dependency) {
11+
if (dependency instanceof Command) {
12+
this.dependencies.push(dependency);
13+
log.info(`${this.name} -> ${dependency.name}`)
14+
}
15+
}
16+
17+
run() {
18+
if (!this.executable) {
19+
this.executable = Promise.all(this.dependencies.map(command => command.run()))
20+
.then(() => this._runInternal())
21+
}
22+
return this.executable;
23+
}
24+
25+
_runInternal() {
26+
log.warn('Command run on base class');
27+
return Promise.resolve(); // todo : maybe reject
28+
}
29+
};

middleware/errorHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ const log = require('../logger');
33
module.exports = function (err, req, res, next) {
44
res.status(err.status || 500);
55
const message = err.message || err.constructor.name;
6-
log.error(message);
6+
log.error(err.status ? message : err.stack);
77
res.json({message: message});
88
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"http-errors": "~1.6.2",
1616
"morgan": "~1.9.0",
1717
"nconf": "^0.10.0",
18+
"parser-yaml": "^0.1.1",
1819
"winston": "^3.1.0",
1920
"yaml": "^1.0.0"
2021
},

services/DockerService.js

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,68 @@
11
const log = require('../logger');
2-
const yaml = require('yaml');
2+
const yaml = require('parser-yaml');
3+
const ServiceCommand = require('../command/ServiceCommand');
4+
const NetworkCommand = require('../command/NetworkCommand');
5+
const VolumeCommand = require('../command/VolumeCommand');
36

47
module.exports = class DockerService {
58
static applyYaml(fileContent) {
6-
const directives = yaml.parse(fileContent);
9+
const directives = yaml.parseSync(fileContent);
710
return this.applyDirectives(directives);
811
}
912

10-
static async applyDirectives(directives) {
11-
const all = {
12-
...directives.services,
13-
...directives.volumes,
14-
...directives.networks
15-
};
13+
static async applyDirectives({services, volumes, networks}) {
14+
const commands = {};
1615

17-
for (const name in directives.services) {
16+
for (const name in services) {
1817
log.info(`service: ${name}`);
1918
}
20-
for (const name in directives.volumes) {
19+
for (const name in volumes) {
2120
log.info(`volume: ${name}`);
2221
}
23-
for (const name in directives.networks) {
22+
for (const name in networks) {
2423
log.info(`network: ${name}`);
2524
}
2625

26+
for (const name in services) {
27+
const directive = services[name];
28+
const command = getCommand(name, services, commands, ServiceCommand);
29+
30+
const dependencies = directive.depends_on;
31+
const depNetworks = directive.networks;
32+
const depVolumes = directive.volumes;
33+
34+
if (dependencies) {
35+
for (const dependency of dependencies) {
36+
command.addDependency(getCommand(dependency, services, commands, ServiceCommand))
37+
}
38+
}
39+
40+
if (depNetworks) {
41+
for (const network of depNetworks) {
42+
command.addDependency(getCommand(network, networks, commands, NetworkCommand))
43+
}
44+
}
45+
46+
if (depVolumes) {
47+
for (const name of depVolumes) {
48+
const volume = name.split(':')[0];
49+
if (volumes.hasOwnProperty(volume)) {
50+
command.addDependency(getCommand(volume, volumes, commands, VolumeCommand))
51+
}
52+
}
53+
}
54+
}
55+
56+
await Promise.all(Object.values(commands).map(c => c.run()));
2757
return {ok: true}
2858
}
2959
};
60+
61+
function getCommand(name, directives, commands, CommandType) {
62+
let command = commands[name];
63+
if (!command) {
64+
command = new CommandType(name, directives[name] || {});
65+
commands[name] = command;
66+
}
67+
return command;
68+
}

0 commit comments

Comments
 (0)