PHP MySQL get_lock

In a script I'm trying to check whether the same script is already running using MySQL GET_LOCK. The problem is, when a script tries to get lock, which isn't free, it blocks forever regardless of the parameter I provide.

<?php

class Controller_Refresher extends Controller {
    public function action_run($key) {
        echo date('H:i:s'."
", time());
        $this->die_if_running();
        $this->run();
        echo date('H:i:s'."
", time());
    }

    private function die_if_running() {
        $result = DB::query(Database::SELECT, "SELECT IS_FREE_LOCK('refresher_running') AS free")->execute();
        if (! intval($result[0]['free'])) die('Running already');
        $result = DB::query(Database::SELECT, "SELECT GET_LOCK('refresher_running', 1)")->execute();
    }

    private function run() {
        echo "Starting
";
        ob_flush();
        sleep(10);

        DB::query(Database::SELECT, "SELECT RELEASE_LOCK('refresher_running')")->execute();
    }
}

When I run this in 2 tabs in browser, I get e.g.:

-tab 1-
20:48:16
Starting
20:48:26
-tab 2-
20:48:27
Starting
20:48:37

While what I want to do is to make the second tab die('Running already');.

Watch out - this problem might actually be caused by php locking the session file:

https://stackoverflow.com/a/5487811/539149

So you should call session_write_close() before any code that needs to run concurrently. I discovered this after trying this Mutex class:

http://code.google.com/p/mutex-for-php/

The class worked great but my php scripts were still running one by one!

Also, you don't need IS_FREE_LOCK(). Just call GET_LOCK('refresher_running', 0) and it will either return 1 if it gives you the lock or 0 if the lock is taken. It's more atomic that way. Of course, lock timeouts can still be useful in certain situations, like when you want to queue up tasks, but watch out for the script timing out if you get too many simultaneous requests.

Zack Morris

One option would be to rely on a filesystem lock instead of a database. Since it's the script execution that needs handling, it should not matter. A sample from the manual with a non-blocking exclusive lock:

$fp = fopen('/tmp/lock.txt', 'r+');

/* Activate the LOCK_NB option on an LOCK_EX operation */
if(!flock($fp, LOCK_EX | LOCK_NB)) {
    die('Running already');
}

/* ... */

fclose($fp);

Edit

Another option would be to use a status file that gets created at the beginning of each exection and will be automatically deleted by register_shutdown_function upon script completion.

The script would simply check the existence of the status file and if it's already there, execution would stop:

define('statusFile', sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'myjob.running');

//
// If a process is already running then exit
//
if (file_exists(statusFile)) {
   die('Running already');
} else {
   file_put_contents(date('Y-m-d H:i:s'), statusFile);
}

//
// Other code here
//

function shutdown() {
   unlink(statusFile);
}

//
// Remove the status file on completion
//
register_shutdown_function('shutdown');