Skip to content

Commit 548ef45

Browse files
committed
Use ColdBox scheduler for feature flag updates
1 parent 2a0fdfc commit 548ef45

File tree

6 files changed

+70
-40
lines changed

6 files changed

+70
-40
lines changed

ModuleConfig.cfc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ component {
1111
"contextProvider": "DefaultContextProvider@unleashsdk",
1212
"apiURL": getSystemSetting( "UNLEASH_API_URL" ),
1313
"apiToken": getSystemSetting( "UNLEASH_API_TOKEN" ),
14-
"cacheTimeout": createTimeSpan( 0, 0, 0, 10 )
14+
"cacheTimeout": createTimeSpan( 0, 0, 0, 10 ),
15+
"refreshInterval": 10
1516
};
16-
}
1717

18-
function onLoad() {
1918
binder.map( "UnleashHyperClient@unleashsdk" )
2019
.to( "hyper.models.HyperBuilder" )
2120
.asSingleton()
@@ -35,4 +34,8 @@ component {
3534
.toDSL( "UnleashSDK@unleashsdk" );
3635
}
3736

37+
function onLoad() {
38+
39+
}
40+
3841
}

config/Scheduler.cfc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
component {
2+
3+
property name="refreshInterval" inject="coldbox:setting:refreshInterval@unleashsdk";
4+
property name="log" inject="logbox:logger:{this}";
5+
6+
function configure() {
7+
task( "unleashsdk-refresh-features" )
8+
.call( getInstance( "UnleashSDK@unleashsdk" ), "refreshFeatures" )
9+
.every( variables.refreshInterval, "seconds" )
10+
.before( function() {
11+
if ( log.canDebug() ) {
12+
log.debug( "Starting to fetch new features from unleash" );
13+
}
14+
} )
15+
.onSuccess( function( task, results ) {
16+
if ( log.canInfo() ) {
17+
log.info( "Successfully refreshed features", results );
18+
}
19+
} )
20+
.onFailure( function( task, exception ) {
21+
if ( log.canError() ) {
22+
log.error( "Exception when running task [unleashsdk-refresh-features]:", exception );
23+
}
24+
} );
25+
}
26+
27+
}

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ services:
99
expose:
1010
- "61442"
1111
environment:
12-
APP_DIR: /unleashsdk
12+
APP_DIR: /app/unleashsdk
1313
PORT: 61442
1414
BOX_SERVER_PROFILE: development
1515
UNLEASH_API_URL: http://unleash:4242/api
1616
volumes:
17-
- .:/unleashsdk
17+
- .:/app/unleashsdk
1818

1919
unleash:
2020
image: unleashorg/unleash-server:4.0.10

models/UnleashSDK.cfc

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ component singleton accessors="true" {
1515
};
1616

1717
public boolean function isEnabled(
18-
required string name,
19-
struct additionalContext = {},
20-
boolean defaultValue = false
21-
) {
18+
required string name,
19+
struct additionalContext = {},
20+
boolean defaultValue = false
21+
) {
2222
var feature = getFeature( arguments.name );
2323
if ( isNull( feature ) ) {
2424
return arguments.defaultValue;
@@ -28,11 +28,11 @@ component singleton accessors="true" {
2828
return false;
2929
}
3030

31-
var context = getContext( arguments.additionalContext );
31+
var context = getContext( arguments.additionalContext );
3232

33-
if ( feature.strategies.isEmpty() ) {
34-
return true;
35-
}
33+
if ( feature.strategies.isEmpty() ) {
34+
return true;
35+
}
3636

3737
for ( var strategyData in feature.strategies ) {
3838
var strategy = getStrategy( strategyData.name );
@@ -55,10 +55,10 @@ component singleton accessors="true" {
5555
}
5656

5757
public boolean function isDisabled(
58-
required string name,
59-
struct additionalContext = {},
60-
boolean defaultValue = false
61-
) {
58+
required string name,
59+
struct additionalContext = {},
60+
boolean defaultValue = false
61+
) {
6262
return !isEnabled( argumentCollection = arguments );
6363
}
6464

@@ -118,26 +118,17 @@ component singleton accessors="true" {
118118
}
119119

120120
public array function getFeatures() {
121-
try {
122-
return cache.getOrSet(
123-
"unleashsdk-features",
124-
function() {
125-
var features = fetchFeatures();
126-
cache.set( "unleashsdk-failover", features, 0 );
127-
return features;
128-
},
129-
variables.settings.cacheTimeout
130-
);
131-
} catch ( any e ) {
132-
if ( log.canError() ) {
133-
log.error( "Exception occurred while retrieving Unleash features. Using failover", e );
134-
}
135-
var features = cache.get( "unleashsdk-failover" );
136-
if ( isNull( features ) ) {
137-
return [];
138-
}
139-
return features;
121+
var features = cache.get( "unleashsdk-failover" );
122+
if ( isNull( features ) ) {
123+
return [];
140124
}
125+
return features;
126+
}
127+
128+
public array function refreshFeatures() {
129+
var features = fetchFeatures();
130+
cache.set( "unleashsdk-failover", features, 0 );
131+
return features;
141132
}
142133

143134
private array function fetchFeatures() {
@@ -161,7 +152,11 @@ component singleton accessors="true" {
161152

162153
private struct function getContext( struct additionalContext = {} ) {
163154
param request.unleashContext = generateContext();
164-
structAppend( arguments.additionalContext, request.unleashContext, false );
155+
structAppend(
156+
arguments.additionalContext,
157+
request.unleashContext,
158+
false
159+
);
165160
return arguments.additionalContext;
166161
}
167162

server.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
}
77
},
88
"app":{
9-
"cfengine":"adobe@2018"
9+
"cfengine":"lucee@5"
1010
},
1111
"directoryBrowsing":"true"
1212
}

tests/Application.cfc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ component {
1111
rootPath = REReplaceNoCase( this.mappings[ "/tests" ], "tests(\\|/)", "" );
1212
this.mappings[ "/root" ] = rootPath;
1313
this.mappings[ "/unleashsdk" ] = rootPath;
14-
this.mappings[ "/testingModuleRoot" ] = "/";
14+
this.mappings[ "/testingModuleRoot" ] = "/app";
1515
this.mappings[ "/app" ] = testsPath & "resources/app";
1616
this.mappings[ "/coldbox" ] = testsPath & "resources/app/coldbox";
1717
this.mappings[ "/testbox" ] = rootPath & "/testbox";
@@ -25,6 +25,7 @@ component {
2525
};
2626

2727
function onRequestStart() {
28+
systemOutput( "Starting request" );
2829
setting requestTimeout="180";
2930
structDelete( application, "cbController" );
3031
structDelete( application, "wirebox" );
@@ -48,7 +49,11 @@ component {
4849
"password": "unleash4all"
4950
})#" );
5051
}
51-
var authCookie = listFirst( local.res.Responseheader[ "Set-Cookie" ], ";" );
52+
var authCookie = local.res.Responseheader[ "Set-Cookie" ];
53+
if ( isArray( authCookie ) ) {
54+
authCookie = authCookie[ 1 ];
55+
}
56+
authCookie = listFirst( authCookie, ";" );
5257
cookie[ "unleash-session" ] = listRest( urlDecode( authCookie ), "=" );
5358
}
5459

0 commit comments

Comments
 (0)