Skip to content

Commit

Permalink
Fb/add env frequency variable (#81)
Browse files Browse the repository at this point in the history
* add env variable for cds and reg polling frequency

* add documentation
  • Loading branch information
rlindner81 authored May 14, 2024
1 parent d124643 commit dac8e16
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 28 deletions.
9 changes: 5 additions & 4 deletions docs/cap-multitenancy/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ Commands for this area are:

## Environment

| environment variable | effect |
| :-------------------- | :-------------------------------------------------------------------- |
| `MTX_CDS_APP` | override configured app for server (running `@sap/cds-mtxs`) accesses |
| `MTX_CDS_CONCURRENCY` | change concurrency used in server calls (default is 10) |
| environment variable | effect |
| :-------------------- | :------------------------------------------------------------------------ |
| `MTX_CDS_APP` | override configured app for server (running `@sap/cds-mtxs`) accesses |
| `MTX_CDS_CONCURRENCY` | change concurrency used for server calls (default is 10) |
| `MTX_CDS_FREQUENCY` | change polling frequency milliseconds for server calls (default is 15000) |

## List and Long List

Expand Down
8 changes: 4 additions & 4 deletions docs/hana-management/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ Commands for this area are:

## Environment

| environment variable | effect |
| :-------------------- | :------------------------------------------------------- |
| `MTX_HDI_APP` | override configured app for `service-manager` accesses |
| `MTX_HDI_CONCURRENCY` | change concurrency used in service calls (default is 10) |
| environment variable | effect |
| :-------------------- | :-------------------------------------------------------- |
| `MTX_HDI_APP` | override configured app for `service-manager` accesses |
| `MTX_HDI_CONCURRENCY` | change concurrency used for service calls (default is 10) |

## List and Long List

Expand Down
8 changes: 5 additions & 3 deletions docs/tenant-registry/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ Commands for this area are:

## Environment

| environment variable | effect |
| :------------------- | :--------------------------------------------------- |
| `MTX_REG_APP` | override configured app for `saas-registry` accesses |
| environment variable | effect |
| :-------------------- | :------------------------------------------------------------------------- |
| `MTX_REG_APP` | override configured app for `saas-registry` accesses |
| `MTX_REG_CONCURRENCY` | change concurrency used for service calls (default is 10) |
| `MTX_REG_FREQUENCY` | change polling frequency milliseconds for service calls (default is 15000) |

## List and Long List

Expand Down
11 changes: 11 additions & 0 deletions src/shared/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const ENV = Object.freeze({
SERVER_APP: "MTX_SRV_APP",
APP_SUFFIX: "MTX_APP_SUFFIX",
REG_CONCURRENCY: "MTX_REG_CONCURRENCY",
REG_FREQUENCY: "MTX_REG_FREQUENCY",
CDS_CONCURRENCY: "MTX_CDS_CONCURRENCY",
CDS_FREQUENCY: "MTX_CDS_FREQUENCY",
HDI_CONCURRENCY: "MTX_HDI_CONCURRENCY",
});

Expand Down Expand Up @@ -369,6 +371,14 @@ const makeOneTime = (cb) => {
};
};

const parseIntWithFallback = (input, fallback) => {
if (typeof input !== "string") {
return fallback;
}
const result = parseInt(input);
return isNaN(result) ? fallback : result;
};

module.exports = {
ENV,
isPortFree,
Expand Down Expand Up @@ -396,4 +406,5 @@ module.exports = {
safeUnshift,
escapeRegExp,
makeOneTime,
parseIntWithFallback,
};
16 changes: 8 additions & 8 deletions src/submodules/capMultitenancy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ const {
limiter,
formatTimestampsWithRelativeDays,
isObject,
parseIntWithFallback,
} = require("../shared/static");
const { assert, assertAll } = require("../shared/error");
const { request } = require("../shared/request");

const CDS_UPGRADE_APP_INSTANCE = 0;
const CDS_JOB_POLL_FREQUENCY = 15000;
const CDS_REQUEST_CONCURRENCY_FALLBACK = 10;
const CDS_JOB_POLL_FREQUENCY_FALLBACK = 15000;
const CDS_CHANGE_TIMEOUT = 30 * 60 * 1000;
const CDS_CHANGE_TIMEOUT_TEXT = "30min";

const writeFileAsync = promisify(writeFile);
const cdsRequestConcurrency = process.env[ENV.CDS_CONCURRENCY]
? parseInt(process.env[ENV.CDS_CONCURRENCY])
: CDS_REQUEST_CONCURRENCY_FALLBACK;
const cdsRequestConcurrency = parseIntWithFallback(process.env[ENV.CDS_CONCURRENCY], CDS_REQUEST_CONCURRENCY_FALLBACK);
const cdsPollFrequency = parseIntWithFallback(process.env[ENV.CDS_FREQUENCY], CDS_JOB_POLL_FREQUENCY_FALLBACK);

const _isMtxs = async (context) => {
if (_isMtxs._result === undefined) {
Expand Down Expand Up @@ -187,7 +187,7 @@ const _cdsUpgradeMtxs = async (
});
const upgradeResponseData = await _safeMaterializeJson(upgradeResponse, "upgrade");
const jobId = upgradeResponseData.ID;
console.log("started upgrade on server with jobId %s polling interval %isec", jobId, CDS_JOB_POLL_FREQUENCY / 1000);
console.log("started upgrade on server with jobId %s polling interval %isec", jobId, cdsPollFrequency / 1000);
const upgradeTenantEntries = upgradeResponseData.tenants && Object.entries(upgradeResponseData.tenants);
assert(upgradeTenantEntries, "no tenants found in response for upgrade\n%j", upgradeResponseData);
const countLength = String(upgradeTenantEntries.length).length;
Expand All @@ -198,7 +198,7 @@ const _cdsUpgradeMtxs = async (
let hasChangeTimeout = false;

while (true) {
await sleep(CDS_JOB_POLL_FREQUENCY);
await sleep(cdsPollFrequency);
const pollJobResponse = await request({
url: cfRouteUrl,
pathname: `/-/cds/jobs/pollJob(ID='${jobId}')`,
Expand Down Expand Up @@ -275,12 +275,12 @@ const _cdsUpgradeMtx = async (
});
const upgradeResponseData = await _safeMaterializeJson(upgradeResponse, "upgrade");
const jobId = upgradeResponseData.jobID;
console.log("started upgrade on server with jobId %s polling interval %isec", jobId, CDS_JOB_POLL_FREQUENCY / 1000);
console.log("started upgrade on server with jobId %s polling interval %isec", jobId, cdsPollFrequency / 1000);

let pollJobResponseData;

while (true) {
await sleep(CDS_JOB_POLL_FREQUENCY);
await sleep(cdsPollFrequency);
const pollJobResponse = await request({
url: cfRouteUrl,
pathname: `/mtx/v1/model/status/${jobId}`,
Expand Down
8 changes: 5 additions & 3 deletions src/submodules/hanaManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
tryJsonParse,
isObject,
makeOneTime,
parseIntWithFallback,
} = require("../shared/static");
const { assert } = require("../shared/error");
const { request } = require("../shared/request");
Expand All @@ -22,9 +23,10 @@ const SERVICE_MANAGER_IDEAL_BINDING_COUNT = 1;
const SENSITIVE_CREDENTIAL_FIELDS = ["password", "hdi_password"];
const HDI_SHARED_SERVICE_PLAN_NAME = "hdi-shared";

const hdiRequestConcurrency = process.env[ENV.HDI_CONCURRENCY]
? parseInt(process.env[ENV.HDI_CONCURRENCY])
: SERVICE_MANAGER_REQUEST_CONCURRENCY_FALLBACK;
const hdiRequestConcurrency = parseIntWithFallback(
process.env[ENV.HDI_CONCURRENCY],
SERVICE_MANAGER_REQUEST_CONCURRENCY_FALLBACK
);

const isValidTenantId = (input) => input && /^[0-9a-z-_/]+$/i.test(input);

Expand Down
15 changes: 9 additions & 6 deletions src/submodules/tenantRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ const {
formatTimestampsWithRelativeDays,
resolveTenantArg,
limiter,
parseIntWithFallback,
} = require("../shared/static");
const { assert } = require("../shared/error");
const { request } = require("../shared/request");

const REGISTRY_PAGE_SIZE = 200;
const REGISTRY_JOB_POLL_FREQUENCY = 15000;
const REGISTRY_JOB_POLL_FREQUENCY_FALLBACK = 15000;
const REGISTRY_REQUEST_CONCURRENCY_FALLBACK = 10;
const TENANT_UPDATABLE_STATES = ["SUBSCRIBED", "UPDATE_FAILED"];
const JOB_STATE = Object.freeze({
Expand All @@ -30,9 +31,11 @@ const JOB_STATE = Object.freeze({
FAILED: "FAILED",
});

const regRequestConcurrency = process.env[ENV.REG_CONCURRENCY]
? parseInt(process.env[ENV.REG_CONCURRENCY])
: REGISTRY_REQUEST_CONCURRENCY_FALLBACK;
const regRequestConcurrency = parseIntWithFallback(
process.env[ENV.REG_CONCURRENCY],
REGISTRY_REQUEST_CONCURRENCY_FALLBACK
);
const regPollFrequency = parseIntWithFallback(process.env[ENV.REG_FREQUENCY], REGISTRY_JOB_POLL_FREQUENCY_FALLBACK);

const _registrySubscriptionsPaged = async (context, tenant) => {
const { subdomain: filterSubdomain, tenantId: filterTenantId } = resolveTenantArg(tenant);
Expand Down Expand Up @@ -121,7 +124,7 @@ const _registryJobPoll = async (context, location, { skipFirst = false } = {}) =
const { saas_registry_url } = credentials;
while (true) {
if (!skipFirst) {
await sleep(REGISTRY_JOB_POLL_FREQUENCY);
await sleep(regPollFrequency);
skipFirst = false;
}
const token = await context.getCachedUaaTokenFromCredentials(credentials);
Expand Down Expand Up @@ -192,7 +195,7 @@ const _registryCallForTenant = async (
const [location] = response.headers.raw().location;
const responseText = await response.text();
console.log("response: %s", responseText);
console.log("polling job %s with interval %isec", location, REGISTRY_JOB_POLL_FREQUENCY / 1000);
console.log("polling job %s with interval %isec", location, regPollFrequency / 1000);

const jobResult = await _registryJobPoll(context, location);

Expand Down

0 comments on commit dac8e16

Please sign in to comment.