-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathDependencyInstaller.cs
More file actions
196 lines (174 loc) · 7.1 KB
/
DependencyInstaller.cs
File metadata and controls
196 lines (174 loc) · 7.1 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright (c) 2026 Erik Darling, Darling Data LLC
*
* This file is part of the SQL Server Performance Monitor.
*
* Licensed under the MIT License. See LICENSE file in the project root for full license information.
*/
using System.Diagnostics;
using Installer.Core.Models;
using Microsoft.Data.SqlClient;
namespace Installer.Core;
/// <summary>
/// Installs community dependencies (sp_WhoIsActive, DarlingData, First Responder Kit)
/// from GitHub. Requires an HttpClient — create one instance and dispose when done.
/// </summary>
public sealed class DependencyInstaller : IDisposable
{
private readonly HttpClient _httpClient;
private bool _disposed;
public DependencyInstaller()
{
_httpClient = new HttpClient
{
Timeout = TimeSpan.FromSeconds(30)
};
}
/// <summary>
/// Install community dependencies from GitHub into the PerformanceMonitor database.
/// Returns the number of successfully installed dependencies.
/// </summary>
public async Task<int> InstallDependenciesAsync(
string connectionString,
IProgress<InstallationProgress>? progress = null,
CancellationToken cancellationToken = default)
{
var dependencies = new List<(string Name, string Url, string Description)>
{
(
"sp_WhoIsActive",
"https://raw.githubusercontent.com/amachanic/sp_whoisactive/refs/heads/master/sp_WhoIsActive.sql",
"Query activity monitoring by Adam Machanic (GPLv3)"
),
(
"DarlingData",
"https://raw.githubusercontent.com/erikdarlingdata/DarlingData/main/Install-All/DarlingData.sql",
"sp_HealthParser, sp_HumanEventsBlockViewer by Erik Darling (MIT)"
),
(
"First Responder Kit",
"https://raw.githubusercontent.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/refs/heads/main/Install-All-Scripts.sql",
"sp_BlitzLock and diagnostic tools by Brent Ozar Unlimited (MIT)"
)
};
progress?.Report(new InstallationProgress
{
Message = "Installing community dependencies...",
Status = "Info"
});
int successCount = 0;
foreach (var (name, url, description) in dependencies)
{
cancellationToken.ThrowIfCancellationRequested();
progress?.Report(new InstallationProgress
{
Message = $"Installing {name}...",
Status = "Info"
});
try
{
var depSw = Stopwatch.StartNew();
progress?.Report(new InstallationProgress { Message = $"[DEBUG] Downloading {name} from {url}", Status = "Debug" });
string sql = await DownloadWithRetryAsync(url, progress, cancellationToken: cancellationToken).ConfigureAwait(false);
progress?.Report(new InstallationProgress { Message = $"[DEBUG] {name}: downloaded {sql.Length} chars in {depSw.ElapsedMilliseconds}ms", Status = "Debug" });
if (string.IsNullOrWhiteSpace(sql))
{
progress?.Report(new InstallationProgress
{
Message = $"{name} - FAILED (empty response)",
Status = "Error"
});
continue;
}
using var connection = new SqlConnection(connectionString);
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
using (var useDbCommand = new SqlCommand("USE PerformanceMonitor;", connection))
{
await useDbCommand.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
}
string[] batches = Patterns.GoBatchSplitter.Split(sql);
int nonEmpty = batches.Count(b => !string.IsNullOrWhiteSpace(b));
progress?.Report(new InstallationProgress { Message = $"[DEBUG] {name}: executing {nonEmpty} batches", Status = "Debug" });
foreach (string batch in batches)
{
string trimmedBatch = batch.Trim();
if (string.IsNullOrWhiteSpace(trimmedBatch))
continue;
using var command = new SqlCommand(trimmedBatch, connection);
command.CommandTimeout = InstallationService.DependencyTimeoutSeconds;
await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
}
progress?.Report(new InstallationProgress
{
Message = $"{name} - Success ({description})",
Status = "Success"
});
successCount++;
}
catch (HttpRequestException ex)
{
progress?.Report(new InstallationProgress
{
Message = $"{name} - Download failed: {ex.Message}",
Status = "Error"
});
}
catch (SqlException ex)
{
progress?.Report(new InstallationProgress
{
Message = $"{name} - SQL execution failed: {ex.Message}",
Status = "Error"
});
}
catch (Exception ex)
{
progress?.Report(new InstallationProgress
{
Message = $"{name} - Failed: {ex.Message}",
Status = "Error"
});
}
}
progress?.Report(new InstallationProgress
{
Message = $"Dependencies installed: {successCount}/{dependencies.Count}",
Status = successCount == dependencies.Count ? "Success" : "Warning"
});
return successCount;
}
private async Task<string> DownloadWithRetryAsync(
string url,
IProgress<InstallationProgress>? progress = null,
int maxRetries = 3,
CancellationToken cancellationToken = default)
{
for (int attempt = 1; attempt <= maxRetries; attempt++)
{
try
{
return await _httpClient.GetStringAsync(url, cancellationToken).ConfigureAwait(false);
}
catch (HttpRequestException) when (attempt < maxRetries)
{
int delaySeconds = (int)Math.Pow(2, attempt);
progress?.Report(new InstallationProgress
{
Message = $"Network error, retrying in {delaySeconds}s ({attempt}/{maxRetries})...",
Status = "Warning"
});
await Task.Delay(delaySeconds * 1000, cancellationToken).ConfigureAwait(false);
}
}
return await _httpClient.GetStringAsync(url, cancellationToken).ConfigureAwait(false);
}
public void Dispose()
{
if (!_disposed)
{
_httpClient?.Dispose();
_disposed = true;
}
GC.SuppressFinalize(this);
}
}