Enable my feature automatically

When I implement a new feature to my application, I would like to manage it after the release (enable, target, tune and obviously in urgent cases roll it back). The feature/release toggles design pattern can solve my issue, but how can I answer the business needs? For example:

  • turn on the feature on Sunday at 12 PM
  • increase the discount value of the promotion every workday
  • enable the feature only on weekends or on weekdays

ConfigCat + cURL + cron = Feature Flags Scheduler

cURL

The cURL is an open-source command-line tool to transfer data (currently I use HTTP protocol).

cron jobs

Cron is a useful utility to schedule commands or tasks on a Unix-like operating system. (On Windows platform you can use Task Scheduler or schtasks)

ConfigCat- Public API

With Public API you can access the ConfigCat platform programmatically.

Step 1 - Ensure all requirements

  1. ConfigCat Public API credentials Follow this setup to get a credential to invoke any ConfigCat Public API endpoints.
  2. Install cURL
sudo apt install curl
  1. Install cron Usually every Linux distribution has some form of cron installed by default. However, if you're using an Ubuntu machine on which cron isn't installed, you can install it using apt:
sudo apt update
sudo apt install cron
sudo systemctl enable cron

Step 2 - Create a command

Create a command to enable feature flag value. Open your favourite text editor (I'm using nano) and create a file (enableMyAwsomeFeature.sh):

nano enableMyAwsomeFeature.sh

insert the following script into the file and replace your API credential and SDK key:

auth_credentials_in_base64="<INSERT YOUR PUBLIC API CREDENTIAL>"
setting_key="myAwesomeFeature"
reason="Enable from cron job"
SDK_KEY="<INSERT YOUR SDK KEY>"
curl -X PUT \
"https://api.configcat.com/v1/settings/${setting_key}/value?reason=${reason}" \
-H "X-CONFIGCAT-SDKKEY: ${SDK_KEY}" \
-H "Authorization: Basic ${auth_credentials_in_base64}" \
-H "Content-Type: application/json" \
-d "{\"value\":true}"

Make sure enableMyAwsomeFeature.sh is executable. I used chmod 755.

Step 3 - Setup your cron job

What is a cron job?

Job describes when and what would you like to execute. Cron jobs are stored in a particular file called crontab in the system. To schedule a job, you need to open up your crontab for editing and create a task. It is written in the form of a cron expression. The expression syntax is broken down into six fields:

  1. minute
  2. hour
  3. day of the month
  4. month
  5. day of the week
  6. command to execute

For example cron expression for run my script every Monday at 7:59AM is: 59 7 * * MON enableMyAwsomeFeature.sh

With cron expression, you can schedule anything

Check crontab.guru to compose cron expression easily.

Setup crontab

As I mentioned earlier, the cron expressions stored in a file and it's not recommend to edit it manually. Manage your cron jobs safety use crontab -e command. You can list your already existing cron jobs (if you don't have any your list will be empty). Each line in this file represents a cron job, you should insert a line to register a new one. Add this line to your list: 59 7 * * MON enableMyAwsomeFeature.sh If you use nano press CTRL + X after Y then ENTER to save.

Check your cron jobs with the following command: crontab -l

If you need system-wide cron expression you could edit with the following command: sudo nano /etc/crontab. These type of cron tabs have an additional field for user profile.

Conclusion

You can imagine that if you create disableMyAwsomeFeature.sh which is very similar, you can schedule anything with cron expression. Here are some examples

  1. Enable feature only on weekdays between 8AM-5PM
0 8 * * 1-5 enableMyAwsomeFeature.sh
0 17 * * 1-5 disableMyAwsomeFeature.sh
  1. Turn on the feature at a fixed time: on the 6th of May at 10 AM
0 10 6 5 * enableMyAwsomeFeature.sh

(* - this expression will run every year)

Make sure that your machine's clock is synchronised, use clock synchronisation (NTP) to ensure precious execution.