Skip to content

Commit 6e4a933

Browse files
committed
Makes a few things public, adds logic to rebuild indexes/content cache when the server's synced file is missing.
1 parent 4fb63c3 commit 6e4a933

5 files changed

+84
-22
lines changed

src/Umbraco.Core/Sync/DatabaseServerMessenger.cs

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Threading;
77
using System.Web.Script.Serialization;
8+
using log4net;
89
using Newtonsoft.Json;
910
using Newtonsoft.Json.Linq;
1011
using Umbraco.Core.Cache;
@@ -17,10 +18,15 @@
1718

1819
namespace Umbraco.Core.Sync
1920
{
20-
internal class DatabaseServerMessenger : DefaultServerMessenger
21+
public class DatabaseServerMessenger : DefaultServerMessenger
2122
{
2223
private readonly ApplicationContext _appContext;
2324
private readonly DatabaseServerMessengerOptions _options;
25+
private readonly object _lock = new object();
26+
private int _lastId = -1;
27+
private volatile bool _syncing = false;
28+
private long _lastUtcTicks;
29+
private bool _initialized = false;
2430

2531
public DatabaseServerMessenger(ApplicationContext appContext, bool enableDistCalls, DatabaseServerMessengerOptions options)
2632
: base(() => enableDistCalls
@@ -33,23 +39,9 @@ public DatabaseServerMessenger(ApplicationContext appContext, bool enableDistCal
3339
_appContext = appContext;
3440
_options = options;
3541
_lastUtcTicks = DateTime.UtcNow.Ticks;
36-
37-
ReadLastSynced();
38-
39-
//TODO: we need to make sure we can read from the db here!
40-
41-
//if there's been nothing sync, perform a first sync, this will store the latest id
42-
if (_lastId == -1)
43-
{
44-
FirstSync();
45-
}
42+
UmbracoApplicationBase.ApplicationStarted += OnApplicationStarted;
4643
}
4744

48-
private readonly object _lock = new object();
49-
private int _lastId = -1;
50-
private volatile bool _syncing = false;
51-
private long _lastUtcTicks;
52-
5345
/// <summary>
5446
/// A check to see if a distributed call should be made or only to refresh on the single instance
5547
/// </summary>
@@ -59,8 +51,13 @@ public DatabaseServerMessenger(ApplicationContext appContext, bool enableDistCal
5951
/// <returns></returns>
6052
protected override bool ShouldMakeDistributedCall(IEnumerable<IServerAddress> servers, ICacheRefresher refresher, MessageType dispatchType)
6153
{
62-
//we don't care if there's servers listed or not, if distributed call is enabled we will make the call
54+
55+
if (_initialized == false)
56+
{
57+
return false;
58+
}
6359

60+
//we don't care if there's servers listed or not, if distributed call is enabled we will make the call
6461
return UseDistributedCalls;
6562
}
6663

@@ -102,7 +99,18 @@ internal void FirstSync()
10299
//we haven't synced - in this case we aren't going to sync the whole thing, we will assume this is a new
103100
// server and it will need to rebuild it's own persisted cache. Currently in that case it is Lucene and the xml
104101
// cache file.
105-
LogHelper.Warn<DatabaseServerMessenger>("No last synced Id found, this generally means this is a new server/install. The server will adjust it's last synced id to the latest found in the database and will start maintaining cache updates based on that id");
102+
LogHelper.Warn<DatabaseServerMessenger>("No last synced Id found, this generally means this is a new server/install. The server will rebuild its caches and indexes and then adjust it's last synced id to the latest found in the database and will start maintaining cache updates based on that id");
103+
104+
//perform rebuilds if specified
105+
if (_options.RebuildingCallbacks != null)
106+
{
107+
foreach (var callback in _options.RebuildingCallbacks)
108+
{
109+
callback();
110+
}
111+
}
112+
113+
106114
//go get the last id in the db and store it
107115
var lastId = _appContext.DatabaseContext.Database.ExecuteScalar<int>(
108116
"SELECT MAX(id) FROM umbracoCacheInstruction");
@@ -253,6 +261,7 @@ private void SaveLastSynced(int id)
253261
File.WriteAllText(Path.Combine(tempFolder, NetworkHelper.FileSafeMachineName + "-lastsynced.txt"), id.ToString(CultureInfo.InvariantCulture));
254262
}
255263

264+
256265
#region Updates the refreshers
257266
private void RefreshAll(Guid uniqueIdentifier)
258267
{
@@ -300,5 +309,29 @@ private void RemoveById(Guid uniqueIdentifier, int Id)
300309
cr.Remove(Id);
301310
}
302311
#endregion
312+
313+
/// <summary>
314+
/// Ensure we can connect to the db and initialize
315+
/// </summary>
316+
public void OnApplicationStarted(object sender, EventArgs eventArgs)
317+
{
318+
if (_appContext.IsConfigured && _appContext.DatabaseContext.IsDatabaseConfigured && _appContext.DatabaseContext.CanConnect)
319+
{
320+
ReadLastSynced();
321+
322+
//if there's been nothing sync, perform a first sync, this will store the latest id
323+
if (_lastId == -1)
324+
{
325+
FirstSync();
326+
}
327+
328+
_initialized = true;
329+
}
330+
else
331+
{
332+
LogHelper.Warn<DatabaseServerMessenger>("The app is not configured or cannot connect to the database, this server cannot be initialized with " + typeof(DatabaseServerMessenger) + ", distributed calls will not be enabled for this server");
333+
}
334+
335+
}
303336
}
304337
}

src/Umbraco.Core/Sync/DatabaseServerMessengerOptions.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
15
namespace Umbraco.Core.Sync
26
{
3-
internal class DatabaseServerMessengerOptions
7+
public class DatabaseServerMessengerOptions
48
{
59
public DatabaseServerMessengerOptions()
610
{
711
DaysToRetainInstructionRecords = 100;
812
ThrottleSeconds = 5;
913
}
1014

15+
/// <summary>
16+
/// A list of callbacks that will be invoked if the lastsynced.txt file does not exist
17+
/// </summary>
18+
/// <remarks>
19+
/// These callbacks will typically be for rebuilding the xml cache file and examine indexes based on the data in the database
20+
/// to get this particular server node up to date.
21+
/// </remarks>
22+
public IEnumerable<Action> RebuildingCallbacks { get; set; }
23+
24+
/// <summary>
25+
/// The number of days to keep instructions in the db table, any records older than this number will be pruned.
26+
/// </summary>
1127
public int DaysToRetainInstructionRecords { get; set; }
1228

1329
/// <summary>

src/Umbraco.Core/Sync/DefaultServerMessenger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Umbraco.Core.Sync
1616
/// <summary>
1717
/// The default server messenger that uses web services to keep servers in sync
1818
/// </summary>
19-
internal class DefaultServerMessenger : IServerMessenger
19+
public class DefaultServerMessenger : IServerMessenger
2020
{
2121
private readonly Func<Tuple<string, string>> _getUserNamePasswordDelegate;
2222
private volatile bool _hasResolvedDelegate = false;

src/Umbraco.Web/BatchedDatabaseServerMessenger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Umbraco.Web
1313
{
14-
internal class BatchedDatabaseServerMessenger : DatabaseServerMessenger
14+
public class BatchedDatabaseServerMessenger : DatabaseServerMessenger
1515
{
1616
public BatchedDatabaseServerMessenger(ApplicationContext appContext, bool enableDistCalls, DatabaseServerMessengerOptions options)
1717
: base(appContext, enableDistCalls, options)

src/Umbraco.Web/WebBootManager.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Web.Routing;
1212
using ClientDependency.Core.Config;
1313
using Examine;
14+
using umbraco;
1415
using Umbraco.Core;
1516
using Umbraco.Core.Configuration;
1617
using Umbraco.Core.Dictionary;
@@ -37,6 +38,7 @@
3738
using Umbraco.Web.UI.JavaScript;
3839
using Umbraco.Web.WebApi;
3940
using umbraco.BusinessLogic;
41+
using GlobalSettings = Umbraco.Core.Configuration.GlobalSettings;
4042
using ProfilingViewEngine = Umbraco.Core.Profiling.ProfilingViewEngine;
4143

4244

@@ -324,7 +326,18 @@ protected override void InitializeResolvers()
324326
ServerMessengerResolver.Current.SetServerMessenger(new BatchedDatabaseServerMessenger(
325327
ApplicationContext,
326328
UmbracoConfig.For.UmbracoSettings().DistributedCall.Enabled,
327-
new DatabaseServerMessengerOptions()));
329+
new DatabaseServerMessengerOptions
330+
{
331+
RebuildingCallbacks = new Action[]
332+
{
333+
//rebuild the xml cache file if the server is not synced
334+
() => content.Instance.RefreshContentFromDatabase(),
335+
//rebuild indexes if the server is not synced
336+
// NOTE: This will rebuild ALL indexes including the members, if developers want to target specific
337+
// indexes then they can adjust this logic themselves.
338+
() => ExamineManager.Instance.RebuildIndex()
339+
}
340+
}));
328341

329342
//ServerMessengerResolver.Current.SetServerMessenger(new DatabaseServerMessenger(UmbracoConfig.For.UmbracoSettings().DistributedCall.Enabled));
330343
//ServerMessengerResolver.Current.SetServerMessenger(new BatchedServerMessenger(() =>

0 commit comments

Comments
 (0)