9
9
using Azure . Storage . Blobs ;
10
10
using Microsoft . Azure . WebJobs . Script . WebHost . Management ;
11
11
using Microsoft . Azure . WebJobs . Script . WebHost . Models ;
12
- using Microsoft . Extensions . Hosting ;
13
12
using Microsoft . Extensions . Logging ;
14
- using Newtonsoft . Json ;
15
13
16
14
namespace Microsoft . Azure . WebJobs . Script . WebHost . ContainerManagement
17
15
{
18
- public class LinuxContainerInitializationHostService : IHostedService
16
+ public class AtlasContainerInitializationHostedService : LinuxContainerInitializationHostedService
19
17
{
20
18
private readonly IEnvironment _environment ;
21
19
private readonly IInstanceManager _instanceManager ;
22
20
private readonly ILogger _logger ;
23
21
private readonly StartupContextProvider _startupContextProvider ;
24
22
private CancellationToken _cancellationToken ;
25
23
26
- public LinuxContainerInitializationHostService ( IEnvironment environment , IInstanceManager instanceManager , ILogger < LinuxContainerInitializationHostService > logger , StartupContextProvider startupContextProvider )
24
+ public AtlasContainerInitializationHostedService ( IEnvironment environment , IInstanceManager instanceManager ,
25
+ ILogger < AtlasContainerInitializationHostedService > logger , StartupContextProvider startupContextProvider )
26
+ : base ( environment , instanceManager , logger , startupContextProvider )
27
27
{
28
28
_environment = environment ;
29
29
_instanceManager = instanceManager ;
30
30
_logger = logger ;
31
31
_startupContextProvider = startupContextProvider ;
32
32
}
33
33
34
- public async Task StartAsync ( CancellationToken cancellationToken )
34
+ protected override async Task < ( bool HasStartContext , string StartContext ) > TryGetStartContextOrNullAsync ( CancellationToken cancellationToken )
35
35
{
36
- _logger . LogInformation ( "Initializing LinuxContainerInitializationService." ) ;
37
36
_cancellationToken = cancellationToken ;
38
-
39
- // The service should be registered in Linux Consumption only, but do additional check here.
40
- if ( _environment . IsLinuxConsumptionOnAtlas ( ) )
41
- {
42
- await ApplyStartContextIfPresent ( ) ;
43
- }
44
- else if ( _environment . IsFlexConsumptionSku ( ) )
45
- {
46
- _logger . LogInformation ( "Container has (re)started. Waiting for specialization" ) ;
47
- }
48
- }
49
-
50
- private async Task ApplyStartContextIfPresent ( )
51
- {
52
- var startContext = await GetStartContextOrNullAsync ( ) ;
53
-
54
- if ( ! string . IsNullOrEmpty ( startContext ) )
55
- {
56
- _logger . LogInformation ( "Applying host context" ) ;
57
-
58
- var encryptedAssignmentContext = JsonConvert . DeserializeObject < EncryptedHostAssignmentContext > ( startContext ) ;
59
- var assignmentContext = _startupContextProvider . SetContext ( encryptedAssignmentContext ) ;
60
-
61
- var msiError = await _instanceManager . SpecializeMSISidecar ( assignmentContext ) ;
62
- if ( ! string . IsNullOrEmpty ( msiError ) )
63
- {
64
- // Log and continue specializing even in case of failures.
65
- // There will be other mechanisms to recover the container.
66
- _logger . LogError ( "MSI Specialization failed with '{msiError}'" , msiError ) ;
67
- }
68
-
69
- bool success = _instanceManager . StartAssignment ( assignmentContext ) ;
70
- _logger . LogInformation ( $ "StartAssignment invoked (Success={ success } )") ;
71
- }
72
- else
73
- {
74
- _logger . LogInformation ( "No host context specified. Waiting for host assignment" ) ;
75
- }
76
- }
77
-
78
- private async Task < string > GetStartContextOrNullAsync ( )
79
- {
80
37
var startContext = _environment . GetEnvironmentVariable ( EnvironmentSettingNames . ContainerStartContext ) ;
81
38
82
39
// Container start context is not available directly
@@ -87,16 +44,27 @@ private async Task<string> GetStartContextOrNullAsync()
87
44
88
45
if ( ! string . IsNullOrEmpty ( sasUri ) )
89
46
{
90
- _logger . LogInformation ( "Host context specified via CONTAINER_START_CONTEXT_SAS_URI " ) ;
47
+ _logger . LogDebug ( $ "Host context specified via { EnvironmentSettingNames . ContainerStartContextSasUri } ") ;
91
48
startContext = await GetAssignmentContextFromSasUri ( sasUri ) ;
92
49
}
93
50
}
94
51
else
95
52
{
96
- _logger . LogInformation ( "Host context specified via CONTAINER_START_CONTEXT " ) ;
53
+ _logger . LogDebug ( $ "Host context specified via { EnvironmentSettingNames . ContainerStartContext } ") ;
97
54
}
98
55
99
- return startContext ;
56
+ return ( ! string . IsNullOrEmpty ( startContext ) , startContext ) ;
57
+ }
58
+
59
+ protected override async Task SpecializeMSISideCar ( HostAssignmentContext assignmentContext )
60
+ {
61
+ var msiError = await _instanceManager . SpecializeMSISidecar ( assignmentContext ) ;
62
+ if ( ! string . IsNullOrEmpty ( msiError ) )
63
+ {
64
+ // Log and continue specializing even in case of failures.
65
+ // There will be other mechanisms to recover the container.
66
+ _logger . LogError ( "MSI Specialization failed with '{msiError}'" , msiError ) ;
67
+ }
100
68
}
101
69
102
70
private async Task < string > GetAssignmentContextFromSasUri ( string sasUri )
@@ -139,10 +107,5 @@ public virtual async Task<string> Read(string uri)
139
107
140
108
return string . Empty ;
141
109
}
142
-
143
- public Task StopAsync ( CancellationToken cancellationToken )
144
- {
145
- return Task . CompletedTask ;
146
- }
147
110
}
148
111
}
0 commit comments