Skip to content

Commit

Permalink
decouple indexer and web services (#517)
Browse files Browse the repository at this point in the history
* decouple indexer and web

* production

* production

* fix web deploy

* publish database schema name

* deploy on main

* delete staging toml
  • Loading branch information
boudra authored Apr 11, 2024
1 parent 6073073 commit c44a829
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 219 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/deploy-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Deploy Branch

on:
push:
branches: [main, release]
branches: [main]

jobs:
build-and-deploy:
Expand All @@ -14,27 +14,27 @@ jobs:
- name: Set up fly
uses: superfly/flyctl-actions/setup-flyctl@master

- name: Set variables for staging
run: |
echo "DEPLOYMENT_ENVIRONMENT=staging" >> $GITHUB_ENV
echo "DEPLOYMENT_URL=https://indexer-staging.fly.dev" >> $GITHUB_ENV
if: ${{ github.ref == 'refs/heads/main' }}

- name: Set variables for production
run: |
echo "DEPLOYMENT_ENVIRONMENT=production" >> $GITHUB_ENV
echo "DEPLOYMENT_URL=https://indexer-production.fly.dev" >> $GITHUB_ENV
if: ${{ github.ref == 'refs/heads/release' }}
echo "DEPLOYMENT_URL=https://indexer-v2.fly.dev" >> $GITHUB_ENV
if: ${{ github.ref == 'refs/heads/main' }}

- name: Build and test
run: |
flyctl -c fly.${{ env.DEPLOYMENT_ENVIRONMENT }}.toml deploy --remote-only --build-only
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

- name: Deploy
- name: Deploy Indexer
run: |
flyctl -c fly.${{ env.DEPLOYMENT_ENVIRONMENT }}.toml deploy --process-groups=indexer --remote-only --wait-timeout=7200 --env BUILD_TAG=`git rev-parse --short HEAD`
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

- name: Deploy HTTP
run: |
flyctl -c fly.${{ env.DEPLOYMENT_ENVIRONMENT }}.toml deploy --remote-only --wait-timeout=7200 --env BUILD_TAG=`git rev-parse --short HEAD`
flyctl -c fly.${{ env.DEPLOYMENT_ENVIRONMENT }}.toml deploy --process-groups=web --remote-only --wait-timeout=7200 --env BUILD_TAG=`git rev-parse --short HEAD`
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

Expand Down
94 changes: 51 additions & 43 deletions fly.production.toml
Original file line number Diff line number Diff line change
@@ -1,56 +1,64 @@
app = "indexer-production"
primary_region = "den"
kill_signal = "SIGINT"
kill_timeout = 5
# fly.toml app configuration file generated for indexer-v2 on 2024-03-26T14:50:51+07:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'indexer-v2'
primary_region = 'den'
kill_signal = 'SIGINT'
kill_timeout = '5s'

[experimental]
auto_rollback = true

[build]

[deploy]
wait_timeout = "60m"

[env]
PORT = "8080"
DEPLOYMENT_ENVIRONMENT = "production"
LOG_LEVEL = "debug"
STORAGE_DIR = "/mnt/indexer"
INDEXED_CHAINS = "mainnet,optimism,fantom,pgn-testnet,pgn-mainnet,arbitrum,arbitrum-goerli,polygon-mumbai,polygon,goerli,sepolia,avalanche,avalanche-fuji"
ENABLE_RESOURCE_MONITOR = "true"
NODE_OPTIONS="--max-old-space-size=7168"
# BUILD_TAG = # set in deploy github action
DEPLOYMENT_ENVIRONMENT = 'production'
ENABLE_RESOURCE_MONITOR = 'true'
INDEXED_CHAINS = 'scroll'
LOG_LEVEL = 'debug'
NODE_OPTIONS = '--max-old-space-size=4096'
PORT = '8080'
STORAGE_DIR = '/mnt/indexer'

[processes]
web = "npm start"
indexer = 'npm start -- --indexer --http'
web = 'npm start -- --http --http-wait-for-sync=false'

[mounts]
source="indexer_production"
destination="/mnt/indexer"
[[mounts]]
source = 'indexer_staging'
destination = '/mnt/indexer'
initial_size = "50gb"
processes = ["indexer", "web"]

[[services]]
protocol = "tcp"
[http_service]
internal_port = 8080
processes = ["web"]

[[services.ports]]
port = 80
handlers = ["http"]
force_https = true
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 1
processes = ['web']
[http_service.concurrency]
type = "requests"
soft_limit = 200
hard_limit = 250

[[services.ports]]
port = 443
handlers = ["tls", "http"]

[services.concurrency]
type = "connections"
hard_limit = 100
soft_limit = 20

[[services.tcp_checks]]
interval = "30s"
[checks]
[checks.http]
grace_period = "30s"
interval = "15s"
method = "get"
path = '/api/v1/status'
port = 8080
timeout = "10s"
grace_period = "1m"
type = "http"
processes = ['web', 'indexer']

[[services.http_checks]]
interval = "60s"
grace_period = "1m"
timeout = "5s"
method = "get"
path = "/api/v1/status"
protocol = "http"
[[vm]]
memory = '4gb'
cpu_kind = 'performance'
cpus = 2
55 changes: 0 additions & 55 deletions fly.staging.toml

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"types": "dist/src/indexer/index.d.ts",
"scripts": {
"start": "node dist/src/index.js",
"dev": "sh -c 'tsx watch src/index.ts --http-wait-for-sync=false $@ | pino-pretty' --",
"dev": "sh -c 'tsx watch src/index.ts --http --indexer --http-wait-for-sync=false $@ | pino-pretty' --",
"build": "tsc",
"lint": "eslint src && prettier --check src",
"format": "prettier --write src",
Expand Down
27 changes: 23 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,8 @@ export type Config = {
passportScorerId: number;
logLevel: "trace" | "debug" | "info" | "warn" | "error";
httpServerWaitForSync: boolean;
httpServerEnabled: boolean;
indexerEnabled: boolean;
ipfsGateway: string;
coingeckoApiKey: string | null;
coingeckoApiUrl: string;
Expand Down Expand Up @@ -1588,6 +1590,12 @@ export function getConfig(): Config {
"http-wait-for-sync": {
type: "string",
},
http: {
type: "boolean",
},
indexer: {
type: "boolean",
},
},
});

Expand Down Expand Up @@ -1638,15 +1646,16 @@ export function getConfig(): Config {
.default(null)
.parse(process.env.SENTRY_DSN);

const hostname = os.hostname();

const databaseUrl = z.string().url().parse(process.env.DATABASE_URL);

const sqlSafeHostname = hostname.replace(/[^a-zA-Z0-9]/g, "_").toLowerCase();
const databaseSchemaName = `chain_data_${sqlSafeHostname}_${CHAIN_DATA_VERSION}`;
const databaseSchemaName = `chain_data_${CHAIN_DATA_VERSION}`;

const dropDb = z.boolean().default(false).parse(args["drop-db"]);

const parseBoolean = z
.boolean()
.or(z.enum(["true", "false"]).transform((value) => value === "true"));

const estimatesLinearQfWorkerPoolSize = z.coerce
.number()
.nullable()
Expand All @@ -1659,6 +1668,14 @@ export function getConfig(): Config {
.transform((value) => value === "true")
.parse(args["http-wait-for-sync"] ?? process.env.HTTP_SERVER_WAIT_FOR_SYNC);

const httpServerEnabled = parseBoolean
.default(false)
.parse(args["http"] ?? process.env.HTTP_ENABLED);

const indexerEnabled = parseBoolean
.default(false)
.parse(args["indexer"] ?? process.env.INDEXER_ENABLED);

return {
buildTag: buildTag,
sentryDsn,
Expand All @@ -1680,6 +1697,8 @@ export function getConfig(): Config {
dropDb,
databaseSchemaName,
httpServerWaitForSync,
httpServerEnabled,
indexerEnabled,
hostname: os.hostname(),
estimatesLinearQfWorkerPoolSize,
};
Expand Down
11 changes: 9 additions & 2 deletions src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ export class Database {

readonly databaseSchemaName: string;

constructor(options: { connectionPool: Pool; schemaName: string }) {
constructor(options: {
statsUpdaterEnabled: boolean;
connectionPool: Pool;
schemaName: string;
}) {
const dialect = new PostgresDialect({
pool: options.connectionPool,
});
Expand All @@ -68,7 +72,10 @@ export class Database {
this.databaseSchemaName = options.schemaName;

this.scheduleDonationQueueFlush();
this.scheduleStatsUpdate();

if (options.statsUpdaterEnabled) {
this.scheduleStatsUpdate();
}
}

private scheduleStatsUpdate() {
Expand Down
1 change: 1 addition & 0 deletions src/http/api/v1/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const createHandler = (config: HttpApiConfig): express.Router => {
res.json({
hostname: config.hostname,
buildTag: config.buildTag,
databaseSchema: config.db.databaseSchemaName,
});
});

Expand Down
Loading

0 comments on commit c44a829

Please sign in to comment.