Closed Thread Icon

Topic awaiting preservation: PHP flock() (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=29375" title="Pages that link to Topic awaiting preservation: PHP flock() (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: PHP flock() <span class="small">(Page 1 of 1)</span>\

 
Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted 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.

Now, if I were doing this in, say, a bash script, I'd use a lockfile to inform any instances of the script that they should send back a certain error code (which the governing program understands as a signal to generate a timeout and try again soon). I'm fairly sure of how I'd go about this in PHP, but I've never used file locks in PHP before, so I want to check my logic.

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;
}




Justice 4 Pat Richard

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana
Insane since: Aug 2000

posted 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.

.



-- not necessarily stoned... just beautiful.

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 07-27-2007 07:03

It's an LDAP database, running with a BDB backend. Locking the database itself is not an option.


Justice 4 Pat Richard

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted 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.

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted 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.


Justice 4 Pat Richard

(Edited by Skaarjj on 07-27-2007 14:46)

« BackwardsOnwards »

Show Forum Drop Down Menu