Skip to content

Commit 4f6b5f2

Browse files
committed
Fixed service init order
1 parent a632a9a commit 4f6b5f2

File tree

4 files changed

+72
-10
lines changed

4 files changed

+72
-10
lines changed

src/ITGlobal.MarkDocs.Blog/Impl/BlogEngineImpl.cs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,69 @@ namespace ITGlobal.MarkDocs.Blog.Impl
88
{
99
internal sealed class BlogEngineImpl : IBlogEngine
1010
{
11+
1112
private readonly IMarkDocService _markDocs;
1213
private readonly IMarkDocsLog _log;
1314

15+
private readonly object _stateLock = new object();
1416
private BlogEngineState _state;
17+
private bool _isDisposed;
1518

1619
public BlogEngineImpl(IMarkDocService service, IMarkDocsLog log, EventConnector eventConnector)
1720
{
1821
_markDocs = service;
1922
_log = log;
2023
eventConnector.Engine = this;
2124

22-
Update(service.Documentations[0]);
25+
if (service.Documentations.Count > 0)
26+
{
27+
if (service.Documentations.Count > 1)
28+
{
29+
_log.Error($"More than one source found. Choosing \"{service.Documentations[0].Id}\" arbitrarily");
30+
}
31+
32+
Update(service.Documentations[0]);
33+
}
2334
}
2435

2536
private BlogEngineState State
2637
{
2738
get
2839
{
29-
return Interlocked.CompareExchange(ref _state, null, null);
40+
lock (_stateLock)
41+
{
42+
while (_state == null && !_isDisposed)
43+
{
44+
Monitor.Wait(_stateLock);
45+
}
46+
47+
if (_isDisposed)
48+
{
49+
throw new ObjectDisposedException(nameof(BlogEngineImpl));
50+
}
51+
52+
return _state;
53+
}
3054
}
3155
set
3256
{
33-
Interlocked.Exchange(ref _state, value);
57+
lock (_stateLock)
58+
{
59+
if (_isDisposed)
60+
{
61+
throw new ObjectDisposedException(nameof(BlogEngineImpl));
62+
}
63+
64+
_state = value;
65+
Monitor.PulseAll(_stateLock);
66+
}
3467

3568
_log.Info($"Blog data updated to version \"{value.SourceInfo.LastChangeId}\"");
3669
}
3770
}
3871

3972
#region IBlogEngine
40-
73+
4174
/// <summary>
4275
/// Blog version
4376
/// </summary>
@@ -100,6 +133,12 @@ public void Synchronize()
100133
/// <inheritdoc />
101134
void IDisposable.Dispose()
102135
{
136+
lock (_stateLock)
137+
{
138+
_isDisposed = true;
139+
Monitor.PulseAll(_stateLock);
140+
}
141+
103142
_markDocs.Dispose();
104143
}
105144

@@ -113,6 +152,7 @@ internal void Update(IMarkDocState state)
113152
if (state.List.Length > 1)
114153
{
115154
report.Error($"More than one source found. Choosing \"{state.List[0].Id}\" arbitrarily");
155+
_log.Error($"More than one source found. Choosing \"{state.List[0].Id}\" arbitrarily");
116156
}
117157

118158
State = new BlogEngineState(this, state.List[0], report);
@@ -125,5 +165,6 @@ internal void Update(IDocumentation documentation)
125165
}
126166

127167
#endregion
168+
128169
}
129-
}
170+
}

src/ITGlobal.MarkDocs.Core/MarkDocsOptions.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ITGlobal.MarkDocs.Cache;
1+
using ITGlobal.MarkDocs.Cache;
22
using ITGlobal.MarkDocs.Extensions;
33
using ITGlobal.MarkDocs.Format;
44
using ITGlobal.MarkDocs.Impl;
@@ -19,8 +19,12 @@ namespace ITGlobal.MarkDocs
1919
[PublicAPI]
2020
public sealed class MarkDocsOptions
2121
{
22+
2223
private readonly IServiceCollection _services;
2324

25+
private readonly List<Action<IServiceCollection>> _configureServicesActions =
26+
new List<Action<IServiceCollection>>();
27+
2428
internal MarkDocsOptions(IServiceCollection services)
2529
{
2630
_services = services;
@@ -148,7 +152,7 @@ public MarkDocsOptions UseLog([NotNull] Func<IServiceProvider, IMarkDocsLog> log
148152
[NotNull]
149153
internal MarkDocsOptions ConfigureServices([NotNull] Action<IServiceCollection> action)
150154
{
151-
action(_services);
155+
_configureServicesActions.Add(action);
152156
return this;
153157
}
154158

@@ -174,8 +178,13 @@ internal void Configure()
174178
throw new Exception("No cache service has been configured");
175179
}
176180

181+
foreach (var action in _configureServicesActions)
182+
{
183+
action(_services);
184+
}
185+
177186
_services.AddSingleton<IMarkDocService, MarkDocService>();
178-
if (ConfigureCustomAssetTreeReader!= null)
187+
if (ConfigureCustomAssetTreeReader != null)
179188
{
180189
ConfigureCustomAssetTreeReader(_services);
181190
}
@@ -210,5 +219,6 @@ IEnumerable<IExtensionFactory> CreateExtensionFactories()
210219
}
211220
}
212221
}
222+
213223
}
214224
}

src/ITGlobal.MarkDocs.DirectorySource/Source/Impl/StaticDirectorySourceTreeProvider.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public StaticDirectorySourceTreeProvider(StaticDirectorySourceTreeOptions option
2222
_options = options;
2323
_reader = reader;
2424

25-
Refresh();
25+
Refresh(throwIfDirectoryIsMissing: true);
2626
}
2727

2828
public string[] IgnorePatterns => null;
@@ -48,6 +48,11 @@ public ISourceTree GetSourceTree(string id)
4848
}
4949

5050
public void Refresh()
51+
{
52+
Refresh(throwIfDirectoryIsMissing: false);
53+
}
54+
55+
private void Refresh(bool throwIfDirectoryIsMissing)
5156
{
5257
var hasAnyChanges = false;
5358
ImmutableDictionary<string, StaticDirectorySourceTree> sourceTrees;
@@ -81,6 +86,13 @@ public void Refresh()
8186

8287
hasAnyChanges = true;
8388
}
89+
else
90+
{
91+
if (throwIfDirectoryIsMissing)
92+
{
93+
throw new Exception($"Source directory \"{directory}\" doesn't exist");
94+
}
95+
}
8496
}
8597

8698
foreach (var id in toBeDeleted)

src/ITGlobal.MarkDocs.Markdown/Format/Impl/Extensions/HighlightJs/HighlightJsWorker.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public HighlightJsWorker(HighlightJsOptions options, IMarkDocsLog log)
3838

3939
Task.Run(Initialize);
4040

41-
4241
void Initialize()
4342
{
4443
try

0 commit comments

Comments
 (0)