forked from dotnet/dotnet-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManualTrigger.cs
93 lines (74 loc) · 2.83 KB
/
ManualTrigger.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Diagnostics.Monitoring.WebApi;
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Configuration;
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Triggers;
using Microsoft.Extensions.Configuration;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Diagnostics.Monitoring.Tool.UnitTests.CollectionRules.Triggers
{
internal sealed class ManualTriggerFactory : ICollectionRuleTriggerFactory
{
private readonly ManualTriggerService _service;
public ManualTriggerFactory(ManualTriggerService service)
{
_service = service ?? throw new ArgumentNullException(nameof(service));
}
public ICollectionRuleTrigger Create(IEndpointInfo endpointInfo, Action callback)
{
return new ManualTrigger(_service, callback);
}
}
internal sealed class ManualTrigger : ICollectionRuleTrigger
{
public const string TriggerName = nameof(ManualTrigger);
private readonly Action _callback;
private readonly ManualTriggerService _service;
public ManualTrigger(ManualTriggerService service, Action callback)
{
_callback = callback ?? throw new ArgumentNullException(nameof(callback));
_service = service ?? throw new ArgumentNullException(nameof(service));
}
public Task StartAsync(CancellationToken cancellationToken)
{
_service.NotifyTrigger += NotifyHandler;
_service.NotifyStartedSubscribers();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_service.NotifyTrigger -= NotifyHandler;
return Task.CompletedTask;
}
private void NotifyHandler(object sender, EventArgs args)
{
_callback();
}
}
internal sealed class ManualTriggerDescriptor : ICollectionRuleTriggerDescriptor
{
public string TriggerName => ManualTrigger.TriggerName;
public Type FactoryType => typeof(ManualTriggerFactory);
public Type OptionsType => null;
public bool TryBindOptions(IConfigurationSection settingsSection, out object settings)
{
settings = null;
return false;
}
}
internal sealed class ManualTriggerService
{
public event EventHandler NotifyStarted;
public event EventHandler NotifyTrigger;
public void NotifyStartedSubscribers()
{
NotifyStarted?.Invoke(this, EventArgs.Empty);
}
public void NotifyTriggerSubscribers()
{
NotifyTrigger?.Invoke(this, EventArgs.Empty);
}
}
}