Adding Windows support, making more robust and improving output messages
Publish PHP Package / docker (push) Successful in 7s
Details
Publish PHP Package / docker (push) Successful in 7s
Details
This commit is contained in:
parent
5b3fff55c7
commit
1c7f64d340
40
lock.inc.php
40
lock.inc.php
|
@ -5,22 +5,45 @@ function gamectl_check_lock()
|
||||||
{
|
{
|
||||||
global $GAME_ROOT;
|
global $GAME_ROOT;
|
||||||
|
|
||||||
|
//let's check if are already in a locked environment
|
||||||
if(getenv("GAMECTL_IN_LOCK"))
|
if(getenv("GAMECTL_IN_LOCK"))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
//don't bother with windows
|
$is_nix = DIRECTORY_SEPARATOR === '/';
|
||||||
if(DIRECTORY_SEPARATOR != '/')
|
|
||||||
return;
|
$is_process_alive = $is_nix ?
|
||||||
|
function($pid) : bool
|
||||||
|
{
|
||||||
|
return !empty(trim(exec("lsof -p $pid")));
|
||||||
|
}
|
||||||
|
:
|
||||||
|
function($pid) : bool
|
||||||
|
{
|
||||||
|
$output = shell_exec("tasklist /FI \"PID eq $pid\" 2>NUL");
|
||||||
|
return strpos($output, "$pid") !== false;
|
||||||
|
};
|
||||||
|
|
||||||
$lock = $GAME_ROOT . '/gamectl.lock';
|
$lock = $GAME_ROOT . '/gamectl.lock';
|
||||||
|
$last_pid = null;
|
||||||
|
|
||||||
while(file_exists($lock))
|
while(file_exists($lock))
|
||||||
{
|
{
|
||||||
echo "gamectl is locked. Waiting...\n";
|
$pid = '0';
|
||||||
$pid = file_get_contents($lock);
|
try
|
||||||
if(!trim(exec("lsof -p $pid")))
|
|
||||||
{
|
{
|
||||||
echo "Seems gamectl process is gone but lock still exists, proceeding..\n";
|
$pid = file_get_contents($lock);
|
||||||
|
}
|
||||||
|
catch(Exception $e) {}
|
||||||
|
|
||||||
|
if($last_pid !== $pid)
|
||||||
|
{
|
||||||
|
echo "gamectl is locked by process ($pid). Waiting...\n";
|
||||||
|
$last_pid = $pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!$is_process_alive($pid))
|
||||||
|
{
|
||||||
|
echo "Seems gamectl process ($pid) is gone but lock still exists, proceeding..\n";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,9 +52,10 @@ function gamectl_check_lock()
|
||||||
|
|
||||||
register_shutdown_function('taskman\\gamectl_unlock');
|
register_shutdown_function('taskman\\gamectl_unlock');
|
||||||
|
|
||||||
|
//for spawned child processes
|
||||||
putenv("GAMECTL_IN_LOCK=1");
|
putenv("GAMECTL_IN_LOCK=1");
|
||||||
|
|
||||||
file_put_contents($lock, getmypid());
|
file_put_contents($lock, getmypid(), LOCK_EX);
|
||||||
}
|
}
|
||||||
|
|
||||||
function gamectl_unlock()
|
function gamectl_unlock()
|
||||||
|
|
Loading…
Reference in New Issue