-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hosting garnet as windows service (#614)
Co-authored-by: Pradeep Yadav <[email protected]> Co-authored-by: Tal Zaccai <[email protected]>
- Loading branch information
1 parent
0cd333d
commit 70fc15b
Showing
5 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |