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