1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-28 06:00:06 +02:00

fix(portal-api): get current scale from db

This commit is contained in:
Sérgio Ramos 2017-08-30 16:46:40 +01:00
parent 3bcd712e42
commit 8695a134d2

View File

@ -675,69 +675,64 @@ class Data extends EventEmitter {
}); });
} }
static _calcCurrentScale ({ config, currentVersion }, cb) { // static _calcCurrentScale ({ config, currentVersion }, cb) {
return config.map(({ name }) => { // return config.map(({ name }) => {
const currentScale = Find(ForceArray(currentVersion ? currentVersion.scale : []), [ // const currentScale = Find(ForceArray(currentVersion ? currentVersion.scale : []), [
'serviceName', // 'serviceName',
name // name
]); // ]);
//
// return {
// id: Uuid(),
// serviceName: name,
// replicas: Number.isFinite(currentScale) ? currentScale : 1
// };
// });
// }
return { _getCurrentScale (deploymentGroupId, cb) {
id: Uuid(), const handleServiceInstanceMap = (err, result) => {
serviceName: name,
replicas: Number.isFinite(currentScale) ? currentScale : 1
};
});
}
_getCurrentScale ({ deploymentGroupName, config, currentVersion }, cb) {
const fallback = (err) => {
if (err) { if (err) {
console.error(err); return cb(err);
} }
Data._calcCurrentScale({ config, currentVersion }, cb); cb(err, ForceArray(result.successes).map(({ name, instances }) => ({
};
if (!this._triton) {
return fallback();
}
if (!this._machines) {
this._server.log(['debug'], '-> watcher not yet defined');
return fallback();
}
const machines = ForceArray(this._machines.getContainers())
.filter(({ tags = {} }) => {
return tags[DEPLOYMENT_GROUP] === deploymentGroupName;
});
const liveServices = machines.reduce((acc, { tags }) => {
return Object.assign(acc, {
[tags[SERVICE]]: 1
});
}, {});
const allAndConfigServices = config.reduce((acc, { name }) => {
return Object.assign(acc, {
[name]: 1
});
}, liveServices);
const scale = Object.keys(allAndConfigServices).map((name) => {
const existingMachines = machines.filter((machine) => {
return machine.tags[SERVICE] === name;
});
return {
id: Uuid(), id: Uuid(),
serviceName: name, serviceName: name,
replicas: existingMachines.length ? existingMachines.length : 1 replicas: ForceArray(instances).length
}; })));
}); };
cb(null, scale); const handleServices = ({ dg }) => (err, services) => {
if (err) {
return cb(err);
}
VAsync.forEachParallel({
inputs: services,
func: (service, next) => {
service.instances({}, (err, instances) => {
if (err) {
return next(err);
}
next(err, Object.assign({}, service, {
instances
}));
});
}
}, handleServiceInstanceMap);
};
const handleDeploymentGroup = (err, dg) => {
if (err) {
return cb(err);
}
dg.services({}, handleServices({ dg }));
};
this.getDeploymentGroup({ id: deploymentGroupId }, handleDeploymentGroup);
} }
scale ({ serviceId, replicas }, cb) { scale ({ serviceId, replicas }, cb) {
@ -918,13 +913,7 @@ class Data extends EventEmitter {
this._server.log(['debug'], '-> fetching current scale'); this._server.log(['debug'], '-> fetching current scale');
this._getCurrentScale({ this._getCurrentScale(ctx.deploymentGroup.id, handleCurrentScale);
deploymentGroupName: ctx.deploymentGroup.name,
currentVersion: ctx.version,
config: [{
name: ctx.service.name
}]
}, handleCurrentScale);
}; };
const handleVersion = (err, version) => { const handleVersion = (err, version) => {
@ -1365,11 +1354,7 @@ class Data extends EventEmitter {
ctx.currentVersion = currentVersion; ctx.currentVersion = currentVersion;
this._getCurrentScale({ this._getCurrentScale(ctx.currentDeploymentGroup.id, handleCurrentScale);
deploymentGroupName: ctx.currentDeploymentGroup.name,
config: ctx.config,
currentVersion
}, handleCurrentScale);
}; };
// 4. handle new version // 4. handle new version