12
12
using Microsoft . Azure . WebJobs . Script . Description ;
13
13
using Microsoft . Azure . WebJobs . Script . Diagnostics ;
14
14
using Microsoft . Azure . WebJobs . Script . Eventing ;
15
+ using Microsoft . Azure . WebJobs . Script . Extensions ;
15
16
using Microsoft . Extensions . Logging ;
16
17
using Microsoft . Extensions . Options ;
17
18
@@ -68,16 +69,12 @@ internal HttpFunctionInvocationDispatcher()
68
69
69
70
public int ErrorEventsThreshold { get ; private set ; }
70
71
71
- internal Task InitializeHttpWorkerChannelAsync ( int attemptCount , CancellationToken cancellationToken = default )
72
+ internal async Task InitializeHttpWorkerChannelAsync ( int attemptCount , CancellationToken cancellationToken = default )
72
73
{
73
74
_httpWorkerChannel = _httpWorkerChannelFactory . Create ( _scriptOptions . RootScriptPath , _metricsLogger , attemptCount ) ;
74
- _httpWorkerChannel . StartWorkerProcessAsync ( cancellationToken ) . ContinueWith ( workerInitTask =>
75
- {
76
- _logger . LogDebug ( "Adding http worker channel. workerId:{id}" , _httpWorkerChannel . Id ) ;
77
- SetFunctionDispatcherStateToInitializedAndLog ( ) ;
78
- } , TaskContinuationOptions . OnlyOnRanToCompletion ) ;
79
-
80
- return Task . CompletedTask ;
75
+ await _httpWorkerChannel . StartWorkerProcessAsync ( cancellationToken ) ;
76
+ _logger . LogDebug ( "Adding http worker channel. workerId:{id}" , _httpWorkerChannel . Id ) ;
77
+ SetFunctionDispatcherStateToInitializedAndLog ( ) ;
81
78
}
82
79
83
80
private void SetFunctionDispatcherStateToInitializedAndLog ( )
@@ -86,41 +83,42 @@ private void SetFunctionDispatcherStateToInitializedAndLog()
86
83
_logger . LogInformation ( "Worker process started and initialized." ) ;
87
84
}
88
85
89
- public async Task InitializeAsync ( IEnumerable < FunctionMetadata > functions , CancellationToken cancellationToken = default )
86
+ public Task InitializeAsync ( IEnumerable < FunctionMetadata > functions , CancellationToken cancellationToken = default )
90
87
{
91
88
cancellationToken . ThrowIfCancellationRequested ( ) ;
92
89
93
90
if ( functions == null || ! functions . Any ( ) )
94
91
{
95
92
// do not initialize function dispachter if there are no functions
96
- return ;
93
+ return Task . CompletedTask ;
97
94
}
98
95
99
96
State = FunctionInvocationDispatcherState . Initializing ;
100
- await InitializeHttpWorkerChannelAsync ( 0 , cancellationToken ) ;
97
+ InitializeHttpWorkerChannelAsync ( 0 , cancellationToken ) . Forget ( ) ;
98
+ return Task . CompletedTask ;
101
99
}
102
100
103
101
public Task InvokeAsync ( ScriptInvocationContext invocationContext )
104
102
{
105
103
return _httpWorkerChannel . InvokeAsync ( invocationContext ) ;
106
104
}
107
105
108
- public async void WorkerError ( HttpWorkerErrorEvent workerError )
106
+ public void WorkerError ( HttpWorkerErrorEvent workerError )
109
107
{
110
108
if ( ! _disposing )
111
109
{
112
110
_logger . LogDebug ( "Handling WorkerErrorEvent for workerId:{workerId}. Failed with: {exception}" , workerError . WorkerId , workerError . Exception ) ;
113
111
AddOrUpdateErrorBucket ( workerError ) ;
114
- await DisposeAndRestartWorkerChannel ( workerError . WorkerId ) ;
112
+ DisposeAndRestartWorkerChannel ( workerError . WorkerId ) ;
115
113
}
116
114
}
117
115
118
- public async void WorkerRestart ( HttpWorkerRestartEvent workerRestart )
116
+ public void WorkerRestart ( HttpWorkerRestartEvent workerRestart )
119
117
{
120
118
if ( ! _disposing )
121
119
{
122
120
_logger . LogDebug ( "Handling WorkerRestartEvent for workerId:{workerId}" , workerRestart . WorkerId ) ;
123
- await DisposeAndRestartWorkerChannel ( workerRestart . WorkerId ) ;
121
+ DisposeAndRestartWorkerChannel ( workerRestart . WorkerId ) ;
124
122
}
125
123
}
126
124
@@ -130,7 +128,7 @@ public Task StartWorkerChannel()
130
128
return Task . CompletedTask ;
131
129
}
132
130
133
- private async Task DisposeAndRestartWorkerChannel ( string workerId )
131
+ private void DisposeAndRestartWorkerChannel ( string workerId )
134
132
{
135
133
// Since we only have one HTTP worker process, as soon as we dispose it, InvokeAsync will fail. Set state to
136
134
// indicate we are not ready to receive new requests.
@@ -140,15 +138,16 @@ private async Task DisposeAndRestartWorkerChannel(string workerId)
140
138
{
141
139
( _httpWorkerChannel as IDisposable ) ? . Dispose ( ) ;
142
140
}
143
- await RestartWorkerChannel ( workerId ) ;
141
+
142
+ RestartWorkerChannel ( workerId ) ;
144
143
}
145
144
146
- private async Task RestartWorkerChannel ( string workerId )
145
+ private void RestartWorkerChannel ( string workerId )
147
146
{
148
147
if ( _invokerErrors . Count < ErrorEventsThreshold )
149
148
{
150
149
_logger . LogDebug ( "Restarting http invoker channel" ) ;
151
- await InitializeHttpWorkerChannelAsync ( _invokerErrors . Count ) ;
150
+ InitializeHttpWorkerChannelAsync ( _invokerErrors . Count ) . Forget ( ) ;
152
151
}
153
152
else
154
153
{
@@ -207,10 +206,11 @@ public Task ShutdownAsync()
207
206
return Task . CompletedTask ;
208
207
}
209
208
210
- public async Task < bool > RestartWorkerWithInvocationIdAsync ( string invocationId )
209
+ public Task < bool > RestartWorkerWithInvocationIdAsync ( string invocationId )
211
210
{
212
- await DisposeAndRestartWorkerChannel ( _httpWorkerChannel . Id ) ; // Since there's only one channel for httpworker
213
- return true ;
211
+ // Since there's only one channel for httpworker
212
+ DisposeAndRestartWorkerChannel ( _httpWorkerChannel . Id ) ;
213
+ return Task . FromResult ( true ) ;
214
214
}
215
215
216
216
public void PreShutdown ( )
0 commit comments