# State Change Handlers

## IFeatureStateService

IFeatureStateService allows registering Action handlers which will be called when the feature changes state.

{% hint style="info" %}
Due to the nature of what we're controling, only deterministic states are considered valid, so the state change will only trigger for AlwaysOn and off. Any other type of rollout will be considered off.&#x20;

\* Support for additional deterministic states will be added in the future
{% endhint %}

### Feature Flags around Hangfire Jobs

```csharp
var featureStateService = app.Services.GetRequiredService<IFeatureStateService>();
featureStateService.WhenFeatureTurnsOn(FeatureFlags.HourlyJob, () =>
{
    //start a service or job
    RecurringJob.AddOrUpdate<ITestRecurringJob>("Hourly job", s => s.RunAsync(), Cron.Hourly());
});

featureStateService.WhenFeatureTurnsOff(FeatureFlags.HourlyJob, () =>
{
    //stop a service or job
    RecurringJob.RemoveIfExists("Hourly job");
});
```
