Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cluster service #343

Merged
merged 2 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion server/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ const {
workflowSignalHandler,
workflowTerminateHandler,
} = require('./routes');
const { clusterService } = require('./services');

const { listWorkflows } = require('./helpers');

const router = new Router();

router.get('/api/cluster', clusterHandler.getCluster);
router.get('/api/cluster', clusterHandler(clusterService));

router.get('/api/domains', domainListHandler);

Expand Down
36 changes: 2 additions & 34 deletions server/router/routes/cluster-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

const { CLUSTER_CACHE_TTL } = require('../constants');

let cache = null;
let cacheExpiryDateTime = null;

const clearCache = () => {
cache = null;
cacheExpiryDateTime = null;
};

const setCache = data => {
cache = data;
cacheExpiryDateTime = Date.now() + CLUSTER_CACHE_TTL;
};

const getCluster = async ctx => {
if (cacheExpiryDateTime && Date.now() < cacheExpiryDateTime) {
return (ctx.body = cache);
}

const cluster = await ctx.cadence.describeCluster();

const data = { ...cluster, membershipInfo: null };

setCache(data);

ctx.body = data;
};

const clusterHandler = {
clearCache,
getCluster,
setCache,
};
const clusterHandler = clusterService => async ctx =>
(ctx.body = await clusterService.getCluster(ctx));

module.exports = clusterHandler;
57 changes: 57 additions & 0 deletions server/router/services/cluster-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2021 Uber Technologies Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

const { CLUSTER_CACHE_TTL } = require('../constants');

let cache = null;
let cacheExpiryDateTime = null;

const clearCache = () => {
cache = null;
cacheExpiryDateTime = null;
};

const setCache = data => {
cache = data;
cacheExpiryDateTime = Date.now() + CLUSTER_CACHE_TTL;
};

const getCluster = async ctx => {
if (cacheExpiryDateTime && Date.now() < cacheExpiryDateTime) {
return cache;
}

const cluster = await ctx.cadence.describeCluster();

const data = { ...cluster, membershipInfo: null };

setCache(data);

return data;
};

const clusterService = {
clearCache,
getCluster,
setCache,
};

module.exports = clusterService;
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import clusterHandler from './cluster-handler';
import clusterService from './cluster-service';

describe('cluster handler', () => {
describe('clusterService', () => {
describe('getCluster', () => {
beforeEach(() => {
clusterHandler.clearCache();
clusterService.clearCache();
});

describe('cache = null', () => {
Expand All @@ -36,15 +36,14 @@ describe('cluster handler', () => {
};

const ctx = {
body: null,
cadence: {
describeCluster: jest.fn().mockImplementation(() => cluster),
},
};

await clusterHandler.getCluster(ctx);
const data = await clusterService.getCluster(ctx);

expect(ctx.body).toEqual(cluster);
expect(data).toEqual(cluster);
});
});

Expand All @@ -57,28 +56,26 @@ describe('cluster handler', () => {
};

const ctx = {
body: null,
cadence: {
describeCluster: jest.fn().mockImplementation(() => clusterCache),
},
};

await clusterHandler.getCluster(ctx);
const data = await clusterService.getCluster(ctx);

expect(ctx.body).toEqual(clusterCache);
expect(data).toEqual(clusterCache);

const clusterUpdated = {
persistenceInfo: {},
membershipInfo: null,
version: 2,
};

ctx.body = null;
ctx.cadence.describeCluster.mockImplementation(() => clusterUpdated);

await clusterHandler.getCluster(ctx);
const updatedData = await clusterService.getCluster(ctx);

expect(ctx.body).toEqual(clusterCache);
expect(updatedData).toEqual(clusterCache);
});

describe('and time passes over CLUSTER_CACHE_TTL', () => {
Expand All @@ -90,7 +87,6 @@ describe('cluster handler', () => {
};

const ctx = {
body: null,
cadence: {
describeCluster: jest.fn().mockImplementation(() => clusterCache),
},
Expand All @@ -103,17 +99,16 @@ describe('cluster handler', () => {
new Date(Date.UTC(2020, 2, 10)).getTime()
);

await clusterHandler.getCluster(ctx);
const data = await clusterService.getCluster(ctx);

expect(ctx.body).toEqual(clusterCache);
expect(data).toEqual(clusterCache);

const clusterUpdated = {
persistenceInfo: {},
membershipInfo: null,
version: 2,
};

ctx.body = null;
ctx.cadence.describeCluster.mockImplementation(() => clusterUpdated);

// set current time to an hour into the future
Expand All @@ -123,9 +118,9 @@ describe('cluster handler', () => {
new Date(Date.UTC(2020, 2, 10, 2)).getTime()
);

await clusterHandler.getCluster(ctx);
const updatedData = await clusterService.getCluster(ctx);

expect(ctx.body).toEqual(clusterUpdated);
expect(updatedData).toEqual(clusterUpdated);
});
});
});
Expand Down
26 changes: 26 additions & 0 deletions server/router/services/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2021 Uber Technologies Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

const clusterService = require('./cluster-service');

module.exports = {
clusterService,
};