Skip to content

Commit

Permalink
Merge pull request #543 from beethovenxfi/cdk
Browse files Browse the repository at this point in the history
CDK
  • Loading branch information
franzns authored Dec 7, 2023
2 parents d6f20d1 + cc15318 commit 93ed17c
Show file tree
Hide file tree
Showing 16 changed files with 643 additions and 54 deletions.
11 changes: 11 additions & 0 deletions .platform/confighooks/prebuild/env-secret.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
secrets=$(aws secretsmanager get-secret-value --secret-id api-secrets --region $AWS_REGION | jq -r '.SecretString')

IFS=$'\n' read -r -d '' -a keys <<< "$(echo $secrets | jq -r 'keys[]')"

touch .env
chown webapp:webapp .env
for key in "${keys[@]}"; do
value=$(echo $secrets | jq -r ".\"$key\"")
printf "%s=%s\n" "$key" "$value"
done > .env
20 changes: 20 additions & 0 deletions .platform/confighooks/predeploy/env-secret.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh
# In case we merge two env secrets into a flattened one in the same region
# secrets=$(aws secretsmanager get-secret-value --secret-id api-secrets --region $AWS_REGION | jq -r '.SecretString')

# IFS=$'\n' read -r -d '' -a key_value_pairs <<< "$(echo $secrets | jq -r --arg prefix "$PREFIX" 'to_entries[] | select((.key | ascii_upcase) | startswith(($prefix+"_") | ascii_upcase)) | ((.key | sub(($prefix+"_"); ""; "i")) + "=" + .value)')"

# for pair in "${key_value_pairs[@]}"; do
# echo "$pair"
# done

secrets=$(aws secretsmanager get-secret-value --secret-id api-secrets --region $AWS_REGION | jq -r '.SecretString')

IFS=$'\n' read -r -d '' -a keys <<< "$(echo $secrets | jq -r 'keys[]')"

touch /var/app/staging/.env
chown webapp:webapp /var/app/staging/.env
for key in "${keys[@]}"; do
value=$(echo $secrets | jq -r ".\"$key\"")
printf "%s=%s\n" "$key" "$value"
done >> /var/app/staging/.env
11 changes: 11 additions & 0 deletions .platform/hooks/prebuild/env-secret.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
secrets=$(aws secretsmanager get-secret-value --secret-id api-secrets --region $AWS_REGION | jq -r '.SecretString')

IFS=$'\n' read -r -d '' -a keys <<< "$(echo $secrets | jq -r 'keys[]')"

touch .env
chown webapp:webapp .env
for key in "${keys[@]}"; do
value=$(echo $secrets | jq -r ".\"$key\"")
printf "%s=%s\n" "$key" "$value"
done > .env
20 changes: 20 additions & 0 deletions .platform/hooks/predeploy/env-secret.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh
# In case we merge two env secrets into a flattened one in the same region
# secrets=$(aws secretsmanager get-secret-value --secret-id api-secrets --region $AWS_REGION | jq -r '.SecretString')

# IFS=$'\n' read -r -d '' -a key_value_pairs <<< "$(echo $secrets | jq -r --arg prefix "$PREFIX" 'to_entries[] | select((.key | ascii_upcase) | startswith(($prefix+"_") | ascii_upcase)) | ((.key | sub(($prefix+"_"); ""; "i")) + "=" + .value)')"

# for pair in "${key_value_pairs[@]}"; do
# echo "$pair"
# done

secrets=$(aws secretsmanager get-secret-value --secret-id api-secrets --region $AWS_REGION | jq -r '.SecretString')

IFS=$'\n' read -r -d '' -a keys <<< "$(echo $secrets | jq -r 'keys[]')"

touch /var/app/staging/.env
chown webapp:webapp /var/app/staging/.env
for key in "${keys[@]}"; do
value=$(echo $secrets | jq -r ".\"$key\"")
printf "%s=%s\n" "$key" "$value"
done >> /var/app/staging/.env
1 change: 1 addition & 0 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async function startServer() {
// tracesSampleRate: 0.005,
environment: `multichain-${env.DEPLOYMENT_ENV}`,
enabled: env.NODE_ENV === 'production',
ignoreErrors: [/.*error: Provide.*chain.*param/],
integrations: [
// new Tracing.Integrations.Apollo(),
// new Tracing.Integrations.GraphQL(),
Expand Down
15 changes: 11 additions & 4 deletions app/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export const schema = {
SENTRY_DSN: String,
SENTRY_AUTH_TOKEN: String,
AWS_REGION: String,
PROTOCOL: String,
PROTOCOL: {
optional: true,
type: String,
},
INFURA_API_KEY: {
optional: true,
type: String,
Expand All @@ -30,10 +33,14 @@ export const schema = {
optional: true,
type: String,
},
WORKER_QUEUE_URL: String,
WORKER_QUEUE_URL: {
optional: true,
type: String,
},
DATABASE_URL: String,
};

export const env: Env = load(schema, {
path: resolve(__dirname, '../.env'),
overrideProcessEnv: process.env.NODE_ENV !== 'production',
path: resolve(__dirname, `../../.env`),
overrideProcessEnv: true,
});
4 changes: 4 additions & 0 deletions buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ env:
VEBALLOCKS_SUBGRAPH: 'https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-gauges'
phases:
install:
runtime-versions:
nodejs: 18
commands:
- yarn install
build:
Expand All @@ -23,6 +25,8 @@ phases:
- yarn build
post_build:
commands:
- rm -fr node_modules
- yarn install --prod
- echo Build completed on `date`
# - echo Pushing to graph cdn
# - npx graphcdn push
Expand Down
9 changes: 7 additions & 2 deletions modules/pool/lib/pool-usd-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,17 @@ export class PoolUsdDataService {
for (const pool of subgraphPools) {
const staked = stakedUsers.find((stakedUser) => stakedUser.poolId === pool.id);

// Limit numbers, as we have seen some pools with skewd values
const max = 1e18;
const lifetimeVolume = Math.min(parseFloat(pool.totalSwapVolume), max);
const lifetimeSwapFees = Math.min(parseFloat(pool.totalSwapFee), max);

updates.push(
prisma.prismaPoolDynamicData.update({
where: { id_chain: { id: pool.id, chain: this.chain } },
data: {
lifetimeVolume: parseFloat(pool.totalSwapVolume),
lifetimeSwapFees: parseFloat(pool.totalSwapFee),
lifetimeVolume: lifetimeVolume,
lifetimeSwapFees: lifetimeSwapFees,
holdersCount: parseInt(pool.holdersCount) + (staked?._count.userAddress || 0),
swapsCount: parseInt(pool.swapsCount),
},
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"@graphql-tools/merge": "^8.2.1",
"@prisma/client": "^4.1.1",
"@sanity/client": "^2.23.1",
"@sentry/node": "^7.56.0",
"@sentry/node": "^7.0.0",
"@sentry/profiling-node": "^1.2.6",
"@sentry/tracing": "^7.56.0",
"apollo-server-core": "^3.5.0",
"apollo-server-express": "^3.5.0",
Expand Down
2 changes: 1 addition & 1 deletion prisma/base.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "linux-arm64-openssl-1.0.x", "rhel-openssl-1.0.x"]
binaryTargets = ["native", "linux-arm64-openssl-1.0.x"] //, "linux-arm64-openssl-3.0.x"]
}

datasource db {
Expand Down
9 changes: 8 additions & 1 deletion prisma/prisma-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { PrismaClient } from '@prisma/client';
import { env } from '../app/env';

export let prisma = new PrismaClient();
export let prisma = new PrismaClient({
datasources: {
db: {
url: env.DATABASE_URL,
},
},
});

// Debugging query times
// export let prisma = new PrismaClient({
Expand Down
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "linux-arm64-openssl-1.0.x", "rhel-openssl-1.0.x"]
binaryTargets = ["native", "linux-arm64-openssl-1.0.x"] //, "linux-arm64-openssl-3.0.x"]
}

datasource db {
Expand Down
31 changes: 26 additions & 5 deletions worker/job-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,30 @@ async function runIfNotAlreadyRunning(
res.sendStatus(200);
return;
}

const sentryTags = {
job: id,
chainId,
};

const sentryTransaction = Sentry.startTransaction({
op: 'http',
name: `POST /${jobId}`,
tags: sentryTags,
});

Sentry.configureScope((scope) => scope.setSpan(sentryTransaction));

const sentryChildSpan = sentryTransaction.startChild({
op: 'background-job',
description: `Running job ${jobId}`,
tags: sentryTags,
});

const startJobTime = moment();
try {
runningJobs.add(jobId);

// TODO, this does not seem to work properly as it is a "global" scope
Sentry.configureScope((scope) => {
scope.setTransactionName(`POST /${jobId}`);
});

console.time(jobId);
console.log(`Start job ${jobId}-start`);

Expand All @@ -54,6 +69,8 @@ async function runIfNotAlreadyRunning(
}
console.log(`Successful job ${jobId}-done`);
} catch (error) {
Sentry.captureException(error);

const durationError = moment.duration(moment().diff(startJobTime)).asSeconds();
if (process.env.AWS_ALERTS === 'true') {
await cronsMetricPublisher.publish(`${jobId}-error`);
Expand All @@ -62,6 +79,9 @@ async function runIfNotAlreadyRunning(
console.log(`Error job ${jobId}-error`, error);
next(error);
} finally {
sentryChildSpan.finish();
sentryTransaction.finish();

runningJobs.delete(jobId);
console.timeEnd(jobId);
res.sendStatus(200);
Expand All @@ -71,6 +91,7 @@ async function runIfNotAlreadyRunning(
export function configureWorkerRoutes(app: Express) {
app.post('/', async (req, res, next) => {
const job = req.body as { name: string; chain: string };

console.log(`Current jobqueue length: ${runningJobs.size}`);
const chainId = job.chain;
initRequestScopedContext();
Expand Down
22 changes: 15 additions & 7 deletions worker/job-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ import { SendMessageCommand, SendMessageCommandInput, SQSClient } from '@aws-sdk
import { env } from '../app/env';

class WokerQueue {
constructor(private readonly client: SQSClient, private readonly queueUrl: string) {}
constructor(private readonly client: SQSClient, private readonly queueUrl?: string) {}

public async sendWithInterval(json: string, intervalMs: number, deDuplicationId?: string): Promise<void> {
try {
if (env.WORKER_QUEUE_URL.match(/localhost/)) {
await this.sendLocalMessage(json );
if (this.queueUrl === undefined) {
return;
}

if (this.queueUrl.match(/localhost/)) {
await this.sendLocalMessage(json);
} else {
await this.sendMessage(json, deDuplicationId);
}
console.log(`Sent message to schedule job on queue ${env.WORKER_QUEUE_URL}: ${json}`);
console.log(`Sent message to schedule job on queue ${this.queueUrl}: ${json}`);
} catch (error) {
console.log(error);
Sentry.captureException(error);
Expand All @@ -26,10 +30,14 @@ class WokerQueue {
}

public async sendLocalMessage(json: string): Promise<void> {
await axios.post(env.WORKER_QUEUE_URL, json, {
if (this.queueUrl === undefined) {
throw new Error('WORKER_QUEUE_URL is undefined');
}

await axios.post(this.queueUrl, json, {
headers: {
'Content-Type': 'application/json'
}
'Content-Type': 'application/json',
},
});
}

Expand Down
4 changes: 4 additions & 0 deletions worker/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from 'express';
import * as Sentry from '@sentry/node';
import { ProfilingIntegration } from '@sentry/profiling-node';
import { env } from '../app/env';
import { configureWorkerRoutes } from './job-handlers';

Expand All @@ -13,7 +14,10 @@ export async function startWorker() {
integrations: [
// new Tracing.Integrations.Express({ app }),
new Sentry.Integrations.Http({ tracing: true }),
new ProfilingIntegration(),
],
tracesSampleRate: 0.2,
profilesSampleRate: 1.0,
});

app.use(Sentry.Handlers.requestHandler());
Expand Down
Loading

0 comments on commit 93ed17c

Please sign in to comment.