-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAzureFunctionProcess.cs
More file actions
105 lines (93 loc) · 3.15 KB
/
Copy pathAzureFunctionProcess.cs
File metadata and controls
105 lines (93 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Diagnostics;
using System.Threading;
namespace AdaTheDev.AzureFunctionsE2ETests
{
public class AzureFunctionProcess : IDisposable
{
private Process _funcHostProcess;
private bool disposed;
private bool _funcHostIsReady;
private readonly bool _useShellExecute;
public AzureFunctionProcess(
string dotnetExePath,
string functionHostPath,
string functionAppFolder,
int port,
bool useShellExecute = false)
{
_funcHostProcess = new Process
{
StartInfo =
{
FileName = dotnetExePath,
Arguments = $"\"{functionHostPath}\" start -p {port}",
WorkingDirectory = functionAppFolder,
RedirectStandardOutput = !useShellExecute,
UseShellExecute = useShellExecute
}
};
_useShellExecute = useShellExecute;
}
public void Start(int timeoutSeconds = 15)
{
_funcHostProcess.OutputDataReceived += _funcHostProcess_OutputDataReceived;
try
{
_funcHostProcess.Start();
var stopwatch = Stopwatch.StartNew();
if (!_useShellExecute)
{
_funcHostProcess.BeginOutputReadLine();
while (!_funcHostProcess.HasExited && !_funcHostIsReady && stopwatch.ElapsedMilliseconds < (timeoutSeconds * 1000))
{
Thread.Sleep(1000);
}
}
else
{
Thread.Sleep(timeoutSeconds * 1000);
_funcHostIsReady = true;
}
}
finally
{
_funcHostProcess.OutputDataReceived -= _funcHostProcess_OutputDataReceived;
}
if (!_funcHostIsReady)
{
throw new InvalidOperationException("The Azure Functions host did not start up within an acceptable time.");
}
}
private void _funcHostProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data?.Contains("For detailed output, run func with --verbose flag") ?? false)
{
_funcHostIsReady = true;
}
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
if (_funcHostProcess != null)
{
if (!_funcHostProcess.HasExited)
{
_funcHostProcess.Kill();
}
_funcHostProcess.Dispose();
}
}
disposed = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}