0

对 writeFileSync 的调用正在随机生成一个 0 字节的文件。

目的是将 json 写入文件,然后在每次 json 更改时重新写入。并在启动时加载 json。但是文件一直变成 0 字节长,大部分时间它可以工作,然后几天后它变成 0 字节。

代码:LiveScript https://github.com/audreyt/ethercalc/blob/master/src/db.ls 或转换成JavaScript https://github.com/audreyt/ethercalc/blob/master/db.js

   try
      db.DB = JSON.parse do
        require \fs .readFileSync "#dataDir/dump.json" \utf8
      console.log "==> Restored previous session from JSON file"
      db.DB = {} if db.DB is true
    Commands =
      bgsave: (cb) ->
        fs.writeFileSync do
          "#dataDir/dump.json"
          JSON.stringify db.DB,,2
          \utf8
        cb?!

使用日志记录等:

if fs.existsSync "#dataDir/dump.json" #check file exists - throws exception if exists and read fails
  #console.log "==> Found previous JSON file"
  try      
    db.DB = JSON.parse do
      require \fs .readFileSync "#dataDir/dump.json" \utf8
    console.log "==> Restored previous session from JSON file"
    db.DB = {} if db.DB is true
  catch 
    console.log "dump file locked, exit process"
    process.exit! 
else
  console.log "No existing dump file - #dataDir/dump.json "
Commands =
  bgsave: (cb) ->
    fs.writeFileSync do
      "#dataDir/dump.json"
      JSON.stringify db.DB,,2
      \utf8
    cb?!

非常感谢有关如何调试或重新结构的建议。

注意: - 解决方法 - 当我每 24 小时添加一次自动重启时,它解决了问题。
- 当它变成 0 字节时,代码打印“dump file locked, exit process” - 代码在 openshift 服务器上运行 - 当服务器自动重启时看起来变成 0 字节 - 不是手动重启,而是当服务器决定时触发重启

4

0 回答 0