2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
+ using System . Threading ;
5
6
using System . Threading . Tasks ;
6
7
using Microsoft . Azure . WebJobs . Script . Description ;
7
8
using Microsoft . Azure . WebJobs . Script . Diagnostics ;
8
- using Microsoft . Azure . WebJobs . Script . Workers . Rpc ;
9
+ using Microsoft . Azure . WebJobs . Script . Eventing ;
9
10
using Microsoft . Extensions . Logging ;
10
11
11
12
namespace Microsoft . Azure . WebJobs . Script . Workers
12
13
{
13
14
public class HttpWorkerChannel : IHttpWorkerChannel , IDisposable
14
15
{
16
+ private readonly IScriptEventManager _eventManager ;
15
17
private bool _disposed ;
16
18
private IDisposable _startLatencyMetric ;
17
19
private ILogger _workerChannelLogger ;
18
- private IWorkerProcess _rpcWorkerProcess ;
20
+ private IWorkerProcess _workerProcess ;
19
21
private IHttpWorkerService _httpWorkerService ;
20
22
21
23
internal HttpWorkerChannel (
22
24
string workerId ,
25
+ IScriptEventManager eventManager ,
23
26
IWorkerProcess rpcWorkerProcess ,
24
27
IHttpWorkerService httpWorkerService ,
25
28
ILogger logger ,
26
29
IMetricsLogger metricsLogger ,
27
30
int attemptCount )
28
31
{
29
32
Id = workerId ;
30
- _rpcWorkerProcess = rpcWorkerProcess ;
33
+ _eventManager = eventManager ;
34
+ _workerProcess = rpcWorkerProcess ;
31
35
_workerChannelLogger = logger ;
32
36
_httpWorkerService = httpWorkerService ;
33
37
_startLatencyMetric = metricsLogger ? . LatencyEvent ( string . Format ( MetricEventNames . WorkerInitializeLatency , "HttpWorker" , attemptCount ) ) ;
@@ -40,10 +44,42 @@ public Task InvokeFunction(ScriptInvocationContext context)
40
44
return _httpWorkerService . InvokeAsync ( context ) ;
41
45
}
42
46
43
- public async Task StartWorkerProcessAsync ( )
47
+ internal async Task DelayUntilWokerInitialized ( CancellationToken cancellationToken )
48
+ {
49
+ _workerChannelLogger . LogDebug ( "Initializing HttpWorker." ) ;
50
+ try
51
+ {
52
+ bool isWorkerReady = await _httpWorkerService . IsWorkerReady ( cancellationToken ) ;
53
+ if ( ! isWorkerReady )
54
+ {
55
+ PublishWorkerErrorEvent ( new TimeoutException ( "Initializing HttpWorker timed out." ) ) ;
56
+ }
57
+ else
58
+ {
59
+ _workerChannelLogger . LogDebug ( "HttpWorker is Initialized." ) ;
60
+ }
61
+ }
62
+ catch ( Exception ex )
63
+ {
64
+ // HttpFunctionInvocationDispatcher will handdle the worker error events
65
+ PublishWorkerErrorEvent ( ex ) ;
66
+ }
67
+ }
68
+
69
+ public async Task StartWorkerProcessAsync ( CancellationToken cancellationToken )
44
70
{
45
71
_workerChannelLogger . LogDebug ( "Initiating Worker Process start up" ) ;
46
- await _rpcWorkerProcess . StartProcessAsync ( ) ;
72
+ await _workerProcess . StartProcessAsync ( ) ;
73
+ await DelayUntilWokerInitialized ( cancellationToken ) ;
74
+ }
75
+
76
+ private void PublishWorkerErrorEvent ( Exception exc )
77
+ {
78
+ if ( _disposed )
79
+ {
80
+ return ;
81
+ }
82
+ _eventManager . Publish ( new HttpWorkerErrorEvent ( Id , exc ) ) ;
47
83
}
48
84
49
85
protected virtual void Dispose ( bool disposing )
@@ -54,7 +90,7 @@ protected virtual void Dispose(bool disposing)
54
90
{
55
91
_startLatencyMetric ? . Dispose ( ) ;
56
92
57
- ( _rpcWorkerProcess as IDisposable ) ? . Dispose ( ) ;
93
+ ( _workerProcess as IDisposable ) ? . Dispose ( ) ;
58
94
}
59
95
_disposed = true ;
60
96
}
0 commit comments