Topic awaiting preservation: PHP flock() (Page 1 of 1) |
|
---|---|
Maniac (V) Mad Scientist From: :morF |
posted 07-26-2007 12:52
I'm writing a set of scripts at the moment (which are going to be shell executed in the end... gotta love *nix and hashbangs). They're interacting with the filesystem and an LDAP database to provision, modify and delete/archive user accounts, groups and virtual mail/ftp/jabber accounts, etc. One big important thing for us is preventing two people concurrently accessing the scripts and (potentially) creating conflicts in the database, and the assorted associated badnesses. code: define('EXIT_NO_CONCURRENT',11); define('EXIT_UNABLE_TO_LOCK',55); $LOCK_FILE="/tmp/ldap.lock"; $FILE = null; function lock() { global $LOCK_FILE, $FILE; if(file_exists($lock_file)) { exit(EXIT_NO_CONCURRENT); } else { if($FILE = fopen($LOCK_FILE)) { @flock($fp, LOCK_EX) or exit(EXIT_UNABLE_TO_LOCK); return true; } else { exit(EXIT_UNABLE_TO_LOCK); } } } function unlock() { global $LOCK_FILE, $FILE; fclose($FILE); unlink($LOCK_FILE); return true; }
|
Paranoid (IV) Inmate From: Madison, Indiana |
posted 07-26-2007 18:45
I'm not sure why you're doing this, but, I assume that you will have an associated database for this script. I seem to remember reading recently that if you want a lock in PHP you're better off locking a table or record in a database than using a lock file to create the lock. |
Maniac (V) Mad Scientist From: :morF |
posted 07-27-2007 07:03
It's an LDAP database, running with a BDB backend. Locking the database itself is not an option. |
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 07-27-2007 14:18
last time I checked, berkley dbs did lock all by them selves... at least the subversion one's do that. |
Maniac (V) Mad Scientist From: :morF |
posted 07-27-2007 14:39
Well, I have no idea how old this version of BDB is. It's the one that OpenLDAP 2.2 is built on top of. And even if BDB locks the table itself, does OpenLDAP understand that? I'm not prepared for this server to b0rk just because two people try to run a script at the same time. I need the concurrency contention to fail gracefully, and inform the governing process that it has happened, so it can queue the request for retrying in a few seconds. |