Skip to content

Commit e79ea09

Browse files
authored
Merge pull request #10 from rollout/CBP-11526
migrate to OpenFeature 1.6+ and enable lifecycle support in CloudBees Provider
2 parents b2112d5 + 22a853b commit e79ea09

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

src/CloudBees.OpenFeature.Provider/CloudBees.OpenFeature.Provider.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="OpenFeature" Version="[1.0.1, 2.0.0)" />
9-
<PackageReference Include="rox-server" Version="5.1.9" />
8+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.3" />
9+
<PackageReference Include="OpenFeature" Version="[1.6.0, 2.0.0)" />
10+
<PackageReference Include="rox-server" Version="6.0.0" />
1011
</ItemGroup>
1112

1213
</Project>

src/CloudBees.OpenFeature.Provider/CloudBeesProvider.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,36 @@
55
using Io.Rollout.Rox.Core.Context;
66
using Io.Rollout.Rox.Server;
77
using OpenFeature;
8+
using OpenFeature.Constant;
89
using OpenFeature.Model;
910

1011
namespace CloudBees.OpenFeature.Provider
1112
{
1213
public class CloudBeesProvider : FeatureProvider
1314
{
1415
private readonly Metadata _metadata = new Metadata("CloudBees Provider");
16+
private ProviderStatus _status = ProviderStatus.NotReady;
17+
private string _appKey;
1518

16-
/// <summary>
17-
/// Call this once before using the provider.
18-
/// </summary>
19-
/// <param name="appKey">The application key from app.rollout.io</param>
20-
public static async Task Setup(string appKey) => await Rox.Setup(appKey);
19+
public CloudBeesProvider(string appKey)
20+
{
21+
_appKey = appKey ?? throw new ArgumentNullException(nameof(appKey));
22+
}
23+
24+
public override async Task Initialize(EvaluationContext context)
25+
{
26+
var options = new RoxOptions(new RoxOptions.RoxOptionsBuilder());
27+
await Rox.Setup(_appKey, options);
28+
_status = Rox.State == RoxState.Corrupted ? ProviderStatus.Error : ProviderStatus.Ready;
29+
}
30+
31+
public override async Task Shutdown()
32+
{
33+
await Rox.Shutdown();
34+
_status = ProviderStatus.NotReady;
35+
}
2136

22-
/// <summary>
23-
/// Call this once when shutting down the application.
24-
/// </summary>
25-
public static async Task Shutdown() => await Rox.Shutdown();
37+
public override ProviderStatus GetStatus() => _status;
2638

2739
public override Metadata GetMetadata() => _metadata;
2840

@@ -62,4 +74,4 @@ private static IContext TransformContext(EvaluationContext context)
6274
: context.AsDictionary().ToDictionary(x => x.Key, x => x.Value.AsObject));
6375
}
6476
}
65-
}
77+
}

src/CloudBees.OpenFeature.Provider/ServiceCollectionExtensions.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System;
22
using CloudBees.OpenFeature.Provider;
33
using OpenFeature;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using System.Threading.Tasks;
46

5-
// ReSharper disable once CheckNamespace
67
namespace Microsoft.Extensions.DependencyInjection
78
{
89
public static class ServiceCollectionExtensions
@@ -17,9 +18,15 @@ public static void AddOpenFeatureCloudBees(this IServiceCollection services, Act
1718
throw new ApplicationException("Missing CloudBees API Key");
1819
}
1920

20-
CloudBeesProvider.Setup(cloudBeesOptions.ApiKey).Wait();
21-
Api.Instance.SetProvider(new CloudBeesProvider());
22-
services.AddSingleton(provider => Api.Instance.GetClient());
21+
var provider = new CloudBeesProvider(cloudBeesOptions.ApiKey);
22+
23+
// Set the provider using async lifecycle call
24+
Task.Run(async () =>
25+
{
26+
await Api.Instance.SetProviderAsync(provider);
27+
}).GetAwaiter().GetResult();
28+
29+
services.AddSingleton(_ => Api.Instance.GetClient());
2330
}
2431
}
25-
}
32+
}

0 commit comments

Comments
 (0)