8
8
9
9
namespace Akka . Hosting
10
10
{
11
- /// <summary>
12
- /// INTERNAL API
13
- /// </summary>
14
- internal sealed class AkkaHostedService : IHostedService
11
+ public abstract class AkkaHostedService : IHostedService
15
12
{
16
13
private ActorSystem ? _actorSystem ;
17
14
private CoordinatedShutdown ? _coordinatedShutdown ; // grab a reference to CoordinatedShutdown early
18
- private readonly IServiceProvider _serviceProvider ;
15
+
16
+ protected IServiceProvider ServiceProvider { get ; }
17
+ protected IHostApplicationLifetime HostApplicationLifetime { get ; }
18
+ protected ILogger < AkkaHostedService > Logger { get ; }
19
+
19
20
private readonly AkkaConfigurationBuilder _configurationBuilder ;
20
- private readonly IHostApplicationLifetime _hostApplicationLifetime ;
21
- private readonly ILogger < AkkaHostedService > _logger ;
22
21
23
- public AkkaHostedService ( AkkaConfigurationBuilder configurationBuilder , IServiceProvider serviceProvider ,
22
+ protected AkkaHostedService ( AkkaConfigurationBuilder configurationBuilder , IServiceProvider serviceProvider ,
24
23
ILogger < AkkaHostedService > logger , IHostApplicationLifetime applicationLifetime )
25
24
{
26
25
_configurationBuilder = configurationBuilder ;
27
- _hostApplicationLifetime = applicationLifetime ;
28
- _serviceProvider = serviceProvider ;
29
- _logger = logger ;
26
+ HostApplicationLifetime = applicationLifetime ;
27
+ ServiceProvider = serviceProvider ;
28
+ Logger = logger ;
29
+ }
30
+
31
+ protected ActorSystem ActorSystem
32
+ {
33
+ get
34
+ {
35
+ if ( _actorSystem is null )
36
+ throw new Exception ( "ActorSystem has not been initialized" ) ;
37
+ return _actorSystem ;
38
+ }
30
39
}
31
40
32
- public async Task StartAsync ( CancellationToken cancellationToken )
41
+ protected CoordinatedShutdown CoordinatedShutdown
33
42
{
34
- try
43
+ get
35
44
{
36
- _actorSystem = _serviceProvider . GetRequiredService < ActorSystem > ( ) ;
37
- _coordinatedShutdown = CoordinatedShutdown . Get ( _actorSystem ) ;
38
- await _configurationBuilder . StartAsync ( _actorSystem ) ;
45
+ if ( _coordinatedShutdown is null )
46
+ throw new Exception ( "ActorSystem has not been initialized" ) ;
47
+ return _coordinatedShutdown ;
48
+ }
49
+ }
39
50
40
- async Task TerminationHook ( )
41
- {
42
- await _actorSystem . WhenTerminated . ConfigureAwait ( false ) ;
43
- _hostApplicationLifetime . StopApplication ( ) ;
44
- }
51
+ protected bool Initialized => _coordinatedShutdown is not null ;
45
52
46
- // terminate the application if the Sys is terminated first
47
- // this can happen in instances such as Akka.Cluster membership changes
48
- #pragma warning disable CS4014
49
- TerminationHook ( ) ;
50
- #pragma warning restore CS4014
51
- }
52
- catch ( Exception ex )
53
+ protected async Task StartAkkaAsync ( CancellationToken cancellationToken )
54
+ {
55
+ _actorSystem = ServiceProvider . GetRequiredService < ActorSystem > ( ) ;
56
+ _coordinatedShutdown = CoordinatedShutdown . Get ( _actorSystem ) ;
57
+ await _configurationBuilder . StartAsync ( _actorSystem ) ;
58
+
59
+ async Task TerminationHook ( )
53
60
{
54
- _logger . Log ( LogLevel . Critical , ex , "Unable to start AkkaHostedService - shutting down application" ) ;
55
- _hostApplicationLifetime . StopApplication ( ) ;
61
+ await _actorSystem . WhenTerminated . ConfigureAwait ( false ) ;
62
+ HostApplicationLifetime . StopApplication ( ) ;
56
63
}
64
+
65
+ // terminate the application if the Sys is terminated first
66
+ // this can happen in instances such as Akka.Cluster membership changes
67
+ #pragma warning disable CS4014
68
+ TerminationHook ( ) ;
69
+ #pragma warning restore CS4014
57
70
}
58
71
59
- public async Task StopAsync ( CancellationToken cancellationToken )
72
+ protected async Task StopAkkaAsync ( CancellationToken cancellationToken )
60
73
{
61
74
// ActorSystem may have failed to start - skip shutdown sequence if that's the case
62
75
// so error message doesn't get conflated.
63
- if ( _coordinatedShutdown == null )
64
- {
76
+ if ( ! Initialized )
65
77
return ;
66
- }
67
78
68
79
// run full CoordinatedShutdown on the Sys
69
- await _coordinatedShutdown . Run ( CoordinatedShutdown . ClrExitReason . Instance )
80
+ await CoordinatedShutdown . Run ( CoordinatedShutdown . ClrExitReason . Instance )
70
81
. ConfigureAwait ( false ) ;
71
82
}
83
+
84
+ public virtual async Task StartAsync ( CancellationToken cancellationToken )
85
+ {
86
+ try
87
+ {
88
+ await StartAkkaAsync ( cancellationToken ) ;
89
+ }
90
+ catch ( Exception ex )
91
+ {
92
+ Logger . Log ( LogLevel . Critical , ex , "Unable to start AkkaHostedService - shutting down application" ) ;
93
+ HostApplicationLifetime . StopApplication ( ) ;
94
+ }
95
+ }
96
+
97
+ public virtual async Task StopAsync ( CancellationToken cancellationToken )
98
+ {
99
+ await StopAkkaAsync ( cancellationToken ) ;
100
+ }
101
+ }
102
+
103
+ /// <summary>
104
+ /// INTERNAL API
105
+ /// </summary>
106
+ internal sealed class AkkaHostedServiceImpl : AkkaHostedService
107
+ {
108
+ public AkkaHostedServiceImpl (
109
+ AkkaConfigurationBuilder configurationBuilder ,
110
+ IServiceProvider serviceProvider ,
111
+ ILogger < AkkaHostedService > logger ,
112
+ IHostApplicationLifetime applicationLifetime )
113
+ : base ( configurationBuilder , serviceProvider , logger , applicationLifetime )
114
+ { }
72
115
}
73
116
}
0 commit comments