Created
October 20, 2011 09:16
-
-
Save real34/1300738 to your computer and use it in GitHub Desktop.
PHP : Simple mutex par fichiers, fonctionnant en CLI mais pas via Apache (désolé pour la variable globale qui pique les yeux ;))
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$rootPath = realpath(dirname(__FILE__) . '/../') . '/'; | |
function takeMutex($name) { | |
global $rootPath; | |
$filename = $rootPath . 'tmp/cron_' . $name . '.lock'; | |
if (($handle = @fopen($filename, 'x')) === false) { | |
die('Une autre instance de cette tâche est en cours d\'exécution, ou impossible de créer le fichier "' . $filename . '", vérifiez les droits d\'écriture !'); | |
} | |
fclose($handle); | |
} | |
function freeMutex($name) { | |
global $rootPath; | |
$filename = $rootPath . 'tmp/cron_' . $name . '.lock'; | |
if (!unlink($filename)) { | |
die('Impossible de supprimer le fichier de lock "' . $filename . '". Veuillez le faire manuellement'); | |
} | |
} | |
takeMutex('foo'); | |
echo 'Mutex ok'; | |
sleep(5); | |
echo ' ... fini'; | |
freeMutex('foo'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment