()
| 83 | } |
| 84 | |
| 85 | _reopenStream() { |
| 86 | fs.open(this._filename, 'a', 0o644, (err, fd) => { |
| 87 | if (err) { |
| 88 | logger.error('ServerAccessLogger: failed to reopenStream: open error', err); |
| 89 | setTimeout(() => { this._reopenStream(); }, this._retryReopenDelayMS); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | fs.fstat(fd, (err, stat) => { |
| 94 | if (err || this._terminated) { |
| 95 | if (this._terminated) { |
| 96 | logger.error('ServerAccessLogger: terminated aborting reopen'); |
| 97 | fs.close(fd); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | logger.error('ServerAccessLogger: failed to reopenStream: stat error', err); |
| 102 | fs.close(fd); |
| 103 | setTimeout(() => { this._reopenStream(); }, this._retryReopenDelayMS); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | if (this.stream) { |
| 108 | this._cleanOldStream(true); |
| 109 | } |
| 110 | |
| 111 | this._stat = stat; |
| 112 | this.stream = fs.createWriteStream(this._filename, { |
| 113 | encoding: 'utf-8', |
| 114 | flush: true, |
| 115 | highWaterMark: this._highWaterMarkBytes, |
| 116 | fd, |
| 117 | }); |
| 118 | |
| 119 | this.stream.on('error', err => { |
| 120 | logger.error('ServerAccessLogger: stream error', err); |
| 121 | // close is called after error so no need to reopen here. |
| 122 | }); |
| 123 | |
| 124 | this.stream.on('close', () => { |
| 125 | logger.info('ServerAccessLogger: stream closed'); |
| 126 | this._cleanOldStream(false); |
| 127 | this._reopenStream(); |
| 128 | }); |
| 129 | }); |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | write(data) { |
| 134 | if (this._waitingDrain) { |
no test coverage detected