2

Locking NFS files in PHP

 2 years ago
source link: https://www.codesd.com/item/locking-nfs-files-in-php.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Locking NFS files in PHP

advertisements

Part of my latest webapp needs to write to file a fair amount as part of its logging. One problem I've noticed is that if there are a few concurrent users, the writes can overwrite each other (instead of appending to file). I assume this is because of the destination file can be open in a number of places at the same time.

flock(...) is usually superb but it doesn't appear to work on NFS... Which is a huge problem for me as the production server uses a NFS array.

The closest thing I've seen to an actual solution involves trying to create a lock dir and waiting until it can be created. To say this lacks elegance is understatement of the year, possibly decade.

Any better ideas?

Edit: I should add that I don't have root on the server and doing the storage in another way isn't really feasible any time soon, not least within my deadline.


Another dirty hack would be to flock() a "local" file, and only open / write to the NFS file if you hold the lock on the local file.

Edit: from the flock() page:

flock() will not work on NFS and many other networked file systems. Check your operating system documentation for more details.

Edit 2:

Of course there's always using the database to synchonise access (I'm assuming your app uses a db). This would be quite a performance hit if you're doing a lot of logging though.

If it's just for logging, do you actually need a centralised log file? Could you log locally (and even combine the logs when they rotate at the end of the day if needed)?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK