4.3 KiB
title |
---|
How to use custom activation strategies |
This guide takes you through how to use custom activation strategies with Unleash. We'll go through how you define a custom strategy in the admin UI, how you add it to a toggle, and how you'd implement it in a client SDK.
In this example we want to define an activation strategy offers a scheduled release of a feature toggle. This means that we want the feature toggle to be activated after a given date and time.
Step 1: Define your custom strategy
-
Navigate to the strategies view. Interact with the "Configure" button in the page header and then go to the "Strategies" link in the dropdown menu that appears.
-
Define your strategy. Use the "Add new strategy" button to open the strategy creation form. Fill in the form to define your strategy. Refer to the custom strategy reference documentation for a full list of options.
Step 2: Apply your custom strategy to a feature toggle
Navigate to your feature toggle and apply the strategy you just created.
Step 3: Implement the strategy in your client SDK
The steps to implement a custom strategy for your client depend on the kind of client SDK you're using:
- if you're using a server-side client SDK, follow the steps in option A.
- if you're using a front-end client SDK (Android, JavaScript, React, iOS), follow the steps in option B
Step 3 Option A: Implement the strategy for a server-side client SDK
-
Implement the custom strategy in your client SDK. The exact way to do this will vary depending on the specific SDK you're using, so refer to the SDK's documentation. The example below shows an example of how you'd implement a custom strategy called "TimeStamp" for the Node.js client SDK.
const { Strategy } = require('unleash-client'); class TimeStampStrategy extends Strategy { constructor() { super('TimeStamp'); } isEnabled(parameters, context) { return Date.parse(parameters.enableAfter) > Date.now(); } }
-
Register the custom strategy with the Unleash Client. When instantiating the Unleash Client, provide it with a list of the custom strategies you'd like to use — again: refer to your client SDK's docs for the specifics.
Here's a full, working example for Node.js:
const { Strategy, initialize, isEnabled } = require('unleash-client'); class TimeStampStrategy extends Strategy { constructor() { super('TimeStamp'); } isEnabled(parameters, context) { return Date.parse(parameters.enableAfter) > Date.now(); } } const instance = initialize({ url: 'http://unleash.herokuapp.com/api/', appName: 'unleash-demo', instanceId: '1', strategies: [new TimeStampStrategy()], }); instance.on('ready', () => { setInterval(() => { console.log(isEnabled('demo.TimeStampRollout')); }, 1000); });
:::note this is different for the Unleash proxy :::
p Depending on the client SDK you're working with, choose one of the below options. If you're using