Skip to content

Commit

Permalink
Hosting garnet as windows service (#614)
Browse files Browse the repository at this point in the history
Co-authored-by: Pradeep Yadav <[email protected]>
Co-authored-by: Tal Zaccai <[email protected]>
  • Loading branch information
3 people authored Sep 3, 2024
1 parent 0cd333d commit 70fc15b
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="8.0.1" />
<PackageVersion Include="System.Interactive.Async" Version="6.0.1" />
<PackageVersion Include="System.Text.Json" Version="8.0.4" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0" />
</ItemGroup>
</Project>
16 changes: 16 additions & 0 deletions Garnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
16 changes: 16 additions & 0 deletions hosting/Windows/Garnet.worker/Garnet.worker.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\libs\host\Garnet.host.csproj" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions hosting/Windows/Garnet.worker/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
60 changes: 60 additions & 0 deletions hosting/Windows/Garnet.worker/Worker.cs
Original file line number Diff line number Diff line change
@@ -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}");
}
}

/// <summary>
/// Triggered when the application host is performing a graceful shutdown.
/// </summary>
/// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param>
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;
}
}
}

0 comments on commit 70fc15b

Please sign in to comment.