From 761a0cbd82594c378e6fe7b0620b6e0e442ffe76 Mon Sep 17 00:00:00 2001 From: Kostas Lekkas Date: Thu, 21 Jul 2016 18:56:25 +0300 Subject: [PATCH] Fix double-printed logs after container restart --- CHANGELOG.md | 1 + src/lib/logger.coffee | 33 +++++++++++++++++++++------------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dde5d548..a0fdf9be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ +* Fix duplicate logs issue [Kostas] * **[Breaking]** Do not bind mount /run/dbus to /run/dbus [Pablo] * Default to not bind mounting kmod if container distro can't be found [Pablo] * Use log-timestamp to add timestamps to logs [Pablo] diff --git a/src/lib/logger.coffee b/src/lib/logger.coffee index 85e4c211..9f9a1f71 100644 --- a/src/lib/logger.coffee +++ b/src/lib/logger.coffee @@ -3,6 +3,7 @@ Docker = require 'dockerode' PUBNUB = require 'pubnub' Promise = require 'bluebird' es = require 'event-stream' +Lock = require 'rwlock' disableLogs = false @@ -52,17 +53,25 @@ exports.log = -> publish(arguments...) do -> + _lock = new Lock() + _writeLock = Promise.promisify(_lock.async.writeLock) + loggerLock = (containerId) -> + _writeLock(containerId) + .disposer (release) -> + release() + attached = {} exports.attach = (app) -> - if !attached[app.containerId] - dockerPromise.then (docker) -> - docker.getContainer(app.containerId) - .attachAsync({ stream: true, stdout: true, stderr: true, tty: true }) - .then (stream) -> - attached[app.containerId] = true - stream.pipe(es.split()) - .on('data', publish) - .on 'error', -> - attached[app.containerId] = false - .on 'end', -> - attached[app.containerId] = false + Promise.using loggerLock(app.containerId), -> + if !attached[app.containerId] + dockerPromise.then (docker) -> + docker.getContainer(app.containerId) + .attachAsync({ stream: true, stdout: true, stderr: true, tty: true }) + .then (stream) -> + attached[app.containerId] = true + stream.pipe(es.split()) + .on('data', publish) + .on 'error', -> + attached[app.containerId] = false + .on 'end', -> + attached[app.containerId] = false