-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #64 : Support for Scheduler Service
- Loading branch information
Showing
37 changed files
with
1,235 additions
and
14 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
19 changes: 11 additions & 8 deletions
19
samples/AspNetCore/ControllerSample/ControllerSample/appsettings.json
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 |
---|---|---|
@@ -1,10 +1,13 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
"Serilog": { | ||
"MinimumLevel": "Debug" | ||
}, | ||
"AllowedHosts": "*" | ||
} | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
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,42 @@ | ||
# Scheduler example | ||
|
||
This sample shows how Dapr Sidekick can be used to host the Dapr Scheduler service alongside a Dapr Sidecar instance. Sidekick will launch the Scheduler service and wait for it | ||
to enter a healthy running state, then will launch the Sidecar alongside. The Sidecar will be configured to use the launched Scheduler service by default. | ||
As with the Sidecar, the Scheduler service will be continually monitored and maintained for the lifetime of the application. | ||
|
||
## How Dapr Sidekick was added | ||
|
||
This is an ASP.NET Core minimal Web API project. Dapr Sidekick was added to the [Program.cs](SchedulerSample\Program.cs) file as follows: | ||
|
||
```csharp | ||
// Add Dapr Sidekick with Scheduler | ||
builder.Services.AddDaprSidekick(builder.Configuration) | ||
.AddScheduler(); | ||
``` | ||
|
||
Typically when installing Dapr in self-hosted mode, a Scheduler service container is added to Docker exposing the default port 6060. If this same is run | ||
while that container is up it will be unable to start due to a port conflict. Instead a different port 6061 is assigned to Scheduler in configuration: | ||
|
||
```json5 | ||
"Scheduler": { | ||
"RuntimeDirectory": "scheduler", | ||
"Id": "dapr-scheduler-server-0", // Optional unique identifier when used in a cluster | ||
"Port": 6061 // To avoid conflicts with local Dapr Scheduler container. Sidecar will use this automatically as well. | ||
} | ||
``` | ||
|
||
By default the Sidecar that is launched alongside the Scheduler service will look for the Scheduler service locally on this custom port, | ||
unless a specific remote address is defined. For example the following specifies a three-host remote Scheduler cluster: | ||
|
||
```json5 | ||
"Sidecar": { | ||
"SchedulerHostAddress": "remote-host-1:50006,remote-host-2:50006,remote-host-3:50006" | ||
} | ||
``` | ||
|
||
## Running the sample | ||
|
||
To run the sample simply set `SchedulerSample` as the startup project and run it in Visual Studio, | ||
it will launch first the Scheduler service then the Dapr sidecar, then open a browser and display | ||
the configured launch options for both. | ||
|
39 changes: 39 additions & 0 deletions
39
samples/AspNetCore/SchedulerSample/SchedulerSample/Program.cs
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,39 @@ | ||
using Man.Dapr.Sidekick; | ||
using Serilog; | ||
|
||
// Add Serilog for enhanced console logging. | ||
Log.Logger = new LoggerConfiguration() | ||
.Enrich.FromLogContext() | ||
.WriteTo.Console() | ||
.CreateLogger(); | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services.AddControllers(); | ||
|
||
// Add Dapr Sidekick with Scheduler | ||
builder.Services.AddDaprSidekick(builder.Configuration) | ||
.AddScheduler(); | ||
|
||
builder.Host.UseSerilog(); | ||
|
||
var app = builder.Build(); | ||
|
||
app.MapGet("/status", (IDaprSidecarHost sidecarHost, IDaprSchedulerHost schedulerHost) => Results.Ok(new | ||
{ | ||
sidecar = new | ||
{ | ||
process = sidecarHost.GetProcessInfo(), // Information about the sidecar process such as if it is running | ||
options = sidecarHost.GetProcessOptions() // The sidecar options if running, including ports and locations | ||
}, | ||
scheduler = new | ||
{ | ||
process = schedulerHost.GetProcessInfo(), // Information about the sentry process such as if it is running | ||
options = schedulerHost.GetProcessOptions() // The sentry options if running, including ports and locations | ||
}, | ||
})); | ||
|
||
// For Dapr | ||
app.MapHealthChecks("/health"); | ||
|
||
app.Run(); |
13 changes: 13 additions & 0 deletions
13
samples/AspNetCore/SchedulerSample/SchedulerSample/Properties/launchSettings.json
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,13 @@ | ||
{ | ||
"profiles": { | ||
"SchedulerSample": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"launchUrl": "status", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"applicationUrl": "http://localhost:5000" | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
samples/AspNetCore/SchedulerSample/SchedulerSample/SchedulerSample.csproj
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,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\..\src\Man.Dapr.Sidekick.AspNetCore\Man.Dapr.Sidekick.AspNetCore.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="dapr\**\*.*"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
<None Update="scheduler\**\*.*"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
8 changes: 8 additions & 0 deletions
8
samples/AspNetCore/SchedulerSample/SchedulerSample/appsettings.Development.json
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,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
samples/AspNetCore/SchedulerSample/SchedulerSample/appsettings.json
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,19 @@ | ||
{ | ||
"DaprSidekick": { | ||
"Sidecar": { | ||
"RuntimeDirectory": "dapr" | ||
}, | ||
"Scheduler": { | ||
"RuntimeDirectory": "scheduler", | ||
"Id": "dapr-scheduler-server-0", // Optional unique identifier when used in a cluster | ||
"Port": 6061 // To avoid conflicts with local Dapr Scheduler container. Sidecar will use this automatically as well. | ||
} | ||
}, | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
8 changes: 8 additions & 0 deletions
8
samples/AspNetCore/SchedulerSample/SchedulerSample/dapr/config.yaml
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,8 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Configuration | ||
metadata: | ||
name: daprsystem | ||
namespace: default | ||
spec: | ||
mtls: | ||
enabled: true |
6 changes: 6 additions & 0 deletions
6
samples/AspNetCore/SchedulerSample/SchedulerSample/scheduler/config.yaml
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,6 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Configuration | ||
metadata: | ||
name: daprsystem | ||
namespace: default | ||
spec: |
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
11 changes: 11 additions & 0 deletions
11
src/Man.Dapr.Sidekick.AspNetCore/Scheduler/DaprSchedulerHealthCheck.cs
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,11 @@ | ||
namespace Man.Dapr.Sidekick.AspNetCore.Scheduler | ||
{ | ||
public class DaprSchedulerHealthCheck : DaprProcessHealthCheck | ||
{ | ||
public DaprSchedulerHealthCheck( | ||
IDaprSchedulerHost daprSchedulerHost) | ||
: base(daprSchedulerHost) | ||
{ | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Man.Dapr.Sidekick.AspNetCore/Scheduler/DaprSchedulerHealthCheckBuilderExtensions.cs
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,31 @@ | ||
using System.Collections.Generic; | ||
using Man.Dapr.Sidekick.AspNetCore.Metrics; | ||
using Man.Dapr.Sidekick.AspNetCore.Scheduler; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
public static class DaprSchedulerHealthCheckBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Add a health check for the Dapr Scheduler. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param> | ||
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'dapr_Scheduler' will be used for the name.</param> | ||
/// <param name="failureStatus"> | ||
/// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <c>null</c> then | ||
/// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported. | ||
/// </param> | ||
/// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param> | ||
/// <returns>The <see cref="IHealthChecksBuilder"/> to allow calls to be chained.</returns> | ||
public static IHealthChecksBuilder AddDaprScheduler( | ||
this IHealthChecksBuilder builder, | ||
string name = default, | ||
HealthStatus? failureStatus = default, | ||
IEnumerable<string> tags = default) | ||
{ | ||
builder.AddCheck<DaprSchedulerHealthCheck>(name ?? DaprMetricsConstants.DaprSchedulerLabel, failureStatus, tags); | ||
return builder; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Man.Dapr.Sidekick.AspNetCore/Scheduler/DaprSchedulerHostedService.cs
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,25 @@ | ||
using System.Threading; | ||
using Man.Dapr.Sidekick.AspNetCore.Metrics; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Man.Dapr.Sidekick.AspNetCore.Scheduler | ||
{ | ||
public class DaprSchedulerHostedService : DaprHostedService<IDaprSchedulerHost, DaprSchedulerOptions> | ||
{ | ||
public DaprSchedulerHostedService( | ||
IDaprSchedulerHost daprSchedulerHost, | ||
IOptionsMonitor<DaprOptions> optionsAccessor) | ||
: base(daprSchedulerHost, optionsAccessor) | ||
{ | ||
} | ||
|
||
protected override void OnStarting(DaprOptions options, CancellationToken cancellationToken) | ||
{ | ||
// Assign metrics | ||
options.Scheduler ??= new DaprSchedulerOptions(); | ||
options.Scheduler.Metrics ??= new DaprMetricsOptions(); | ||
options.Scheduler.Metrics.SetLabel(DaprMetricsConstants.ServiceLabelName, options.Sidecar?.AppId); | ||
options.Scheduler.Metrics.SetLabel(DaprMetricsConstants.AppLabelName, DaprMetricsConstants.DaprSchedulerLabel); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Man.Dapr.Sidekick.AspNetCore/Scheduler/DaprSchedulerMetricsCollector.cs
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,12 @@ | ||
using Man.Dapr.Sidekick.AspNetCore.Metrics; | ||
|
||
namespace Man.Dapr.Sidekick.AspNetCore.Scheduler | ||
{ | ||
public class DaprSchedulerMetricsCollector : DaprProcessHostPrometheusCollector | ||
{ | ||
public DaprSchedulerMetricsCollector(IDaprSchedulerHost daprSchedulerHost) | ||
: base(daprSchedulerHost) | ||
{ | ||
} | ||
} | ||
} |
Oops, something went wrong.