fix(windows): suppress duplicate logon markers per session

This commit is contained in:
igor04091968
2026-04-27 03:00:53 +03:00
parent 0ae2ac7ec1
commit 5584144978
+33 -6
View File
@@ -527,13 +527,33 @@ function Send-LogonMarkerIfNeeded {
} }
`$stateRoot = [string]`$Config.paths.stateRoot `$stateRoot = [string]`$Config.paths.stateRoot
if ([string]::IsNullOrWhiteSpace(`$stateRoot)) { `$markerRoots = New-Object System.Collections.Generic.List[string]
return if (-not [string]::IsNullOrWhiteSpace(`$env:LOCALAPPDATA)) {
`$markerRoots.Add((Join-Path `$env:LOCALAPPDATA 'ActivityWatch-Phase2\markers'))
}
if (-not [string]::IsNullOrWhiteSpace(`$stateRoot)) {
`$markerRoots.Add((Join-Path `$stateRoot 'markers'))
} }
`$markerDir = Join-Path `$stateRoot 'markers' `$markerDir = `$null
if (-not (Test-Path -LiteralPath `$markerDir)) { foreach (`$candidate in `$markerRoots) {
New-Item -Path `$markerDir -ItemType Directory -Force | Out-Null try {
if (-not (Test-Path -LiteralPath `$candidate)) {
New-Item -Path `$candidate -ItemType Directory -Force | Out-Null
}
`$probePath = Join-Path `$candidate 'write-test.tmp'
Set-Content -LiteralPath `$probePath -Value 'ok' -Encoding ASCII
Remove-Item -LiteralPath `$probePath -Force -ErrorAction SilentlyContinue
`$markerDir = `$candidate
break
}
catch {
}
}
if (-not `$markerDir) {
return
} }
`$markerFile = Join-Path `$markerDir ("logon-{0}-{1}.marker" -f `$env:USERNAME, `$SessionId) `$markerFile = Join-Path `$markerDir ("logon-{0}-{1}.marker" -f `$env:USERNAME, `$SessionId)
@@ -541,6 +561,8 @@ function Send-LogonMarkerIfNeeded {
return return
} }
Set-Content -LiteralPath `$markerFile -Value ((Get-Date).ToUniversalTime().ToString('o')) -Encoding UTF8
`$bucketId = ('{0}_{1}' -f `$bucketPrefix, `$script:Hostname) `$bucketId = ('{0}_{1}' -f `$bucketPrefix, `$script:Hostname)
Ensure-Bucket -BucketId `$bucketId -ClientName 'aw-session-events' -BucketType 'aw.session.event' Ensure-Bucket -BucketId `$bucketId -ClientName 'aw-session-events' -BucketType 'aw.session.event'
@@ -557,8 +579,13 @@ function Send-LogonMarkerIfNeeded {
} }
} | ConvertTo-Json -Depth 5 -Compress } | ConvertTo-Json -Depth 5 -Compress
try {
Invoke-AwJsonPost -Uri "`$(`$script:ApiBase)/buckets/`$bucketId/heartbeat?pulsetime=1" -Json `$payload Invoke-AwJsonPost -Uri "`$(`$script:ApiBase)/buckets/`$bucketId/heartbeat?pulsetime=1" -Json `$payload
Set-Content -LiteralPath `$markerFile -Value ((Get-Date).ToUniversalTime().ToString('o')) -Encoding UTF8 }
catch {
Remove-Item -LiteralPath `$markerFile -Force -ErrorAction SilentlyContinue
throw
}
} }
function Start-CollectorScriptIfNeeded { function Start-CollectorScriptIfNeeded {