Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to quit the batcher loop ASAP when closing? #126

Open
whc2001 opened this issue Dec 24, 2022 · 1 comment
Open

Possible to quit the batcher loop ASAP when closing? #126

whc2001 opened this issue Dec 24, 2022 · 1 comment

Comments

@whc2001
Copy link

whc2001 commented Dec 24, 2022

I tried to gracefully shutdown winston by doing this before the exit of the program:

const loggerEnd = new Promise((resolve) => logger.on('finish', resolve));
logger.end();
await loggerEnd;
console.log("Debug - winston ended");

However, seems like that when the message is print, it still takes several seconds for node to actually quit. By inspecting the code I found this section:

winston-loki/src/batcher.js

Lines 232 to 259 in e6c2d20

async run () {
this.runLoop = true
while (this.runLoop) {
try {
await this.sendBatchToLoki()
if (this.interval === this.circuitBreakerInterval) {
if (this.options.interval !== undefined) {
this.interval = Number(this.options.interval) * 1000
} else {
this.interval = 5000
}
}
} catch (e) {
this.interval = this.circuitBreakerInterval
}
await this.wait(this.interval)
}
}
/**
* Stops the batch push loop
*
* @param {() => void} [callback]
*/
close (callback) {
this.runLoop = false
this.sendBatchToLoki()
.then(() => { if (callback) { callback() } }) // maybe should emit something here

After closing routine is triggered, the routine would still delay for the set timespan after executing sendBatchToLoki before returning. Is this designed behaviour? I tried to add if (!this.runLoop) break; after line 236 and the problem went away, but I am not sure if that would affect other things.

@mattbangert
Copy link

mattbangert commented Oct 31, 2023

I see that this has been merged, but even in 6.0.7 when batching is enabled node doesn't quit

private createLoggerInstance(): void {
    const lokiOptions = {
      transports: [
        new LokiTransport({
          host: GRAFANA_URL,
          labels: {
            ...this.labels, // Merge provided labels with default labels
            job: 'my-job'
          },
          replaceTimestamp: true,
          basicAuth: `${GRAFANA_USER}@${GRAFANA_KEY}`,
          json: true,
          batching: true,
          interval: 30,
          onConnectionError: (err: Error) => logger(err.message, 'error'),
          timeout: TIMEOUT_MS
        })
      ]
    }

    this.logger = createLogger(lokiOptions)
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants