|
7 | 7 | using System.Collections.ObjectModel;
|
8 | 8 | using System.IO;
|
9 | 9 | using System.Linq;
|
| 10 | +using System.Reactive.Linq; |
10 | 11 | using System.Text.RegularExpressions;
|
| 12 | +using System.Threading; |
11 | 13 | using Microsoft.Azure.AppService.Proxy.Client;
|
12 | 14 | using Microsoft.Azure.WebJobs.Logging;
|
13 | 15 | using Microsoft.Azure.WebJobs.Script.Description;
|
| 16 | +using Microsoft.Azure.WebJobs.Script.Eventing; |
14 | 17 | using Microsoft.Extensions.Logging;
|
15 | 18 | using Microsoft.Extensions.Options;
|
16 | 19 | using Newtonsoft.Json.Linq;
|
17 | 20 |
|
18 | 21 | namespace Microsoft.Azure.WebJobs.Script
|
19 | 22 | {
|
20 |
| - public class ProxyMetadataManager : IProxyMetadataManager |
| 23 | + public class ProxyMetadataManager : IProxyMetadataManager, IDisposable |
21 | 24 | {
|
22 | 25 | private static readonly Regex ProxyNameValidationRegex = new Regex(@"[^a-zA-Z0-9_-]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
23 |
| - private readonly Lazy<ProxyMetadataInfo> _metadata; |
| 26 | + private readonly ReaderWriterLockSlim _metadataLock = new ReaderWriterLockSlim(); |
24 | 27 | private readonly IOptionsMonitor<ScriptApplicationHostOptions> _applicationHostOptions;
|
25 | 28 | private readonly IEnvironment _environment;
|
26 | 29 | private readonly ILogger _logger;
|
| 30 | + private readonly IDisposable _fileChangeSubscription; |
| 31 | + private Lazy<ProxyMetadataInfo> _metadata; |
| 32 | + private bool _disposed = false; |
27 | 33 |
|
28 |
| - public ProxyMetadataManager(IOptionsMonitor<ScriptApplicationHostOptions> applicationHostOptions, IEnvironment environment, ILoggerFactory loggerFactory) |
| 34 | + public ProxyMetadataManager(IOptionsMonitor<ScriptApplicationHostOptions> applicationHostOptions, IEnvironment environment, IScriptEventManager eventManager, ILoggerFactory loggerFactory) |
29 | 35 | {
|
30 | 36 | _applicationHostOptions = applicationHostOptions;
|
31 | 37 | _environment = environment;
|
32 | 38 | _logger = loggerFactory.CreateLogger(LogCategories.Startup);
|
33 | 39 | _metadata = new Lazy<ProxyMetadataInfo>(LoadFunctionMetadata);
|
| 40 | + |
| 41 | + _fileChangeSubscription = eventManager.OfType<FileEvent>() |
| 42 | + .Where(f => string.Equals(f.Source, EventSources.ScriptFiles, StringComparison.Ordinal) && |
| 43 | + string.Equals(Path.GetFileName(f.FileChangeArguments.Name), ScriptConstants.ProxyMetadataFileName, StringComparison.OrdinalIgnoreCase)) |
| 44 | + .Subscribe(e => HandleProxyFileChange()); |
34 | 45 | }
|
35 | 46 |
|
36 |
| - public ProxyMetadataInfo ProxyMetadata => _metadata.Value; |
| 47 | + public ProxyMetadataInfo ProxyMetadata |
| 48 | + { |
| 49 | + get |
| 50 | + { |
| 51 | + _metadataLock.EnterReadLock(); |
| 52 | + try |
| 53 | + { |
| 54 | + return _metadata.Value; |
| 55 | + } |
| 56 | + finally |
| 57 | + { |
| 58 | + _metadataLock.ExitReadLock(); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private void HandleProxyFileChange() |
| 64 | + { |
| 65 | + _metadataLock.EnterWriteLock(); |
| 66 | + try |
| 67 | + { |
| 68 | + if (_metadata.IsValueCreated) |
| 69 | + { |
| 70 | + _metadata = new Lazy<ProxyMetadataInfo>(LoadFunctionMetadata); |
| 71 | + } |
| 72 | + } |
| 73 | + finally |
| 74 | + { |
| 75 | + _metadataLock.ExitWriteLock(); |
| 76 | + } |
| 77 | + } |
37 | 78 |
|
38 | 79 | private ProxyMetadataInfo LoadFunctionMetadata()
|
39 | 80 | {
|
@@ -137,5 +178,23 @@ internal static string NormalizeProxyName(string name)
|
137 | 178 | {
|
138 | 179 | return ProxyNameValidationRegex.Replace(name, string.Empty);
|
139 | 180 | }
|
| 181 | + |
| 182 | + protected virtual void Dispose(bool disposing) |
| 183 | + { |
| 184 | + if (!_disposed) |
| 185 | + { |
| 186 | + if (disposing) |
| 187 | + { |
| 188 | + _fileChangeSubscription?.Dispose(); |
| 189 | + } |
| 190 | + |
| 191 | + _disposed = true; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + public void Dispose() |
| 196 | + { |
| 197 | + Dispose(true); |
| 198 | + } |
140 | 199 | }
|
141 | 200 | }
|
0 commit comments