mirror of
https://github.com/pawelmalak/flame.git
synced 2025-08-06 03:15:18 +02:00
Use Promise.allSettled instead of Promise.all
This allows for any hosts that are able to be resolved to still function correctly while any that don't will throw an error
This commit is contained in:
parent
f4c4ba98be
commit
21d651b897
1 changed files with 116 additions and 106 deletions
|
@ -4,40 +4,49 @@ const Logger = require('../../../utils/Logger');
|
|||
const logger = new Logger();
|
||||
const loadConfig = require('../../../utils/loadConfig');
|
||||
|
||||
const useDocker = async (apps) => {
|
||||
const {
|
||||
useOrdering: orderType,
|
||||
unpinStoppedApps,
|
||||
dockerHost: host,
|
||||
} = await loadConfig();
|
||||
|
||||
let containers = []
|
||||
const hosts = host.split(';');
|
||||
|
||||
try {
|
||||
// Get list of containers
|
||||
containers = (await Promise.all(hosts.reduce((acc, host) => {
|
||||
async function getContainer ({ host }) {
|
||||
const isLocalhost = host.includes('localhost');
|
||||
acc.push(axios.get(
|
||||
try {
|
||||
return await axios.get(
|
||||
`http://${host}/containers/json?{"status":["running"]}`,
|
||||
isLocalhost ? {
|
||||
socketPath: '/var/run/docker.sock',
|
||||
} : {}
|
||||
))
|
||||
return acc;
|
||||
}, []))).flatMap(({ data }) => data)
|
||||
} catch (err) {
|
||||
logger.log(`Can't connect to the Docker API on one of the hosts: ${hosts.join(', ')}`, 'ERROR');
|
||||
)
|
||||
} catch {
|
||||
const error = `Can't connect to the Docker API on the host ${host}`;
|
||||
logger.log(error, 'ERROR');
|
||||
throw new Error(error)
|
||||
}
|
||||
}
|
||||
|
||||
const useDocker = async (apps) => {
|
||||
const {
|
||||
useOrdering: orderType,
|
||||
unpinStoppedApps,
|
||||
dockerHost: host = '',
|
||||
} = await loadConfig();
|
||||
|
||||
const hosts = host.split(';')
|
||||
const hostRequests = await Promise.allSettled(hosts.map((host) => getContainer({ host })))
|
||||
|
||||
if (hostRequests.length > 0) {
|
||||
let containers = hostRequests
|
||||
// Extract successful requests to docker socket
|
||||
.reduce((acc, { status, value }) => {
|
||||
if (status === 'fulfilled') {
|
||||
acc = acc.concat(value.data)
|
||||
}
|
||||
return acc;
|
||||
}, [])
|
||||
// Filter out containers without any annotations
|
||||
.filter((e) => Object.keys(e.Labels).length !== 0)
|
||||
|
||||
if (containers.length > 0) {
|
||||
apps = await App.findAll({
|
||||
order: [[orderType, 'ASC']],
|
||||
});
|
||||
|
||||
// Filter out containers without any annotations
|
||||
containers = containers.filter((e) => Object.keys(e.Labels).length !== 0);
|
||||
|
||||
const dockerApps = [];
|
||||
|
||||
for (const container of containers) {
|
||||
|
@ -136,6 +145,7 @@ const useDocker = async (apps) => {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = useDocker;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue