lock improve
This commit is contained in:
parent
8f7a3e38c9
commit
50d139a1b2
1 changed files with 12 additions and 9 deletions
|
|
@ -5,39 +5,42 @@ function new-lock {
|
||||||
[string]
|
[string]
|
||||||
$by
|
$by
|
||||||
)
|
)
|
||||||
|
|
||||||
Write-Output 'Setting lock'
|
Write-Output 'Setting lock'
|
||||||
$lock = $true
|
|
||||||
$timeout = 15
|
$timeout = 15
|
||||||
$i = 0
|
$i = 0
|
||||||
while ($lock) {
|
while ($true) {
|
||||||
if ($env:temp) {
|
if ($env:temp) {
|
||||||
$lockFile = Join-Path -Path $env:temp -ChildPath 'lockfile_pubg.lock'
|
$lockFile = Join-Path -Path $env:temp -ChildPath 'lockfile_pubg.lock'
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$lockFile = "/tmp/lockfile_pubg.lock"
|
$lockFile = "/tmp/lockfile_pubg.lock"
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Test-Path -Path $lockFile) {
|
if (Test-Path -Path $lockFile) {
|
||||||
Write-Host "Job is already running. Sleeping 10 seconds"
|
Write-Host "Job is already running. Lock file found at $lockFile. Sleeping 15 seconds."
|
||||||
Start-Sleep -Seconds 10
|
Start-Sleep -Seconds 15
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
try {
|
try {
|
||||||
$content = if ($by) { $by } else { "" }
|
$content = if ($by) { $by } else { "" }
|
||||||
New-Item -ItemType File -Path $lockFile -Value $content
|
New-Item -ItemType File -Path $lockFile -Value $content -Force
|
||||||
$lock = $false
|
Write-Host "Lock file created at $lockFile."
|
||||||
|
break
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
Write-Output "Unable to create lockfile , resuming lock loop"
|
Write-Output "Unable to create lockfile, error: $_. Resuming lock loop."
|
||||||
$lock = $true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($i -ge $timeout) {
|
if ($i -ge $timeout) {
|
||||||
Write-Output "Timed out"
|
Write-Output "Timed out after $timeout attempts."
|
||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
$i++
|
$i++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function remove-lock {
|
function remove-lock {
|
||||||
Write-Output 'Removing lock'
|
Write-Output 'Removing lock'
|
||||||
if ($env:temp) {
|
if ($env:temp) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue