Reputation:
I'm making a discord bot that queues two people together for a game, it performs this by having their discord Id, queue status, an opponent in a JSON file. Looks like this for each user:
{
"discordId": "296062947329966080",
"dateAdded": "2019-03-11T02:34:01.303Z",
"queueStatus": "notQueuing",
"opponent": null
},
When one person queues up with a command it sets "queueStatus" to Queuing and when another person is found with Queuing it sets opponent to that person and tells both users that they are opponents. The problem is that randomly the JSON file will corrupt when changing and something like this will happen to the bottom:
"dateAdded": "2019-03-11T02:34:01.303Z",
"queueStatus": "notQueuing",
"opponent": null
}
]
}537"
}
]
}
My only idea is that it's because two people doing it at the same time writes to the file at the same time and corrupts it and that fs.writeFileSync
would fix it, but if I use fs.writeFileSync
the entire rest of the discord bot pauses and stops working until it's done writing which isn't a very practical solution.
Upvotes: 0
Views: 289
Reputation: 314
The data being stored in the JSON file should be migrated to MongoDB
or other DB. CRUD operations on a single static file from multiple jobs/sources is not a scalable solution. Migrating this data storage to a Database will resolve these pausing and stoppages.
Checkout this video on Youtube by freecodecamp.org
However, if the JSON file is required or still preferred I would recommend using EventEmitter
to create a single blocking queue for reading and writing.
Upvotes: 1