Repository navigation
Expand file tree
/
Copy pathupdate_scripts.ps1
More file actions
98 lines (91 loc) · 3.94 KB
/
Copy pathupdate_scripts.ps1
File metadata and controls
98 lines (91 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
param(
[ValidateRange(1, 168)]
[int]$IntervalHours = 6,
[switch]$Force
)
$ErrorActionPreference = "Stop"
$scriptDir = $PSScriptRoot
$logPath = Join-Path $scriptDir "update_scripts.log"
$updateLock = $null
function Write-UpdateLog {
param([string]$Message)
Add-Content -LiteralPath $logPath -Encoding UTF8 -Value "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') $Message"
}
function Invoke-UpdateGit {
param([string]$Arguments)
# Own the process so a network/authentication failure cannot hang the task
# or change the caller's LASTEXITCODE. Drain both pipes while Git runs.
$info = New-Object System.Diagnostics.ProcessStartInfo
$info.FileName = $gitPath
$info.Arguments = $Arguments
$info.WorkingDirectory = $scriptDir
$info.UseShellExecute = $false
$info.CreateNoWindow = $true
$info.RedirectStandardInput = $true
$info.RedirectStandardOutput = $true
$info.RedirectStandardError = $true
$info.EnvironmentVariables["GIT_TERMINAL_PROMPT"] = "0"
$info.EnvironmentVariables["GCM_INTERACTIVE"] = "never"
$process = [System.Diagnostics.Process]::Start($info)
try {
$process.StandardInput.Close()
$stdout = $process.StandardOutput.ReadToEndAsync()
$stderr = $process.StandardError.ReadToEndAsync()
if (-not $process.WaitForExit(60000)) {
if ($env:OS -eq "Windows_NT") {
$killInfo = New-Object System.Diagnostics.ProcessStartInfo
$killInfo.FileName = "$env:SystemRoot\System32\taskkill.exe"
$killInfo.Arguments = "/PID $($process.Id) /T /F"
$killInfo.UseShellExecute = $false
$killInfo.CreateNoWindow = $true
$killer = [System.Diagnostics.Process]::Start($killInfo)
try { [void]$killer.WaitForExit(5000) } finally { $killer.Dispose() }
}
if (-not $process.HasExited) { $process.Kill() }
throw "git $Arguments timed out after 60 seconds"
}
$output = ($stdout.GetAwaiter().GetResult() + $stderr.GetAwaiter().GetResult()).Trim()
if ($process.ExitCode -ne 0) {
throw "git $Arguments failed (exit=$($process.ExitCode)): $output"
}
return $output
}
finally {
$process.Dispose()
}
}
try {
$gitPath = (Get-Command git -CommandType Application -ErrorAction Stop | Select-Object -First 1).Source
$gitDir = Invoke-UpdateGit "rev-parse --absolute-git-dir"
$statePath = Join-Path $gitDir "ckb-sync-update.last-success"
$lockPath = Join-Path $gitDir "ckb-sync-update.lock"
try {
$updateLock = [IO.File]::Open($lockPath, [IO.FileMode]::OpenOrCreate, [IO.FileAccess]::ReadWrite, [IO.FileShare]::None)
}
catch [IO.IOException] {
# run.ps1 and get_diff_task.ps1 may start at the same time.
return
}
if (-not $Force -and (Test-Path -LiteralPath $statePath)) {
$lastSuccess = [DateTimeOffset]::MinValue
if ([DateTimeOffset]::TryParse((Get-Content -LiteralPath $statePath -Raw).Trim(), [ref]$lastSuccess)) {
$age = [DateTimeOffset]::UtcNow - $lastSuccess
if ($age.TotalSeconds -ge 0 -and $age.TotalHours -lt $IntervalHours) { return }
}
}
$before = Invoke-UpdateGit "rev-parse --short HEAD"
Write-UpdateLog "update_start commit=$before"
$output = Invoke-UpdateGit "-c merge.autoStash=false -c rebase.autoStash=false pull --ff-only --no-rebase"
if ($output) { Write-UpdateLog $output }
$after = Invoke-UpdateGit "rev-parse --short HEAD"
Set-Content -LiteralPath $statePath -Encoding ASCII -Value ([DateTimeOffset]::UtcNow.ToString("o"))
Write-UpdateLog "update_done before=$before after=$after"
}
catch {
$message = "update_failed: $($_.Exception.Message)"
try { Write-UpdateLog $message } catch { Write-Warning "Cannot write $logPath" }
Write-Warning "$message; continuing the existing task"
}
finally {
if ($null -ne $updateLock) { $updateLock.Dispose() }
}