Skip to content

Commit

Permalink
Fix double-printed logs after container restart
Browse files Browse the repository at this point in the history
  • Loading branch information
lekkas committed Jul 21, 2016
1 parent 72ed288 commit 761a0cb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
33 changes: 21 additions & 12 deletions src/lib/logger.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Docker = require 'dockerode'
PUBNUB = require 'pubnub'
Promise = require 'bluebird'
es = require 'event-stream'
Lock = require 'rwlock'

disableLogs = false

Expand Down Expand Up @@ -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

0 comments on commit 761a0cb

Please sign in to comment.