From 50d139a1b2c1d901b7faa4a26bfa1d856f559054 Mon Sep 17 00:00:00 2001 From: Lanta Date: Wed, 22 Nov 2023 22:13:08 +0100 Subject: [PATCH] lock improve --- includes/ps1/lockfile.ps1 | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/includes/ps1/lockfile.ps1 b/includes/ps1/lockfile.ps1 index 5804ae4..82005cf 100644 --- a/includes/ps1/lockfile.ps1 +++ b/includes/ps1/lockfile.ps1 @@ -5,39 +5,42 @@ function new-lock { [string] $by ) + Write-Output 'Setting lock' - $lock = $true $timeout = 15 $i = 0 - while ($lock) { + while ($true) { 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 + Write-Host "Job is already running. Lock file found at $lockFile. Sleeping 15 seconds." + Start-Sleep -Seconds 15 } else { try { $content = if ($by) { $by } else { "" } - New-Item -ItemType File -Path $lockFile -Value $content - $lock = $false + New-Item -ItemType File -Path $lockFile -Value $content -Force + Write-Host "Lock file created at $lockFile." + break } catch { - Write-Output "Unable to create lockfile , resuming lock loop" - $lock = $true + Write-Output "Unable to create lockfile, error: $_. Resuming lock loop." } } + if ($i -ge $timeout) { - Write-Output "Timed out" + Write-Output "Timed out after $timeout attempts." exit } $i++ } } + function remove-lock { Write-Output 'Removing lock' if ($env:temp) {