@@ -35,6 +35,7 @@ public sealed class WebHostResolver : IDisposable
35
35
private ILoggerFactory _defaultLoggerFactory ;
36
36
private TraceWriter _defaultTraceWriter ;
37
37
private Timer _specializationTimer ;
38
+ private ILogger _logger ;
38
39
39
40
public WebHostResolver ( ScriptSettingsManager settingsManager , ISecretManagerFactory secretManagerFactory , IScriptEventManager eventManager )
40
41
{
@@ -123,74 +124,85 @@ public WebHookReceiverManager GetWebHookReceiverManager(WebHostSettings settings
123
124
/// </summary>
124
125
internal void EnsureInitialized ( WebHostSettings settings )
125
126
{
126
- lock ( _syncLock )
127
+ ILoggerFactory defaultLoggerFactory = GetDefaultLoggerFactory ( settings ) ;
128
+ _logger = defaultLoggerFactory . CreateLogger ( ScriptConstants . LogCategoryHostStartup ) ;
129
+ try
127
130
{
128
- // Determine whether we should do normal or standby initialization
129
- if ( ! WebScriptHostManager . InStandbyMode )
131
+ lock ( _syncLock )
130
132
{
131
- // We're not in standby mode. There are two cases to consider:
132
- // 1) We _were_ in standby mode and now we're ready to specialize
133
- // 2) We're doing non-specialization normal initialization
134
- if ( _activeHostManager == null &&
135
- ( _standbyHostManager == null || ( _settingsManager . ContainerReady && _settingsManager . ConfigurationReady ) ) )
133
+ // Determine whether we should do normal or standby initialization
134
+ if ( ! WebScriptHostManager . InStandbyMode )
136
135
{
137
- _specializationTimer ? . Dispose ( ) ;
138
- _specializationTimer = null ;
136
+ // We're not in standby mode. There are two cases to consider:
137
+ // 1) We _were_ in standby mode and now we're ready to specialize
138
+ // 2) We're doing non-specialization normal initialization
139
+ if ( _activeHostManager == null &&
140
+ ( _standbyHostManager == null || ( _settingsManager . ContainerReady && _settingsManager . ConfigurationReady ) ) )
141
+ {
142
+ _logger . LogDebug ( "Not in standby mode.Initializing host" ) ;
139
143
140
- _activeScriptHostConfig = CreateScriptHostConfiguration ( settings ) ;
141
- var defaultLoggerFactory = GetDefaultLoggerFactory ( settings ) ;
142
- _activeHostManager = new WebScriptHostManager ( _activeScriptHostConfig , _secretManagerFactory , _eventManager , _settingsManager , settings , defaultLoggerFactory ) ;
143
- _activeReceiverManager = new WebHookReceiverManager ( _activeHostManager . SecretManager ) ;
144
- InitializeFileSystem ( _settingsManager . FileSystemIsReadOnly ) ;
144
+ _specializationTimer ? . Dispose ( ) ;
145
+ _specializationTimer = null ;
145
146
146
- if ( _standbyHostManager != null )
147
- {
148
- // we're starting the one and only one
149
- // standby mode specialization
150
- _activeScriptHostConfig . TraceWriter . Info ( Resources . HostSpecializationTrace ) ;
147
+ _activeScriptHostConfig = CreateScriptHostConfiguration ( settings ) ;
148
+ _activeHostManager = new WebScriptHostManager ( _activeScriptHostConfig , _secretManagerFactory , _eventManager , _settingsManager , settings , defaultLoggerFactory ) ;
149
+ _activeReceiverManager = new WebHookReceiverManager ( _activeHostManager . SecretManager ) ;
150
+ InitializeFileSystem ( _settingsManager . FileSystemIsReadOnly ) ;
151
+
152
+ if ( _standbyHostManager != null )
153
+ {
154
+ // we're starting the one and only one
155
+ // standby mode specialization
156
+ _activeScriptHostConfig . TraceWriter . Info ( Resources . HostSpecializationTrace ) ;
151
157
152
- // After specialization, we need to ensure that custom timezone
153
- // settings configured by the user (WEBSITE_TIME_ZONE) are honored.
154
- // DateTime caches timezone information, so we need to clear the cache.
155
- TimeZoneInfo . ClearCachedData ( ) ;
158
+ // After specialization, we need to ensure that custom timezone
159
+ // settings configured by the user (WEBSITE_TIME_ZONE) are honored.
160
+ // DateTime caches timezone information, so we need to clear the cache.
161
+ TimeZoneInfo . ClearCachedData ( ) ;
156
162
157
- // ensure we reinitialize hostname after specialization
158
- HostNameProvider . Reset ( ) ;
159
- }
163
+ // ensure we reinitialize hostname after specialization
164
+ HostNameProvider . Reset ( ) ;
165
+ }
160
166
161
- if ( _standbyHostManager != null )
162
- {
163
- _standbyHostManager . Stop ( ) ;
164
- _standbyHostManager . Dispose ( ) ;
167
+ if ( _standbyHostManager != null )
168
+ {
169
+ _standbyHostManager . Stop ( ) ;
170
+ _standbyHostManager . Dispose ( ) ;
171
+ }
172
+ _standbyReceiverManager ? . Dispose ( ) ;
173
+ _standbyScriptHostConfig = null ;
174
+ _standbyHostManager = null ;
175
+ _standbyReceiverManager = null ;
165
176
}
166
- _standbyReceiverManager ? . Dispose ( ) ;
167
- _standbyScriptHostConfig = null ;
168
- _standbyHostManager = null ;
169
- _standbyReceiverManager = null ;
170
177
}
171
- }
172
- else
173
- {
174
- // We're in standby (placeholder) mode. Initialize the standby services.
175
- if ( _standbyHostManager == null )
178
+ else
176
179
{
177
- var standbySettings = CreateStandbySettings ( settings ) ;
178
- _standbyScriptHostConfig = CreateScriptHostConfiguration ( standbySettings , true ) ;
179
- var defaultLoggerFactory = GetDefaultLoggerFactory ( settings ) ;
180
- _standbyHostManager = new WebScriptHostManager ( _standbyScriptHostConfig , _secretManagerFactory , _eventManager , _settingsManager , standbySettings , defaultLoggerFactory ) ;
181
- _standbyReceiverManager = new WebHookReceiverManager ( _standbyHostManager . SecretManager ) ;
182
-
183
- InitializeFileSystem ( _settingsManager . FileSystemIsReadOnly ) ;
184
- StandbyManager . Initialize ( _standbyScriptHostConfig ) ;
185
-
186
- // start a background timer to identify when specialization happens
187
- // specialization usually happens via an http request (e.g. scale controller
188
- // ping) but this timer is started as well to handle cases where we
189
- // might not receive a request
190
- _specializationTimer = new Timer ( OnSpecializationTimerTick , settings , 1000 , 1000 ) ;
180
+ // We're in standby (placeholder) mode. Initialize the standby services.
181
+ if ( _standbyHostManager == null )
182
+ {
183
+ _logger . LogDebug ( "In standby mode" ) ;
184
+ var standbySettings = CreateStandbySettings ( settings ) ;
185
+ _standbyScriptHostConfig = CreateScriptHostConfiguration ( standbySettings , true ) ;
186
+ _standbyHostManager = new WebScriptHostManager ( _standbyScriptHostConfig , _secretManagerFactory , _eventManager , _settingsManager , standbySettings , defaultLoggerFactory ) ;
187
+ _standbyReceiverManager = new WebHookReceiverManager ( _standbyHostManager . SecretManager ) ;
188
+
189
+ InitializeFileSystem ( _settingsManager . FileSystemIsReadOnly ) ;
190
+ StandbyManager . Initialize ( _standbyScriptHostConfig ) ;
191
+
192
+ // start a background timer to identify when specialization happens
193
+ // specialization usually happens via an http request (e.g. scale controller
194
+ // ping) but this timer is started as well to handle cases where we
195
+ // might not receive a request
196
+ _specializationTimer = new Timer ( OnSpecializationTimerTick , settings , 1000 , 1000 ) ;
197
+ }
191
198
}
192
199
}
193
200
}
201
+ catch ( Exception initializationException )
202
+ {
203
+ var errorEventId = new EventId ( 103 , "HostInitializationError" ) ;
204
+ _logger . LogError ( errorEventId , initializationException , "Failed to initialize host" ) ;
205
+ }
194
206
}
195
207
196
208
internal static WebHostSettings CreateStandbySettings ( WebHostSettings settings )
0 commit comments