Skip to content

Instantly share code, notes, and snippets.

@r15ch13
Last active December 14, 2015 16:29
Show Gist options
  • Save r15ch13/5115320 to your computer and use it in GitHub Desktop.
Save r15ch13/5115320 to your computer and use it in GitHub Desktop.
Useful for cronjobs, to run only once at the same time.
<?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