Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .editorconfig

This file was deleted.

19 changes: 17 additions & 2 deletions csharp/Platform.Data.Doublets.Gql.Server/Data.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
using Platform.Data.Doublets.Memory.Split.Generic;
using Platform.Memory;
using System;

namespace Platform.Data.Doublets.Gql.Server
{
public class Data
{
public static string DefaultDatabaseFileName = "db.links";
private static SplitMemoryLinks<ulong>? _disposableLinks;

public static ILinks<ulong> CreateLinks()
{
// var disposableLinks = new UnitedMemoryLinks<ulong>(new FileMappedResizableDirectMemory(DefaultDatabaseFileName), UnitedMemoryLinks<ulong>.DefaultLinksSizeStep, new LinksConstants<ulong>(enableExternalReferencesSupport: true), IndexTreeType.Default);
var disposableLinks = new SplitMemoryLinks<ulong>(new FileMappedResizableDirectMemory(DefaultDatabaseFileName), new FileMappedResizableDirectMemory(DefaultDatabaseFileName + ".index"), SplitMemoryLinks<ulong>.DefaultLinksSizeStep, new LinksConstants<ulong>(true));
return new SynchronizedLinks<ulong>(disposableLinks.DecorateWithAutomaticUniquenessAndUsagesResolution());
_disposableLinks = new SplitMemoryLinks<ulong>(new FileMappedResizableDirectMemory(DefaultDatabaseFileName), new FileMappedResizableDirectMemory(DefaultDatabaseFileName + ".index"), SplitMemoryLinks<ulong>.DefaultLinksSizeStep, new LinksConstants<ulong>(true));
return new SynchronizedLinks<ulong>(_disposableLinks.DecorateWithAutomaticUniquenessAndUsagesResolution());
}

public static void DisposeLinks()
{
try
{
_disposableLinks?.Dispose();
_disposableLinks = null;
}
catch (Exception)
{
// Suppress exceptions during disposal to prevent AccessViolationException
}
}
}
}
51 changes: 51 additions & 0 deletions csharp/Platform.Data.Doublets.Gql.Server/LinksLifetimeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Platform.Data.Doublets.Gql.Server
{
public class LinksLifetimeService : IHostedService
{
private readonly ILogger<LinksLifetimeService> _logger;
private readonly IHostApplicationLifetime _lifetime;

public LinksLifetimeService(ILogger<LinksLifetimeService> logger, IHostApplicationLifetime lifetime)
{
_logger = logger;
_lifetime = lifetime;
}

public Task StartAsync(CancellationToken cancellationToken)
{
_lifetime.ApplicationStopping.Register(OnApplicationStopping);
_lifetime.ApplicationStopped.Register(OnApplicationStopped);
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}

private void OnApplicationStopping()
{
_logger.LogInformation("Application is stopping, disposing links...");
try
{
Data.DisposeLinks();
_logger.LogInformation("Links disposed successfully");
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Error occurred while disposing links during application stop");
}
}

private void OnApplicationStopped()
{
_logger.LogInformation("Application stopped");
}
}
}
9 changes: 9 additions & 0 deletions csharp/Platform.Data.Doublets.Gql.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ public static int Main(params string[] args)
}
finally
{
try
{
Log.Information("Disposing links before shutdown");
Data.DisposeLinks();
}
catch (Exception ex)
{
Log.Warning(ex, "Error during links disposal");
}
Log.CloseAndFlush();
}
}
Expand Down
1 change: 1 addition & 0 deletions csharp/Platform.Data.Doublets.Gql.Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public Startup(IConfiguration configuration, IWebHostEnvironment environment)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) => services.AddSingleton(sp => Data.CreateLinks())
.AddSingleton<LinksSchema>()
.AddHostedService<LinksLifetimeService>()
.AddGraphQL((options, provider) =>
{
options.EnableMetrics = Environment.IsDevelopment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void ConfigureServices(IServiceCollection services) => services.AddRoutin
})
.AddSingleton(sp => Data.CreateLinks())
.AddSingleton<LinksSchema>()
.AddHostedService<LinksLifetimeService>()
.AddGraphQL((options, provider) =>
{
options.EnableMetrics = Environment.IsDevelopment();
Expand Down