Skip to content

Commit f3b5c51

Browse files
committed
Update monitors not applying the Task.Run() requirement.
1 parent 9006767 commit f3b5c51

12 files changed

Lines changed: 117 additions & 440 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.27
1+
2.1.28

src/VirtualClient/VirtualClient.Main/profiles/MONITORS-NONE.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/VirtualClient/VirtualClient.Monitors/AmdSmiMonitor.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,25 @@ public AmdSmiMonitor(IServiceCollection dependencies, IDictionary<string, IConve
3131
}
3232

3333
/// <inheritdoc/>
34-
protected override async Task ExecuteAsync(EventContext telemetryContext, CancellationToken cancellationToken)
34+
protected override Task ExecuteAsync(EventContext telemetryContext, CancellationToken cancellationToken)
3535
{
36-
switch (this.Platform)
36+
// All background monitor ExecuteAsync methods should be either 'async' or should use a Task.Run() if running a 'while' loop or the
37+
// logic will block without returning. Monitors are typically expected to be fire-and-forget.
38+
39+
return Task.Run(async () =>
3740
{
38-
case PlatformID.Win32NT:
39-
await this.QueryGpuAsync(telemetryContext, cancellationToken)
40-
.ConfigureAwait(false);
41-
break;
42-
43-
case PlatformID.Unix:
44-
// not supported at the moment
45-
break;
46-
}
41+
switch (this.Platform)
42+
{
43+
case PlatformID.Win32NT:
44+
await this.QueryGpuAsync(telemetryContext, cancellationToken)
45+
.ConfigureAwait(false);
46+
break;
47+
48+
case PlatformID.Unix:
49+
// not supported at the moment
50+
break;
51+
}
52+
});
4753
}
4854

4955
/// <inheritdoc/>

src/VirtualClient/VirtualClient.Monitors/EventLog/LinuxEventLogMonitor.cs

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -113,57 +113,63 @@ protected static Dictionary<string, object> ConvertRecord(JObject eventRecord)
113113
/// <summary>
114114
/// Executes the monitoring operations.
115115
/// </summary>
116-
protected override async Task ExecuteAsync(EventContext telemetryContext, CancellationToken cancellationToken)
116+
protected override Task ExecuteAsync(EventContext telemetryContext, CancellationToken cancellationToken)
117117
{
118-
try
119-
{
120-
await this.WaitAsync(this.MonitorWarmupPeriod, cancellationToken);
118+
// All background monitor ExecuteAsync methods should be either 'async' or should use a Task.Run() if running a 'while' loop or the
119+
// logic will block without returning. Monitors are typically expected to be fire-and-forget.
121120

122-
long iterations = 0;
123-
while (!cancellationToken.IsCancellationRequested)
121+
return Task.Run(async () =>
122+
{
123+
try
124124
{
125-
EventContext relatedContext = telemetryContext.Clone();
125+
await this.WaitAsync(this.MonitorWarmupPeriod, cancellationToken);
126126

127-
try
127+
long iterations = 0;
128+
while (!cancellationToken.IsCancellationRequested)
128129
{
129-
if (this.IsIterationComplete(iterations))
130+
EventContext relatedContext = telemetryContext.Clone();
131+
132+
try
130133
{
131-
break;
132-
}
134+
if (this.IsIterationComplete(iterations))
135+
{
136+
break;
137+
}
133138

134-
relatedContext
135-
.AddContext("logLevel", this.LogLevel)
136-
.AddContext("lastCheckPointTime", this.LastCheckPoint)
137-
.AddContext("iteration", iterations);
139+
relatedContext
140+
.AddContext("logLevel", this.LogLevel)
141+
.AddContext("lastCheckPointTime", this.LastCheckPoint)
142+
.AddContext("iteration", iterations);
138143

139-
await this.ProcessEventsAsync(this.LastCheckPoint, relatedContext, cancellationToken);
140-
}
141-
catch (OperationCanceledException)
142-
{
143-
// Expected with Task.Delay on cancellations.
144-
}
145-
catch (Exception exc)
146-
{
147-
this.Logger.LogErrorMessage(exc, telemetryContext, LogLevel.Warning);
148-
}
149-
finally
150-
{
151-
iterations++;
144+
await this.ProcessEventsAsync(this.LastCheckPoint, relatedContext, cancellationToken);
145+
}
146+
catch (OperationCanceledException)
147+
{
148+
// Expected with Task.Delay on cancellations.
149+
}
150+
catch (Exception exc)
151+
{
152+
this.Logger.LogErrorMessage(exc, telemetryContext, LogLevel.Warning);
153+
}
154+
finally
155+
{
156+
iterations++;
152157

153-
// MUST be in local time.
154-
this.LastCheckPoint = DateTime.Now;
155-
await this.WaitAsync(this.MonitorFrequency, cancellationToken);
158+
// MUST be in local time.
159+
this.LastCheckPoint = DateTime.Now;
160+
await this.WaitAsync(this.MonitorFrequency, cancellationToken);
161+
}
156162
}
157163
}
158-
}
159-
catch (OperationCanceledException)
160-
{
161-
// Do nothing
162-
}
163-
catch (Exception exc)
164-
{
165-
this.Logger.LogErrorMessage(exc, telemetryContext, LogLevel.Warning);
166-
}
164+
catch (OperationCanceledException)
165+
{
166+
// Do nothing
167+
}
168+
catch (Exception exc)
169+
{
170+
this.Logger.LogErrorMessage(exc, telemetryContext, LogLevel.Warning);
171+
}
172+
});
167173
}
168174

169175
/// <summary>

src/VirtualClient/VirtualClient.Monitors/ExampleProfilingMonitor.cs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,34 @@ protected override void Dispose(bool disposing)
6161
/// </summary>
6262
/// <param name="telemetryContext">Provides context information that will be captured with telemetry events.</param>
6363
/// <param name="cancellationToken">A token that can be used to cancel the operation.</param>
64-
protected override async Task ExecuteAsync(EventContext telemetryContext, CancellationToken cancellationToken)
64+
protected override Task ExecuteAsync(EventContext telemetryContext, CancellationToken cancellationToken)
6565
{
6666
// All background monitor ExecuteAsync methods should be either 'async' or should use a Task.Run() if running a 'while' loop or the
6767
// logic will block without returning. Monitors are typically expected to be fire-and-forget.
6868

69-
try
69+
return Task.Run(async () =>
7070
{
71-
if (this.ProfilingEnabled && this.ProfilingMode != ProfilingMode.None)
71+
try
7272
{
73-
if (this.ProfilingMode == ProfilingMode.OnDemand)
74-
{
75-
await this.ExecuteProfilingOnDemandAsync(cancellationToken)
76-
.ConfigureAwait(false);
77-
}
78-
else if (this.ProfilingMode == ProfilingMode.Interval)
73+
if (this.ProfilingEnabled && this.ProfilingMode != ProfilingMode.None)
7974
{
80-
await this.ExecuteProfilingOnIntervalAsync(cancellationToken)
81-
.ConfigureAwait(false);
75+
if (this.ProfilingMode == ProfilingMode.OnDemand)
76+
{
77+
await this.ExecuteProfilingOnDemandAsync(cancellationToken)
78+
.ConfigureAwait(false);
79+
}
80+
else if (this.ProfilingMode == ProfilingMode.Interval)
81+
{
82+
await this.ExecuteProfilingOnIntervalAsync(cancellationToken)
83+
.ConfigureAwait(false);
84+
}
8285
}
8386
}
84-
}
85-
catch (OperationCanceledException)
86-
{
87-
// Expected when a Task.Delay is cancelled.
88-
}
87+
catch (OperationCanceledException)
88+
{
89+
// Expected when a Task.Delay is cancelled.
90+
}
91+
});
8992
}
9093

9194
private async Task ExecuteProfilingOnDemandAsync(CancellationToken cancellationToken)

src/VirtualClient/VirtualClient.Monitors/Lspci/LspciMonitor.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,25 @@ public LspciMonitor(IServiceCollection dependencies, IDictionary<string, IConver
3333
}
3434

3535
/// <inheritdoc/>
36-
protected override async Task ExecuteAsync(EventContext telemetryContext, CancellationToken cancellationToken)
36+
protected override Task ExecuteAsync(EventContext telemetryContext, CancellationToken cancellationToken)
3737
{
3838
// All background monitor ExecuteAsync methods should be either 'async' or should use a Task.Run() if running a 'while' loop or the
3939
// logic will block without returning. Monitors are typically expected to be fire-and-forget.
4040

41-
switch (this.Platform)
41+
return Task.Run(async () =>
4242
{
43-
case PlatformID.Win32NT:
44-
// Do nothing on Windows for now. The binary we are using have error:
45-
// lspci: Cannot find any working access method.
46-
break;
47-
48-
case PlatformID.Unix:
49-
await this.ListPciAsync(telemetryContext, cancellationToken);
50-
break;
51-
}
43+
switch (this.Platform)
44+
{
45+
case PlatformID.Win32NT:
46+
// Do nothing on Windows for now. The binary we are using have error:
47+
// lspci: Cannot find any working access method.
48+
break;
49+
50+
case PlatformID.Unix:
51+
await this.ListPciAsync(telemetryContext, cancellationToken);
52+
break;
53+
}
54+
});
5255
}
5356

5457
/// <inheritdoc/>

src/VirtualClient/VirtualClient.Monitors/NoMonitors.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/VirtualClient/VirtualClient.Monitors/AtopParser.cs renamed to src/VirtualClient/VirtualClient.Monitors/PerformanceCounters/AtopParser.cs

File renamed without changes.

0 commit comments

Comments
 (0)