mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-20 00:08:02 +01:00
feat: Feature lifecycle sql store (#6790)
This commit is contained in:
parent
e8be5dbee4
commit
9dc9fb3586
@ -3,6 +3,35 @@ import { FakeFeatureLifecycleStore } from './fake-feature-lifecycle-store';
|
|||||||
import { FeatureLifecycleService } from './feature-lifecycle-service';
|
import { FeatureLifecycleService } from './feature-lifecycle-service';
|
||||||
import FakeEnvironmentStore from '../project-environments/fake-environment-store';
|
import FakeEnvironmentStore from '../project-environments/fake-environment-store';
|
||||||
import type { IUnleashConfig } from '../../types';
|
import type { IUnleashConfig } from '../../types';
|
||||||
|
import EventStore from '../../db/event-store';
|
||||||
|
import type { Db } from '../../db/db';
|
||||||
|
import { FeatureLifecycleStore } from './feature-lifecycle-store';
|
||||||
|
import EnvironmentStore from '../project-environments/environment-store';
|
||||||
|
|
||||||
|
export const createFeatureLifecycleService = (
|
||||||
|
db: Db,
|
||||||
|
config: IUnleashConfig,
|
||||||
|
) => {
|
||||||
|
const { eventBus, getLogger, flagResolver } = config;
|
||||||
|
const eventStore = new EventStore(db, getLogger, flagResolver);
|
||||||
|
const featureLifecycleStore = new FeatureLifecycleStore(db);
|
||||||
|
const environmentStore = new EnvironmentStore(db, eventBus, getLogger);
|
||||||
|
const featureLifecycleService = new FeatureLifecycleService(
|
||||||
|
{
|
||||||
|
eventStore,
|
||||||
|
featureLifecycleStore,
|
||||||
|
environmentStore,
|
||||||
|
},
|
||||||
|
config,
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
featureLifecycleService,
|
||||||
|
featureLifecycleStore,
|
||||||
|
eventStore,
|
||||||
|
environmentStore,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export const createFakeFeatureLifecycleService = (config: IUnleashConfig) => {
|
export const createFakeFeatureLifecycleService = (config: IUnleashConfig) => {
|
||||||
const eventStore = new FakeEventStore();
|
const eventStore = new FakeEventStore();
|
||||||
|
@ -8,9 +8,12 @@ export class FakeFeatureLifecycleStore implements IFeatureLifecycleStore {
|
|||||||
private lifecycles: Record<string, FeatureLifecycleView> = {};
|
private lifecycles: Record<string, FeatureLifecycleView> = {};
|
||||||
|
|
||||||
async insert(featureLifecycleStage: FeatureLifecycleStage): Promise<void> {
|
async insert(featureLifecycleStage: FeatureLifecycleStage): Promise<void> {
|
||||||
const existing = await this.get(featureLifecycleStage.feature);
|
if (await this.stageExists(featureLifecycleStage)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const existingStages = await this.get(featureLifecycleStage.feature);
|
||||||
this.lifecycles[featureLifecycleStage.feature] = [
|
this.lifecycles[featureLifecycleStage.feature] = [
|
||||||
...existing,
|
...existingStages,
|
||||||
{
|
{
|
||||||
stage: featureLifecycleStage.stage,
|
stage: featureLifecycleStage.stage,
|
||||||
enteredStageAt: new Date(),
|
enteredStageAt: new Date(),
|
||||||
|
@ -8,10 +8,8 @@ import {
|
|||||||
} from '../../types';
|
} from '../../types';
|
||||||
import { createFakeFeatureLifecycleService } from './createFeatureLifecycle';
|
import { createFakeFeatureLifecycleService } from './createFeatureLifecycle';
|
||||||
import EventEmitter from 'events';
|
import EventEmitter from 'events';
|
||||||
|
import type { StageName } from './feature-lifecycle-store-type';
|
||||||
function ms(timeMs) {
|
import { STAGE_ENTERED } from './feature-lifecycle-service';
|
||||||
return new Promise((resolve) => setTimeout(resolve, timeMs));
|
|
||||||
}
|
|
||||||
|
|
||||||
test('can insert and read lifecycle stages', async () => {
|
test('can insert and read lifecycle stages', async () => {
|
||||||
const eventBus = new EventEmitter();
|
const eventBus = new EventEmitter();
|
||||||
@ -22,9 +20,15 @@ test('can insert and read lifecycle stages', async () => {
|
|||||||
} as unknown as IUnleashConfig);
|
} as unknown as IUnleashConfig);
|
||||||
const featureName = 'testFeature';
|
const featureName = 'testFeature';
|
||||||
|
|
||||||
async function emitMetricsEvent(environment: string) {
|
function emitMetricsEvent(environment: string) {
|
||||||
await eventBus.emit(CLIENT_METRICS, { featureName, environment });
|
eventBus.emit(CLIENT_METRICS, { featureName, environment });
|
||||||
await ms(1);
|
}
|
||||||
|
function reachedStage(name: StageName) {
|
||||||
|
return new Promise((resolve) =>
|
||||||
|
featureLifecycleService.on(STAGE_ENTERED, (event) => {
|
||||||
|
if (event.stage === name) resolve(name);
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await environmentStore.create({
|
await environmentStore.create({
|
||||||
@ -45,18 +49,23 @@ test('can insert and read lifecycle stages', async () => {
|
|||||||
} as IEnvironment);
|
} as IEnvironment);
|
||||||
featureLifecycleService.listen();
|
featureLifecycleService.listen();
|
||||||
|
|
||||||
await eventStore.emit(FEATURE_CREATED, { featureName });
|
eventStore.emit(FEATURE_CREATED, { featureName });
|
||||||
|
await reachedStage('initial');
|
||||||
|
|
||||||
await emitMetricsEvent('unknown-environment');
|
emitMetricsEvent('unknown-environment');
|
||||||
await emitMetricsEvent('my-dev-environment');
|
emitMetricsEvent('my-dev-environment');
|
||||||
await emitMetricsEvent('my-dev-environment');
|
await reachedStage('pre-live');
|
||||||
await emitMetricsEvent('my-another-dev-environment');
|
emitMetricsEvent('my-dev-environment');
|
||||||
await emitMetricsEvent('my-prod-environment');
|
emitMetricsEvent('my-another-dev-environment');
|
||||||
await emitMetricsEvent('my-prod-environment');
|
emitMetricsEvent('my-prod-environment');
|
||||||
await emitMetricsEvent('my-another-prod-environment');
|
await reachedStage('live');
|
||||||
|
emitMetricsEvent('my-prod-environment');
|
||||||
|
emitMetricsEvent('my-another-prod-environment');
|
||||||
|
|
||||||
await eventStore.emit(FEATURE_COMPLETED, { featureName });
|
eventStore.emit(FEATURE_COMPLETED, { featureName });
|
||||||
await eventStore.emit(FEATURE_ARCHIVED, { featureName });
|
await reachedStage('completed');
|
||||||
|
eventStore.emit(FEATURE_ARCHIVED, { featureName });
|
||||||
|
await reachedStage('archived');
|
||||||
|
|
||||||
const lifecycle =
|
const lifecycle =
|
||||||
await featureLifecycleService.getFeatureLifecycle(featureName);
|
await featureLifecycleService.getFeatureLifecycle(featureName);
|
||||||
|
@ -12,9 +12,11 @@ import type {
|
|||||||
FeatureLifecycleView,
|
FeatureLifecycleView,
|
||||||
IFeatureLifecycleStore,
|
IFeatureLifecycleStore,
|
||||||
} from './feature-lifecycle-store-type';
|
} from './feature-lifecycle-store-type';
|
||||||
import type EventEmitter from 'events';
|
import EventEmitter from 'events';
|
||||||
|
|
||||||
export class FeatureLifecycleService {
|
export const STAGE_ENTERED = 'STAGE_ENTERED';
|
||||||
|
|
||||||
|
export class FeatureLifecycleService extends EventEmitter {
|
||||||
private eventStore: IEventStore;
|
private eventStore: IEventStore;
|
||||||
|
|
||||||
private featureLifecycleStore: IFeatureLifecycleStore;
|
private featureLifecycleStore: IFeatureLifecycleStore;
|
||||||
@ -40,6 +42,7 @@ export class FeatureLifecycleService {
|
|||||||
eventBus,
|
eventBus,
|
||||||
}: Pick<IUnleashConfig, 'flagResolver' | 'eventBus'>,
|
}: Pick<IUnleashConfig, 'flagResolver' | 'eventBus'>,
|
||||||
) {
|
) {
|
||||||
|
super();
|
||||||
this.eventStore = eventStore;
|
this.eventStore = eventStore;
|
||||||
this.featureLifecycleStore = featureLifecycleStore;
|
this.featureLifecycleStore = featureLifecycleStore;
|
||||||
this.environmentStore = environmentStore;
|
this.environmentStore = environmentStore;
|
||||||
@ -86,6 +89,7 @@ export class FeatureLifecycleService {
|
|||||||
|
|
||||||
private async featureInitialized(feature: string) {
|
private async featureInitialized(feature: string) {
|
||||||
await this.featureLifecycleStore.insert({ feature, stage: 'initial' });
|
await this.featureLifecycleStore.insert({ feature, stage: 'initial' });
|
||||||
|
this.emit(STAGE_ENTERED, { stage: 'initial' });
|
||||||
}
|
}
|
||||||
|
|
||||||
private async stageReceivedMetrics(
|
private async stageReceivedMetrics(
|
||||||
@ -98,6 +102,7 @@ export class FeatureLifecycleService {
|
|||||||
});
|
});
|
||||||
if (!stageExists) {
|
if (!stageExists) {
|
||||||
await this.featureLifecycleStore.insert({ feature, stage });
|
await this.featureLifecycleStore.insert({ feature, stage });
|
||||||
|
this.emit(STAGE_ENTERED, { stage });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,9 +123,11 @@ export class FeatureLifecycleService {
|
|||||||
feature,
|
feature,
|
||||||
stage: 'completed',
|
stage: 'completed',
|
||||||
});
|
});
|
||||||
|
this.emit(STAGE_ENTERED, { stage: 'completed' });
|
||||||
}
|
}
|
||||||
|
|
||||||
private async featureArchived(feature: string) {
|
private async featureArchived(feature: string) {
|
||||||
await this.featureLifecycleStore.insert({ feature, stage: 'archived' });
|
await this.featureLifecycleStore.insert({ feature, stage: 'archived' });
|
||||||
|
this.emit(STAGE_ENTERED, { stage: 'archived' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,52 @@
|
|||||||
|
import type {
|
||||||
|
FeatureLifecycleStage,
|
||||||
|
IFeatureLifecycleStore,
|
||||||
|
FeatureLifecycleView,
|
||||||
|
StageName,
|
||||||
|
} from './feature-lifecycle-store-type';
|
||||||
|
import type { Db } from '../../db/db';
|
||||||
|
|
||||||
|
type DBType = {
|
||||||
|
feature: string;
|
||||||
|
stage: StageName;
|
||||||
|
created_at: Date;
|
||||||
|
};
|
||||||
|
|
||||||
|
export class FeatureLifecycleStore implements IFeatureLifecycleStore {
|
||||||
|
private db: Db;
|
||||||
|
|
||||||
|
constructor(db: Db) {
|
||||||
|
this.db = db;
|
||||||
|
}
|
||||||
|
|
||||||
|
async insert(featureLifecycleStage: FeatureLifecycleStage): Promise<void> {
|
||||||
|
await this.db('feature_lifecycles')
|
||||||
|
.insert({
|
||||||
|
feature: featureLifecycleStage.feature,
|
||||||
|
stage: featureLifecycleStage.stage,
|
||||||
|
})
|
||||||
|
.returning('*')
|
||||||
|
.onConflict(['feature', 'stage'])
|
||||||
|
.ignore();
|
||||||
|
}
|
||||||
|
|
||||||
|
async get(feature: string): Promise<FeatureLifecycleView> {
|
||||||
|
const results = await this.db('feature_lifecycles')
|
||||||
|
.where({ feature })
|
||||||
|
.orderBy('created_at', 'asc');
|
||||||
|
|
||||||
|
return results.map(({ stage, created_at }: DBType) => ({
|
||||||
|
stage,
|
||||||
|
enteredStageAt: created_at,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
async stageExists(stage: FeatureLifecycleStage): Promise<boolean> {
|
||||||
|
const result = await this.db.raw(
|
||||||
|
`SELECT EXISTS(SELECT 1 FROM feature_lifecycles WHERE stage = ? and feature = ?) AS present`,
|
||||||
|
[stage.stage, stage.feature],
|
||||||
|
);
|
||||||
|
const { present } = result.rows[0];
|
||||||
|
return present;
|
||||||
|
}
|
||||||
|
}
|
@ -5,14 +5,23 @@ import {
|
|||||||
} from '../../../test/e2e/helpers/test-helper';
|
} from '../../../test/e2e/helpers/test-helper';
|
||||||
import getLogger from '../../../test/fixtures/no-logger';
|
import getLogger from '../../../test/fixtures/no-logger';
|
||||||
import {
|
import {
|
||||||
|
CLIENT_METRICS,
|
||||||
FEATURE_ARCHIVED,
|
FEATURE_ARCHIVED,
|
||||||
FEATURE_CREATED,
|
FEATURE_CREATED,
|
||||||
type IEventStore,
|
type IEventStore,
|
||||||
} from '../../types';
|
} from '../../types';
|
||||||
|
import type EventEmitter from 'events';
|
||||||
|
import {
|
||||||
|
type FeatureLifecycleService,
|
||||||
|
STAGE_ENTERED,
|
||||||
|
} from './feature-lifecycle-service';
|
||||||
|
import type { StageName } from './feature-lifecycle-store-type';
|
||||||
|
|
||||||
let app: IUnleashTest;
|
let app: IUnleashTest;
|
||||||
let db: ITestDb;
|
let db: ITestDb;
|
||||||
|
let featureLifecycleService: FeatureLifecycleService;
|
||||||
let eventStore: IEventStore;
|
let eventStore: IEventStore;
|
||||||
|
let eventBus: EventEmitter;
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
db = await dbInit('feature_lifecycle', getLogger);
|
db = await dbInit('feature_lifecycle', getLogger);
|
||||||
@ -28,6 +37,8 @@ beforeAll(async () => {
|
|||||||
db.rawDatabase,
|
db.rawDatabase,
|
||||||
);
|
);
|
||||||
eventStore = db.stores.eventStore;
|
eventStore = db.stores.eventStore;
|
||||||
|
eventBus = app.config.eventBus;
|
||||||
|
featureLifecycleService = app.services.featureLifecycleService;
|
||||||
|
|
||||||
await app.request
|
await app.request
|
||||||
.post(`/auth/demo/login`)
|
.post(`/auth/demo/login`)
|
||||||
@ -50,18 +61,31 @@ const getFeatureLifecycle = async (featureName: string, expectedCode = 200) => {
|
|||||||
.expect(expectedCode);
|
.expect(expectedCode);
|
||||||
};
|
};
|
||||||
|
|
||||||
function ms(timeMs) {
|
function reachedStage(name: StageName) {
|
||||||
return new Promise((resolve) => setTimeout(resolve, timeMs));
|
return new Promise((resolve) =>
|
||||||
|
featureLifecycleService.on(STAGE_ENTERED, (event) => {
|
||||||
|
if (event.stage === name) resolve(name);
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
test('should return lifecycle stages', async () => {
|
test('should return lifecycle stages', async () => {
|
||||||
await eventStore.emit(FEATURE_CREATED, { featureName: 'my_feature_a' });
|
await app.createFeature('my_feature_a');
|
||||||
await eventStore.emit(FEATURE_ARCHIVED, { featureName: 'my_feature_a' });
|
eventStore.emit(FEATURE_CREATED, { featureName: 'my_feature_a' });
|
||||||
|
await reachedStage('initial');
|
||||||
|
eventBus.emit(CLIENT_METRICS, {
|
||||||
|
featureName: 'my_feature_a',
|
||||||
|
environment: 'default',
|
||||||
|
});
|
||||||
|
await reachedStage('live');
|
||||||
|
eventStore.emit(FEATURE_ARCHIVED, { featureName: 'my_feature_a' });
|
||||||
|
await reachedStage('archived');
|
||||||
|
|
||||||
const { body } = await getFeatureLifecycle('my_feature_a');
|
const { body } = await getFeatureLifecycle('my_feature_a');
|
||||||
|
|
||||||
expect(body).toEqual([
|
expect(body).toEqual([
|
||||||
{ stage: 'initial', enteredStageAt: expect.any(String) },
|
{ stage: 'initial', enteredStageAt: expect.any(String) },
|
||||||
|
{ stage: 'live', enteredStageAt: expect.any(String) },
|
||||||
{ stage: 'archived', enteredStageAt: expect.any(String) },
|
{ stage: 'archived', enteredStageAt: expect.any(String) },
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
@ -67,6 +67,7 @@ import {
|
|||||||
createEnvironmentService,
|
createEnvironmentService,
|
||||||
createFakeEnvironmentService,
|
createFakeEnvironmentService,
|
||||||
createFakeProjectService,
|
createFakeProjectService,
|
||||||
|
createFeatureLifecycleService,
|
||||||
createFeatureToggleService,
|
createFeatureToggleService,
|
||||||
createProjectService,
|
createProjectService,
|
||||||
} from '../features';
|
} from '../features';
|
||||||
@ -349,8 +350,9 @@ export const createServices = (
|
|||||||
const inactiveUsersService = new InactiveUsersService(stores, config, {
|
const inactiveUsersService = new InactiveUsersService(stores, config, {
|
||||||
userService,
|
userService,
|
||||||
});
|
});
|
||||||
const { featureLifecycleService } =
|
const { featureLifecycleService } = db
|
||||||
createFakeFeatureLifecycleService(config);
|
? createFeatureLifecycleService(db, config)
|
||||||
|
: createFakeFeatureLifecycleService(config);
|
||||||
featureLifecycleService.listen();
|
featureLifecycleService.listen();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
18
src/migrations/20240405120422-add-feature-lifecycles.js
Normal file
18
src/migrations/20240405120422-add-feature-lifecycles.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
exports.up = function(db, cb) {
|
||||||
|
db.runSql(
|
||||||
|
`
|
||||||
|
CREATE TABLE IF NOT EXISTS feature_lifecycles (
|
||||||
|
feature VARCHAR(255) NOT NULL REFERENCES features(name) ON DELETE CASCADE,
|
||||||
|
stage VARCHAR(255) NULL,
|
||||||
|
created_at TIMESTAMP WITH TIME ZONE default now(),
|
||||||
|
PRIMARY KEY (feature, stage)
|
||||||
|
);`,
|
||||||
|
cb,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.down = function(db, cb) {
|
||||||
|
db.runSql('DROP TABLE IF EXISTS feature_lifecycles;', cb);
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user