You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整node-tail的文件变更检测频率,推荐参数设置值是多少?

延迟原因

你当前配置开启了useWatchFile: true,node-tail底层会调用Node.js原生fs.watchFile轮询检测文件变更,默认轮询间隔为5007ms,你遇到的3秒左右延迟是轮询间隔的正常波动。

解决方案

在Tail实例化的配置项中新增fsWatchOptions参数,指定interval值自定义轮询频率即可,示例修改后的代码如下:

import { Config } from "../config";
import { Tail } from "tail"
import { Bridges } from "./bridge";
import { existsSync } from "fs";

export module Tails {
  export function init(server : Config.Server) {

    if (existsSync(server.local_log_file_path)) {
        const tail = new Tail(server.local_log_file_path, {
          useWatchFile: true,
          // 新增配置项
          fsWatchOptions: {
            interval: 100 // 单位为毫秒
          }
        })
        const bridge = Bridges.getFromName(server.name);
        console.log(`Tailing log file in '${server.name}'`);

        tail.on("line", (line: string) => {
        if (!bridge) return;
        bridge.emit("minecraft", line);
        });

        tail.on("error", (error: any) => {
          console.log("Error when tailing log file: " + error);
        });
    } else {
      console.log("Error: Could not find log file : " + server.local_log_file_path);
    }
  }
}

参数取值建议

  • 常规场景下设置为100~200ms即可,既可以将延迟控制在人感知不到的范围,也不会产生过高的磁盘IO和CPU占用
  • 不建议设置低于50ms,高频轮询会带来不必要的资源开销,性能收益极低
  • 如果你的日志文件存储在本地磁盘(非网络挂载盘、共享盘),可以直接删除useWatchFile: true配置,node-tail默认会使用事件驱动的fs.watch监听文件变更,不需要轮询,延迟更低且资源占用更小。

内容的提问来源于stack exchange,提问作者Syntro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 06:00:01