Skip to content

Commit

Permalink
Add Azure Redis private endpoint; config geoprocessing for deployment…
Browse files Browse the repository at this point in the history
…; refactor redis config loading
  • Loading branch information
tiagojsag committed Feb 24, 2022
1 parent 034a4da commit 56f61d4
Show file tree
Hide file tree
Showing 44 changed files with 372 additions and 331 deletions.
11 changes: 5 additions & 6 deletions api/apps/api/config/custom-environment-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@
"geoprocessing": {
"url": "GEOPROCESSING_URL"
},
"redisApi": {
"connection": {
"host": "REDIS_HOST",
"port": "REDIS_PORT",
"password": "REDIS_PASSWORD"
}
"redis": {
"host": "REDIS_HOST",
"port": "REDIS_PORT",
"password": "REDIS_PASSWORD",
"useTLS": "REDIS_USE_TLS"
},
"api": {
"url": "API_SERVICE_URL",
Expand Down
13 changes: 6 additions & 7 deletions api/apps/api/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
]
}
},
"redisApi": {
"connection": {
"host": "marxan-redis",
"port": 6379,
"password": null
},
"concurrency": 50
"redis": {
"host": "marxan-redis",
"port": 6379,
"password": null,
"useTLS": false,
"concurrency" : 50
},
"postgresApi": {
"url": null,
Expand Down
3 changes: 2 additions & 1 deletion api/apps/api/src/modules/queue/queue-options.provider.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { FactoryProvider } from '@nestjs/common';
import { QueueOptions } from 'bullmq';
import * as config from 'config';
import { getRedisConfig } from '@marxan-api/utils/redisConfig.utils';

export const queueOptionsToken = Symbol('queue options token');
export const queueOptionsProvider: FactoryProvider<QueueOptions> = {
provide: queueOptionsToken,
useFactory: () => {
return {
...config.get('redisApi'),
...getRedisConfig(),
defaultJobOptions: config.get('jobOptions'),
};
},
Expand Down
23 changes: 23 additions & 0 deletions api/apps/api/src/utils/redisConfig.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as config from 'config';
import { QueueBaseOptions } from 'bullmq/dist/interfaces/queue-options';

export function getRedisConfig() {
const redisConfig: Record<string, any> = config.get('redis');
const useTLS: boolean = `${redisConfig.useTLS}`.toLowerCase() === 'true';

const redisSettings: QueueBaseOptions = {
connection: {
host: redisConfig.host,
port: redisConfig.port,
password: redisConfig.password,
tls: useTLS
? {
host: redisConfig.host,
port: redisConfig.port,
}
: undefined,
},
};

return redisSettings;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Queue, Worker } from 'bullmq';
import * as config from 'config';
import waitForExpect from 'wait-for-expect';
import { QueueModule } from '@marxan-api/modules/queue/queue.module';
import { getRedisConfig } from '@marxan-api/utils/redisConfig.utils';

const queueName = baseQueueName + '_test_' + Date.now();

Expand Down Expand Up @@ -124,7 +125,10 @@ const getFixtures = async () => {
async () => {
/**/
},
config.get('redisApi'),
{
...getRedisConfig(),
concurrency: config.get('redis.concurrency'),
},
);
workers.push(worker);
},
Expand Down
3 changes: 2 additions & 1 deletion api/apps/api/test/utils/queues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { assertDefined, FieldsOf } from '@marxan/utils';
import { QueueBuilder } from '@marxan-api/modules/queue/queue.builder';
import { QueueNameToken } from '@marxan-api/modules/queue/queue.tokens';
import * as config from 'config';
import { getRedisConfig } from '@marxan-api/utils/redisConfig.utils';

@Injectable({
scope: Scope.TRANSIENT,
Expand Down Expand Up @@ -34,7 +35,7 @@ export class FakeQueue implements Partial<Queue> {
private static instanceKeys: Record<string, Key | undefined> = {};
public jobs: Record<string, Job> = {};
private readonly queueBase = new QueueBase(v4(), {
...config.get('redisApi'),
...getRedisConfig(),
});

constructor(
Expand Down
11 changes: 5 additions & 6 deletions api/apps/geoprocessing/config/custom-environment-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@
"secret": "API_AUTH_X_API_KEY"
}
},
"redisApi": {
"connection": {
"host": "REDIS_HOST",
"port": "REDIS_PORT",
"password": "REDIS_PASSWORD"
}
"redis": {
"host": "REDIS_HOST",
"port": "REDIS_PORT",
"password": "REDIS_PASSWORD",
"useTLS": "REDIS_USE_TLS"
},
"api": {
"url": "API_SERVICE_URL"
Expand Down
13 changes: 6 additions & 7 deletions api/apps/geoprocessing/config/default.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"redisApi": {
"concurrency": 50,
"connection": {
"host": "marxan-redis",
"port": 6379,
"password": null
}
"redis": {
"useTLS": false,
"host": "marxan-redis",
"port": 6379,
"password": null,
"concurrency" : 50
},
"postgresApi": {
"url": null,
Expand Down
8 changes: 3 additions & 5 deletions api/apps/geoprocessing/config/test.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"redisApi": {
"connection": {
"host": "test-e2e-redis",
"port": "6379"
}
"redis": {
"host": "test-e2e-redis",
"port": "6379"
}
}
6 changes: 5 additions & 1 deletion api/apps/geoprocessing/src/modules/worker/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import * as config from 'config';
import { Injectable } from '@nestjs/common';
import { WorkerOptions } from 'bullmq';
import { getRedisConfig } from '@marxan-geoprocessing/utils/redisConfig.utils';

@Injectable()
export class Config {
public readonly redis: WorkerOptions;

constructor() {
this.redis = config.get('redisApi');
this.redis = {
...getRedisConfig(),
concurrency: config.get('redis.concurrency'),
};
}
}
23 changes: 23 additions & 0 deletions api/apps/geoprocessing/src/utils/redisConfig.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as config from 'config';
import { QueueBaseOptions } from 'bullmq/dist/interfaces/queue-options';

export function getRedisConfig() {
const redisConfig: Record<string, any> = config.get('redis');
const useTLS: boolean = `${redisConfig.useTLS}`.toLowerCase() === 'true';

const redisSettings: QueueBaseOptions = {
connection: {
host: redisConfig.host,
port: redisConfig.port,
password: redisConfig.password,
tls: useTLS
? {
host: redisConfig.host,
port: redisConfig.port,
}
: undefined,
},
};

return redisSettings;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
ScenarioCostSurfaceRepository,
} from '@marxan/scenario-cost-surface';
import { AppConfig } from '@marxan-geoprocessing/utils/config.utils';
import { getRedisConfig } from '@marxan-geoprocessing/utils/redisConfig.utils';

jest.setTimeout(35000);

Expand Down Expand Up @@ -110,7 +111,7 @@ async function getFixtures() {
const fileRepository = application.get(ScenarioCostSurfaceRepository);

const queue = new Queue(`cost-surface-template-creation`, {
...config.get('redisApi'),
...getRedisConfig(),
});

let processedScenario: string | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
runWorkerQueueNameToken,
sandboxRunnerToken,
} from '@marxan-geoprocessing/modules/scenarios/runs/tokens';
import { getRedisConfig } from '@marxan-geoprocessing/utils/redisConfig.utils';

let fixtures: PromiseType<ReturnType<typeof getFixtures>>;

Expand Down Expand Up @@ -100,9 +101,10 @@ async function getFixtures() {
}).compile();
await testingModule.enableShutdownHooks().init();

const queue = new Queue(testingModule.get(runWorkerQueueNameToken), {
...config.get('redisApi'),
});
const queue = new Queue(
testingModule.get(runWorkerQueueNameToken),
getRedisConfig(),
);
const fakeMarxanRunner = testingModule.get(FakeMarxanRunner);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
WorkerModule,
WorkerProcessor,
} from '../../src/modules/worker';
import { getRedisConfig } from '@marxan-geoprocessing/utils/redisConfig.utils';

let app: TestingModule;
let queue: Queue;
Expand All @@ -27,9 +28,7 @@ beforeAll(async () => {
app = await sandbox.init();
processor = app.get(ExampleProcessingService);

queue = new Queue(queueName, {
...config.get('redisApi'),
});
queue = new Queue(queueName, getRedisConfig());
});

afterAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as config from 'config';

import { ExampleWorkerJobProcessor } from './bullmq-worker-code';
import { WorkerModule, WorkerBuilder } from '../../src/modules/worker';
import { getRedisConfig } from '@marxan-geoprocessing/utils/redisConfig.utils';

let app: TestingModule;
let queue: Queue;
Expand All @@ -23,9 +24,7 @@ beforeAll(async () => {
app = await sandbox.init();
processor = app.get(ExampleProcessingService);

queue = new Queue(queueName, {
...config.get('redisApi'),
});
queue = new Queue(queueName, getRedisConfig());
});

afterAll(async () => {
Expand Down
Loading

0 comments on commit 56f61d4

Please sign in to comment.