1
Fork 0

make calling with specific names also monitor started containers

This commit is contained in:
Conduitry 2018-07-11 09:32:25 -04:00
parent 585e3810ff
commit e9d60d043b
1 changed files with 13 additions and 15 deletions

View File

@ -1,6 +1,7 @@
const EventEmitter = require('events'); const EventEmitter = require('events');
const { stat, watch } = require('fs'); const { stat, watch } = require('fs');
const { request } = require('http'); const { request } = require('http');
const { URLSearchParams } = require('url');
const { promisify } = require('util'); const { promisify } = require('util');
const statAsync = promisify(stat); const statAsync = promisify(stat);
@ -111,20 +112,17 @@ const detachWatchers = containerId => {
}; };
(async () => { (async () => {
if (process.argv.length > 2) { // prepare filters
// attach watchers to specified containers const name = process.argv.slice(2).map(name => `^/${name}$`);
for (const container of process.argv.slice(2)) { const [streamQuery, initQuery] = [
attachWatchers(container); { type: ['container'], event: ['start', 'die'], name },
} { name },
} else { ].map(filters => new URLSearchParams({ filters: JSON.stringify(filters) }));
// attach watchers to all containers and monitor starting and stopping of containers // attach watchers to all matching containers and monitor starting and stopping of matching containers
stream( stream(`/events?${streamQuery}`).on('', ({ Action, id }) =>
'/events?filters=%7B%22type%22%3A%5B%22container%22%5D%2C%22event%22%3A%5B%22start%22%2C%22die%22%5D%7D', (Action === 'start' ? attachWatchers : detachWatchers)(id),
).on('', ({ Action, id }) => );
(Action === 'start' ? attachWatchers : detachWatchers)(id), for (const { Id } of await api('get', `/containers/json?${initQuery}`)) {
); attachWatchers(Id);
for (const container of await api('get', '/containers/json')) {
attachWatchers(container.Id);
}
} }
})(); })();