diff --git a/lib/ContainerLogger.js b/lib/ContainerLogger.js index b5869f7..d32065f 100644 --- a/lib/ContainerLogger.js +++ b/lib/ContainerLogger.js @@ -96,9 +96,10 @@ class ContainerLogger extends EventEmitter { _getLogsStrategyStream() { return Q.all([ Q.ninvoke(this.containerInterface, 'logs', { - follow: 1, - stdout: 1, - stderr: 1 + follow: true, + stdout: true, + stderr: true, + tail: 1000 }) ]); } diff --git a/lib/logger.js b/lib/logger.js index d711676..291f0b5 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -180,7 +180,7 @@ class Logger { * @param docker * @param newContainer */ - async _handleContainer(container) { // jshint ignore:line + async _handleContainer(container, loggerStrategy) { // jshint ignore:line const containerId = container.Id || container.id; const containerStatus = container.Status || container.status; const receivedLoggerId = _.get(container, 'Labels', _.get(container, 'Actor.Attributes'))['io.codefresh.logger.id']; @@ -189,7 +189,13 @@ class Logger { const receivedLogSizeLimit = _.get(container, 'Labels', _.get(container, 'Actor.Attributes'))['io.codefresh.logger.logSizeLimit']; - const loggerStrategy = _.get(container, 'Labels', _.get(container, 'Actor.Attributes'))['io.codefresh.logger.strategy']; + const maxRetryAttempts = 10; + const retryInterval = 1000; + var retryCount = 0; + + if (!loggerStrategy) { + loggerStrategy = _.get(container, 'Labels', _.get(container, 'Actor.Attributes'))['io.codefresh.logger.strategy']; + } if (!containerId) { logger.error(`Not handling container because id is missing`); @@ -256,16 +262,27 @@ class Logger { containerLogger.once('end', this._handleContainerStreamEnd.bind(this)); containerLogger.start() - .done(() => { - this.state.containers[containerId] = { status: ContainerHandlingStatus.LISTENING }; + .then(() => { + this.state[containerId] = { status: ContainerHandlingStatus.LISTENING }; this._writeNewState(); - }, (err) => { + }) + .catch(async (err) => { const error = new CFError({ cause: err, message: `Failed to start logging for container:${containerId}`, containerId }); logger.error(error.toString()); + + if (retryCount !== maxRetryAttempts) { + retryCount++; + logger.warn(`Making another attempt switching to the "${LoggerStrategy.LOGS}" logging strategy`); + + await new Promise(r => setTimeout(r, retryInterval)); + + delete this.state.containers[containerId]; + await this._handleContainer(container, LoggerStrategy.LOGS); + } }); }