From 98232ed7efe0b8c9820c2e083489224b86c5eade Mon Sep 17 00:00:00 2001 From: Thijs Stobbelaar Date: Mon, 13 Nov 2023 10:15:07 +0100 Subject: [PATCH] lock sleep and timeout --- includes/ps1/lockfile.ps1 | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/includes/ps1/lockfile.ps1 b/includes/ps1/lockfile.ps1 index 55a3637..ef5ecff 100644 --- a/includes/ps1/lockfile.ps1 +++ b/includes/ps1/lockfile.ps1 @@ -1,16 +1,29 @@ function new-lock { Write-Output 'Setting lock' - if ($env:temp) { - $lockFile = Join-Path -Path $env:temp -ChildPath 'lockfile_pubg.lock' - } - else { - $lockFile = "/tmp/lockfile_pubg.lock" - } - if (Test-Path -Path $lockFile) { - Write-Host "Job is already running." - Exit + $lock = $true + $timeout = 10 + $i = 0 + while ($lock) { + if ($env:temp) { + $lockFile = Join-Path -Path $env:temp -ChildPath 'lockfile_pubg.lock' + } + else { + $lockFile = "/tmp/lockfile_pubg.lock" + } + if (Test-Path -Path $lockFile) { + Write-Host "Job is already running. Sleeping 10 seconds" + Start-Sleep -Seconds 10 + }else{ + $lock = $false + } + if($i -ge $timeout){ + Write-Output "Timed out" + exit + } + $i++ } New-Item -ItemType File -Path $lockFile | Out-Null + } function remove-lock { Write-Output 'Removing lock'