A call to writeFileSync is producing a 0 bytes file at random.
The aim is to write a json to a file, then re-write it every time the json changes. And also load the json on startup. But the file keeps becoming 0 bytes long, most of the time it works then after a few days it becomes 0 bytes.
Code: LiveScript https://github.com/audreyt/ethercalc/blob/master/src/db.ls Or converted to 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?!
With logging etc:
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?!
Suggestion on how to debug or re-structure much appreciated.
Notes:
- Workaround - When I added an auto restart every 24 hours it fixed the problem.
- When it becomes 0 bytes, the code prints "dump file locked, exit process"
- the code is running on an openshift server
- looks to becomes 0 bytes when the server is auto restarted - not a manual restart, but when the server decides to trigger a restart