Skip to content

Commit

Permalink
Merge pull request #58 from Nexus-Mods/rework-startup
Browse files Browse the repository at this point in the history
Fix startup code
  • Loading branch information
halgari authored May 24, 2024
2 parents 7b270f4 + 91d1e48 commit 4ec2719
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 27 deletions.
61 changes: 61 additions & 0 deletions src/NexusMods.MnemonicDB.Abstractions/Models/ModelDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace NexusMods.MnemonicDB.Abstractions.Models;

public class ModelDefinition
{
/// <summary>
/// Creates a new ModelDefinition, with the given name.
/// </summary>
public static ModelDefinition New(string name)
{
return new ModelDefinition();
}

public ModelDefinition Composes<TModel>()
{
return this;
}

public ModelDefinition Composes<TModelA, TModelB>(string name)
{
return this;
}

public ModelDefinition Composes<TModelA, TModelB, TModelC>(string name)
{
return this;
}

public ModelDefinition Composes<TModelA, TModelB, TModelC, TModelD>(string name)
{
return this;
}

public ModelDefinition Composes<TModelA, TModelB, TModelC, TModelD, TModelE>(string name)
{
return this;
}

/// <summary>
/// Defines a new attribute on the model of the given attribute type with the given parameters
/// </summary>
public ModelDefinition WithAttribute<TAttribute>(string name, bool isIndexed = false, bool noHistory = false)
where TAttribute : IAttribute
{
return this;
}

/// <summary>
/// Defines a reference in another model that points to this class. These references will be exposed
/// in the `name` property of this model.
/// </summary>
public ModelDefinition WithBackReference<TModel>(string name)
{
return this;
}

public ModelDefinition Build()
{
return this;
}

}

This file was deleted.

30 changes: 16 additions & 14 deletions src/NexusMods.MnemonicDB.Storage/DatomStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class DatomStore : IDatomStore, IHostedService
private readonly PooledMemoryBufferWriter _prevWriter;
private TxId _asOfTx = TxId.MinValue;

private Task? _bootStrapTask = null;

private static readonly TimeSpan TransactionTimeout = TimeSpan.FromMinutes(120);

/// <summary>
Expand All @@ -55,8 +57,6 @@ public class DatomStore : IDatomStore, IHostedService
/// </summary>
private NextIdCache _nextIdCache;

private bool _isStarted = false;

/// <summary>
/// The task consuming and logging transactions
/// </summary>
Expand Down Expand Up @@ -241,18 +241,21 @@ private async Task Bootstrap()
// Call directly into `Log` as the transaction channel is not yet set up
Log(pending, out _);
_currentSnapshot = _backend.GetSnapshot();
return;
}

_logger.LogInformation("Bootstrapping the datom store, existing state found, last tx: {LastTx}",
lastTx.Value.ToString("x"));
_asOfTx = TxId.From(lastTx.Value);
_currentSnapshot = snapshot;
else
{
_logger.LogInformation("Bootstrapping the datom store, existing state found, last tx: {LastTx}",
lastTx.Value.ToString("x"));
_asOfTx = TxId.From(lastTx.Value);
_currentSnapshot = snapshot;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to bootstrap the datom store");
throw;
}
_txTask = Task.Run(ConsumeTransactions);
}

#region Internals
Expand Down Expand Up @@ -553,17 +556,16 @@ public async Task StartAsync(CancellationToken cancellationToken)
{
lock (this)
{
if (_isStarted) return;
_isStarted = true;
_bootStrapTask ??= Task.Run(Bootstrap, cancellationToken);
}

await Bootstrap();
_txTask = Task.Run(ConsumeTransactions);
await _bootStrapTask;
}

/// <inheritdoc />
public Task StopAsync(CancellationToken cancellationToken)
public async Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
_txChannel.Writer.TryComplete();
await (_txTask ?? Task.CompletedTask);
}
}
10 changes: 6 additions & 4 deletions src/NexusMods.MnemonicDB/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Connection : IConnection, IHostedService
private IDb? _db;
private readonly IEnumerable<IAttribute> _declaredAttributes;
private readonly ILogger<Connection> _logger;
private bool _isStarted = false;
private Task? _bootstrapTask;

/// <summary>
/// Main connection class, co-ordinates writes and immutable reads
Expand Down Expand Up @@ -157,11 +157,13 @@ public async Task StartAsync(CancellationToken cancellationToken)
{
lock (this)
{
if (_isStarted)
return;
_isStarted = true;
_bootstrapTask ??= Task.Run(Bootstrap, cancellationToken);
}
await _bootstrapTask;
}

private async Task Bootstrap()
{
// Won't complete until the DatomStore has properly started
await _store.StartAsync(CancellationToken.None);
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\NexusMods.MnemonicDB.Abstractions\NexusMods.MnemonicDB.Abstractions.csproj"/>
<ProjectReference Include="..\..\src\NexusMods.MnemonicDB.SourceGenerator\NexusMods.MnemonicDB.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

</Project>

0 comments on commit 4ec2719

Please sign in to comment.