From 70fc15bbf59573c8007556b1a8774fe9a62b9922 Mon Sep 17 00:00:00 2001 From: pradeepyadavmsft <122352511+pradeepyadavmsft@users.noreply.github.com> Date: Tue, 3 Sep 2024 12:45:49 -0700 Subject: [PATCH] Hosting garnet as windows service (#614) Co-authored-by: Pradeep Yadav Co-authored-by: Tal Zaccai --- Directory.Packages.props | 2 + Garnet.sln | 16 +++++ .../Garnet.worker/Garnet.worker.csproj | 16 +++++ hosting/Windows/Garnet.worker/Program.cs | 23 +++++++ hosting/Windows/Garnet.worker/Worker.cs | 60 +++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 hosting/Windows/Garnet.worker/Garnet.worker.csproj create mode 100644 hosting/Windows/Garnet.worker/Program.cs create mode 100644 hosting/Windows/Garnet.worker/Worker.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index c025628c1a..df66545fc2 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -25,5 +25,7 @@ + + \ No newline at end of file diff --git a/Garnet.sln b/Garnet.sln index d85da9228e..cf0c3713c4 100644 --- a/Garnet.sln +++ b/Garnet.sln @@ -99,6 +99,12 @@ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GarnetJSON", "playground\GarnetJSON\GarnetJSON.csproj", "{2C8F1F5D-31E5-4D00-A46E-F3B1D9BC098F}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MigrateBench", "playground\MigrateBench\MigrateBench.csproj", "{6B66B394-E410-4B61-9A5A-1595FF6F5E08}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "hosting", "hosting", "{01823EA4-4446-4D66-B268-DFEE55951964}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Windows", "Windows", "{697766CD-2046-46D9-958A-0FD3B46C98D4}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Garnet.worker", "hosting\Windows\Garnet.worker\Garnet.worker.csproj", "{DF2DD03E-87EE-482A-9FBA-6C8FBC23BDC5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -299,6 +305,14 @@ Global {6B66B394-E410-4B61-9A5A-1595FF6F5E08}.Release|Any CPU.Build.0 = Release|Any CPU {6B66B394-E410-4B61-9A5A-1595FF6F5E08}.Release|x64.ActiveCfg = Release|Any CPU {6B66B394-E410-4B61-9A5A-1595FF6F5E08}.Release|x64.Build.0 = Release|Any CPU + {DF2DD03E-87EE-482A-9FBA-6C8FBC23BDC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DF2DD03E-87EE-482A-9FBA-6C8FBC23BDC5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DF2DD03E-87EE-482A-9FBA-6C8FBC23BDC5}.Debug|x64.ActiveCfg = Debug|Any CPU + {DF2DD03E-87EE-482A-9FBA-6C8FBC23BDC5}.Debug|x64.Build.0 = Debug|Any CPU + {DF2DD03E-87EE-482A-9FBA-6C8FBC23BDC5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DF2DD03E-87EE-482A-9FBA-6C8FBC23BDC5}.Release|Any CPU.Build.0 = Release|Any CPU + {DF2DD03E-87EE-482A-9FBA-6C8FBC23BDC5}.Release|x64.ActiveCfg = Release|Any CPU + {DF2DD03E-87EE-482A-9FBA-6C8FBC23BDC5}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -330,6 +344,8 @@ Global {A8CA619E-8F13-4EF8-943F-2D5E3FEBFB3F} = {69A71E2C-00E3-42F3-854E-BE157A24834E} {2C8F1F5D-31E5-4D00-A46E-F3B1D9BC098F} = {69A71E2C-00E3-42F3-854E-BE157A24834E} {6B66B394-E410-4B61-9A5A-1595FF6F5E08} = {69A71E2C-00E3-42F3-854E-BE157A24834E} + {697766CD-2046-46D9-958A-0FD3B46C98D4} = {01823EA4-4446-4D66-B268-DFEE55951964} + {DF2DD03E-87EE-482A-9FBA-6C8FBC23BDC5} = {697766CD-2046-46D9-958A-0FD3B46C98D4} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {2C02C405-4798-41CA-AF98-61EDFEF6772E} diff --git a/hosting/Windows/Garnet.worker/Garnet.worker.csproj b/hosting/Windows/Garnet.worker/Garnet.worker.csproj new file mode 100644 index 0000000000..4f639c7e12 --- /dev/null +++ b/hosting/Windows/Garnet.worker/Garnet.worker.csproj @@ -0,0 +1,16 @@ + + + + net8.0 + + + + + + + + + + + + diff --git a/hosting/Windows/Garnet.worker/Program.cs b/hosting/Windows/Garnet.worker/Program.cs new file mode 100644 index 0000000000..8418da8671 --- /dev/null +++ b/hosting/Windows/Garnet.worker/Program.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +using Garnet; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +class Program +{ + static void Main(string[] args) + { + var builder = Host.CreateApplicationBuilder(args); + builder.Services.AddHostedService(_ => new Worker(args)); + + builder.Services.AddWindowsService(options => + { + options.ServiceName = "Microsoft Garnet Server"; + }); + + var host = builder.Build(); + host.Run(); + } +} \ No newline at end of file diff --git a/hosting/Windows/Garnet.worker/Worker.cs b/hosting/Windows/Garnet.worker/Worker.cs new file mode 100644 index 0000000000..d69adb7e3c --- /dev/null +++ b/hosting/Windows/Garnet.worker/Worker.cs @@ -0,0 +1,60 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Hosting; + +namespace Garnet +{ + public class Worker : BackgroundService + { + private bool _isDisposed = false; + private readonly string[] args; + + private GarnetServer server; + + public Worker(string[] args) + { + this.args = args; + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + try + { + server = new GarnetServer(args); + + // Start the server + server.Start(); + + await Task.Delay(Timeout.Infinite, stoppingToken).ConfigureAwait(false); + } + catch (Exception ex) + { + Console.WriteLine($"Unable to initialize server due to exception: {ex.Message}"); + } + } + + /// + /// Triggered when the application host is performing a graceful shutdown. + /// + /// Indicates that the shutdown process should no longer be graceful. + public override async Task StopAsync(CancellationToken cancellationToken) + { + Dispose(); + await base.StopAsync(cancellationToken).ConfigureAwait(false); + } + + public override void Dispose() + { + if (_isDisposed) + { + return; + } + server?.Dispose(); + _isDisposed = true; + } + } +} \ No newline at end of file