-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathSqlTargetScaler.cs
37 lines (31 loc) · 1.31 KB
/
SqlTargetScaler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#if FUNCTIONS_V4
namespace DurableTask.SqlServer.AzureFunctions
{
using System;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Host.Scale;
public class SqlTargetScaler : ITargetScaler
{
readonly SqlMetricsProvider sqlMetricsProvider;
public SqlTargetScaler(string taskHubName, SqlMetricsProvider sqlMetricsProvider)
{
this.sqlMetricsProvider = sqlMetricsProvider;
// Scalers in Durable Functions are shared for all functions in the same task hub.
// So instead of using a function ID, we use the task hub name as the basis for the descriptor ID.
string id = $"DurableTask-SqlServer:{taskHubName ?? "default"}";
this.TargetScalerDescriptor = new TargetScalerDescriptor(id);
}
public TargetScalerDescriptor TargetScalerDescriptor { get; }
public async Task<TargetScalerResult> GetScaleResultAsync(TargetScalerContext context)
{
SqlScaleMetric sqlScaleMetric = await this.sqlMetricsProvider.GetMetricsAsync();
return new TargetScalerResult
{
TargetWorkerCount = Math.Max(0, sqlScaleMetric.RecommendedReplicaCount),
};
}
}
}
#endif