Last active
December 14, 2015 16:29
-
-
Save r15ch13/5115320 to your computer and use it in GitHub Desktop.
Useful for cronjobs, to run only once at the same time.
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 | |
function runonce($name, Closure $closure) | |
{ | |
$lockname = $name . '.pid'; | |
try { | |
if(!Cache::has($lockname)) { | |
Cache::forever($lockname, getmypid()); | |
if ($closure instanceof Closure) { | |
$closure = call_user_func($closure); | |
} | |
Cache::forget($lockname); | |
} else { | |
Log::info($lockname.' is already running'); | |
} | |
} catch (Exception $e) { | |
Cache::forget($lockname); | |
Log::error($e->getMessage()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment