From 21f4f9dcab0eea56313a99332f7488b65168ec97 Mon Sep 17 00:00:00 2001 From: Lanta Date: Sat, 30 Sep 2023 10:07:39 +0200 Subject: [PATCH] lock file feature --- includes/ps1/lockfile.ps1 | 26 ++++++++++++++++++++++++++ update/get_matches.ps1 | 10 +++++++++- update/matchparser.ps1 | 6 +++++- update/update_clan.ps1 | 7 +++++-- update/update_clan_members.ps1 | 7 +++++-- 5 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 includes/ps1/lockfile.ps1 diff --git a/includes/ps1/lockfile.ps1 b/includes/ps1/lockfile.ps1 new file mode 100644 index 0000000..0e53033 --- /dev/null +++ b/includes/ps1/lockfile.ps1 @@ -0,0 +1,26 @@ +function new-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 + } + New-Item -ItemType File -Path $lockFile | Out-Null + + + + +} +function remove-lock { + if ($env:temp) { + $lockFile = Join-Path -Path $env:temp -ChildPath 'lockfile_pubg.lock' + } + else { + $lockFile = "/tmp/lockfile_pubg.lock" + } + Remove-Item -Path $lockFile +} \ No newline at end of file diff --git a/update/get_matches.ps1 b/update/get_matches.ps1 index cfabcfa..8163ca8 100644 --- a/update/get_matches.ps1 +++ b/update/get_matches.ps1 @@ -1,3 +1,6 @@ +. .\..\includes\ps1\lockfile.ps1 + +new-lock if ($PSScriptRoot.length -eq 0) { $scriptroot = Get-Location @@ -5,6 +8,9 @@ if ($PSScriptRoot.length -eq 0) { else { $scriptroot = $PSScriptRoot } + + + # Read the content of the file as a single string $fileContent = Get-Content -Path "$scriptroot/../config/config.php" -Raw @@ -92,4 +98,6 @@ $playermatches += [PSCustomObject]@{ updated = $formattedString } -$player_matches | convertto-json -Depth 100 | out-file "$scriptroot/../data/player_matches.json" \ No newline at end of file +$player_matches | convertto-json -Depth 100 | out-file "$scriptroot/../data/player_matches.json" + +remove-lock \ No newline at end of file diff --git a/update/matchparser.ps1 b/update/matchparser.ps1 index ad3da4e..5f83080 100644 --- a/update/matchparser.ps1 +++ b/update/matchparser.ps1 @@ -1,3 +1,6 @@ +. .\..\includes\ps1\lockfile.ps1 + +new-lock if ($PSScriptRoot.length -eq 0) { $scriptroot = Get-Location @@ -172,4 +175,5 @@ foreach ($file in $difference) { write-output "removing $scriptroot/../data/telemetry_cache/$file" Remove-Item -Path "$scriptroot/../data/telemetry_cache/$file" } -write-output "Operation complete" \ No newline at end of file +write-output "Operation complete" +remove-lock \ No newline at end of file diff --git a/update/update_clan.ps1 b/update/update_clan.ps1 index a138c4c..5c9b66a 100644 --- a/update/update_clan.ps1 +++ b/update/update_clan.ps1 @@ -1,4 +1,6 @@ - +. .\..\includes\ps1\lockfile.ps1 + +new-lock if($PSScriptRoot.length -eq 0){ $scriptroot = Get-Location }else{ @@ -45,4 +47,5 @@ $formattedString = "$currentDateTime - Time Zone: $currentTimezone" $clandata | Add-Member -Name "updated" -MemberType NoteProperty -Value $formattedString $clandata | convertto-json -Depth 100 | out-file "$scriptroot/../data/claninfo.json" -$clandata | convertto-json -Depth 100 \ No newline at end of file +$clandata | convertto-json -Depth 100 +remove-lock \ No newline at end of file diff --git a/update/update_clan_members.ps1 b/update/update_clan_members.ps1 index 075cf09..8501de3 100644 --- a/update/update_clan_members.ps1 +++ b/update/update_clan_members.ps1 @@ -1,4 +1,6 @@ - +. .\..\includes\ps1\lockfile.ps1 + +new-lock if ($PSScriptRoot.length -eq 0) { $scriptroot = Get-Location } @@ -122,4 +124,5 @@ $lifetimestats['updated'] = $formattedString # Output the formatted string -$lifetimestats | convertto-json -Depth 100 | out-file "$scriptroot/../data/player_lifetime_data.json" \ No newline at end of file +$lifetimestats | convertto-json -Depth 100 | out-file "$scriptroot/../data/player_lifetime_data.json" +remove-lock \ No newline at end of file