Skip to content

Commit 9e9146e

Browse files
authored
Merge pull request #8 from dotnetcore/master
Add support for backgroundservice to extend scheduled tasks
2 parents 56af652 + c08ab7d commit 9e9146e

File tree

21 files changed

+528
-12
lines changed

21 files changed

+528
-12
lines changed

src/Surging.Core/Surging.Core.CPlatform/Support/Implementation/BreakeRemoteInvokeService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ bool reachErrorThresholdPercentage() =>
5858
if (command.BreakerForceClosed)
5959
{
6060
_serviceInvokeListenInfo.AddOrUpdate(serviceId, new ServiceInvokeListenInfo(), (k, v) => { v.LocalServiceRequests++; return v; });
61-
return null;
61+
return await MonitorRemoteInvokeAsync(parameters, serviceId, serviceKey, decodeJOject, command.ExecutionTimeoutInMilliseconds, item);
6262
}
6363
else
6464
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Logging;
3+
using Surging.Core.CPlatform.Module;
4+
using Surging.Core.CPlatform.Utilities;
5+
using Surging.Core.KestrelHttpServer;
6+
using Surging.Core.Log4net;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Text;
10+
11+
namespace Surging.Core.Kestrel.Log4net
12+
{
13+
14+
public class KestrelLog4netModule : KestrelHttpModule
15+
{
16+
private string log4NetConfigFile = "${LogPath}|log4net.config";
17+
public override void Initialize(AppModuleContext context)
18+
{
19+
20+
}
21+
22+
public override void RegisterBuilder(WebHostContext context)
23+
{
24+
}
25+
26+
public override void Initialize(ApplicationInitializationContext context)
27+
{
28+
var serviceProvider = context.Builder.ApplicationServices;
29+
base.Initialize(context);
30+
var section = CPlatform.AppConfig.GetSection("Logging");
31+
log4NetConfigFile = EnvironmentHelper.GetEnvironmentVariable(log4NetConfigFile);
32+
serviceProvider.GetService<ILoggerFactory>().AddProvider(new Log4NetProvider(log4NetConfigFile));
33+
}
34+
35+
public override void RegisterBuilder(ConfigurationContext context)
36+
{
37+
context.Services.AddLogging();
38+
}
39+
40+
protected override void RegisterBuilder(ContainerBuilderWrapper builder)
41+
{
42+
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\Surging.Core.KestrelHttpServer\Surging.Core.KestrelHttpServer.csproj" />
9+
<ProjectReference Include="..\Surging.Core.Log4net\Surging.Core.Log4net.csproj" />
10+
</ItemGroup>
11+
12+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Logging;
3+
using Surging.Core.CPlatform;
4+
using Surging.Core.CPlatform.Module;
5+
using Surging.Core.CPlatform.Utilities;
6+
using Surging.Core.KestrelHttpServer;
7+
using Surging.Core.Nlog;
8+
9+
10+
11+
namespace Surging.Core.Kestrel.Nlog
12+
{
13+
public class KestrelNLogModule : KestrelHttpModule
14+
{
15+
private string nlogConfigFile = "${LogPath}|NLog.config";
16+
public override void Initialize(AppModuleContext context)
17+
{
18+
19+
}
20+
21+
public override void RegisterBuilder(WebHostContext context)
22+
{
23+
}
24+
25+
public override void Initialize(ApplicationInitializationContext context)
26+
{
27+
var serviceProvider = context.Builder.ApplicationServices;
28+
base.Initialize(context);
29+
var section = AppConfig.GetSection("Logging");
30+
nlogConfigFile = EnvironmentHelper.GetEnvironmentVariable(nlogConfigFile);
31+
32+
NLog.LogManager.LoadConfiguration(nlogConfigFile);
33+
serviceProvider.GetService<ILoggerFactory>().AddProvider(new NLogProvider());
34+
}
35+
36+
public override void RegisterBuilder(ConfigurationContext context)
37+
{
38+
context.Services.AddLogging();
39+
}
40+
41+
protected override void RegisterBuilder(ContainerBuilderWrapper builder)
42+
{
43+
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\Surging.Core.KestrelHttpServer\Surging.Core.KestrelHttpServer.csproj" />
9+
<ProjectReference Include="..\Surging.Core.NLog\Surging.Core.Nlog.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

src/Surging.Core/Surging.Core.Protocol.Udp/Runtime/Implementation/DefaultUdpServiceEntryProvider.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class DefaultUdpServiceEntryProvider : IUdpServiceEntryProvider
1818
private readonly IEnumerable<Type> _types;
1919
private readonly ILogger<DefaultUdpServiceEntryProvider> _logger;
2020
private readonly CPlatformContainer _serviceProvider;
21-
private UdpServiceEntry _dnsServiceEntry;
21+
private UdpServiceEntry _udpServiceEntry;
2222

2323
#endregion Field
2424

@@ -44,24 +44,24 @@ public DefaultUdpServiceEntryProvider(IServiceEntryProvider serviceEntryProvider
4444
public UdpServiceEntry GetEntry()
4545
{
4646
var services = _types.ToArray();
47-
if (_dnsServiceEntry == null)
47+
if (_udpServiceEntry == null)
4848
{
49-
_dnsServiceEntry = new UdpServiceEntry();
49+
_udpServiceEntry = new UdpServiceEntry();
5050
foreach (var service in services)
5151
{
5252
var entry = CreateServiceEntry(service);
5353
if (entry != null)
5454
{
55-
_dnsServiceEntry = entry;
55+
_udpServiceEntry = entry;
5656
break;
5757
}
5858
}
5959
if (_logger.IsEnabled(LogLevel.Debug))
6060
{
61-
_logger.LogDebug($"发现了以下Udp服务:{_dnsServiceEntry.Type.FullName}。");
61+
_logger.LogDebug($"发现了以下Udp服务:{_udpServiceEntry.Type.FullName}。");
6262
}
6363
}
64-
return _dnsServiceEntry;
64+
return _udpServiceEntry;
6565
}
6666

6767
public UdpServiceEntry CreateServiceEntry(Type service)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using Autofac;
2+
using Surging.Core.CPlatform.EventBus.Events;
3+
using Surging.Core.CPlatform.EventBus.Implementation;
4+
using Surging.Core.CPlatform.Ioc;
5+
using Surging.Core.CPlatform.Utilities;
6+
using Surging.Core.ProxyGenerator;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Text;
10+
using System.Threading;
11+
using System.Threading.Tasks;
12+
13+
namespace Surging.Core.ServiceHosting.Extensions.Runtime
14+
{
15+
public abstract class BackgroundServiceBehavior : IServiceBehavior, IDisposable
16+
{
17+
private Task _executingTask;
18+
private readonly CancellationTokenSource _stoppingCts = new CancellationTokenSource();
19+
20+
public T CreateProxy<T>(string key) where T : class
21+
{
22+
return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy<T>(key);
23+
}
24+
25+
public object CreateProxy(Type type)
26+
{
27+
return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy(type);
28+
}
29+
30+
public object CreateProxy(string key, Type type)
31+
{
32+
return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy(key, type);
33+
}
34+
35+
public T CreateProxy<T>() where T : class
36+
{
37+
return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy<T>();
38+
}
39+
40+
public T GetService<T>(string key) where T : class
41+
{
42+
if (ServiceLocator.Current.IsRegisteredWithKey<T>(key))
43+
return ServiceLocator.GetService<T>(key);
44+
else
45+
return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy<T>(key);
46+
}
47+
48+
public T GetService<T>() where T : class
49+
{
50+
if (ServiceLocator.Current.IsRegistered<T>())
51+
return ServiceLocator.GetService<T>();
52+
else
53+
return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy<T>();
54+
55+
}
56+
57+
public object GetService(Type type)
58+
{
59+
if (ServiceLocator.Current.IsRegistered(type))
60+
return ServiceLocator.GetService(type);
61+
else
62+
return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy(type);
63+
}
64+
65+
public object GetService(string key, Type type)
66+
{
67+
if (ServiceLocator.Current.IsRegisteredWithKey(key, type))
68+
return ServiceLocator.GetService(key, type);
69+
else
70+
return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy(key, type);
71+
72+
}
73+
74+
public void Publish(IntegrationEvent @event)
75+
{
76+
GetService<IEventBus>().Publish(@event);
77+
}
78+
79+
protected abstract Task ExecuteAsync(CancellationToken stoppingToken);
80+
81+
public virtual Task StartAsync(CancellationToken cancellationToken)
82+
{
83+
_executingTask = ExecutingAsync(_stoppingCts.Token);
84+
85+
if (_executingTask.IsCompleted)
86+
{
87+
return _executingTask;
88+
}
89+
90+
return Task.CompletedTask;
91+
}
92+
93+
public virtual async Task StopAsync(CancellationToken cancellationToken)
94+
{
95+
if (_executingTask == null)
96+
{
97+
return;
98+
}
99+
100+
try
101+
{
102+
_stoppingCts.Cancel();
103+
}
104+
finally
105+
{
106+
await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite, cancellationToken));
107+
}
108+
109+
}
110+
111+
public virtual void Dispose()
112+
{
113+
_stoppingCts.Cancel();
114+
}
115+
116+
private async Task ExecutingAsync(CancellationToken stoppingToken)
117+
{
118+
while (!stoppingToken.IsCancellationRequested)
119+
{
120+
await ExecuteAsync(stoppingToken);
121+
}
122+
}
123+
}
124+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Surging.Core.ServiceHosting.Extensions.Runtime
6+
{
7+
public class BackgroundServiceEntry
8+
{
9+
public string Path { get; set; }
10+
11+
public Type Type { get; set; }
12+
13+
public BackgroundServiceBehavior Behavior { get; set; }
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Surging.Core.ServiceHosting.Extensions.Runtime
6+
{
7+
public interface IBackgroundServiceEntryProvider
8+
{
9+
IEnumerable<BackgroundServiceEntry> GetEntries();
10+
}
11+
}

0 commit comments

Comments
 (0)