Skip to content
This repository was archived by the owner on Jun 27, 2025. It is now read-only.

Commit 2867643

Browse files
authored
Postgres (#28)
* basic port to postgres * cap table size * update ci * fix env * fix log * stream logs * add data * fix lint * update migration
1 parent 820642b commit 2867643

File tree

10 files changed

+15743
-177
lines changed

10 files changed

+15743
-177
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,28 @@ on:
66
jobs:
77
build:
88
runs-on: ubuntu-latest
9+
services:
10+
postgres:
11+
image: postgres:latest
12+
env:
13+
POSTGRES_DB: spark_rewards
14+
POSTGRES_PASSWORD: postgres
15+
POSTGRES_USER: postgres
16+
ports:
17+
- 5432:5432
18+
options: >-
19+
--health-cmd pg_isready
20+
--health-interval 10s
21+
--health-timeout 5s
22+
--health-retries 5
923
env:
1024
SENTRY_ENVIRONMENT: CI
25+
DATABASE_URL: postgres://postgres:postgres@localhost:5432/spark_rewards
1126
steps:
1227
- uses: actions/checkout@v4
1328
- uses: actions/setup-node@v4
1429
with:
1530
node-version: 20
16-
- uses: supercharge/[email protected]
1731
- run: npm ci
1832
- run: npm test
1933

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Redis from 'ioredis'
2+
3+
const {
4+
REDIS_URL: redisUrl = 'redis://localhost:6379'
5+
} = process.env
6+
7+
const redisUrlParsed = new URL(redisUrl)
8+
const redis = new Redis({
9+
host: redisUrlParsed.hostname,
10+
port: redisUrlParsed.port,
11+
username: redisUrlParsed.username,
12+
password: redisUrlParsed.password,
13+
family: 6 // required for upstash
14+
})
15+
16+
const rewards = await redis.hgetall('rewards')
17+
18+
console.log('INSERT INTO scheduled_rewards (address, amount)')
19+
console.log('VALUES')
20+
console.log(
21+
Object.entries(rewards)
22+
.map(([address, amount]) => `('${address}', ${amount})`)
23+
.join(',\n')
24+
)
25+
console.log(';')
26+
27+
process.exit()

bin/spark-rewards.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import '../lib/instrument.js'
22
import http from 'node:http'
33
import { once } from 'node:events'
44
import { createHandler } from '../index.js'
5-
import Redis from 'ioredis'
6-
import Redlock from 'redlock'
5+
import pg from 'pg'
6+
import { migrate } from '../migrations/index.js'
77

88
const {
99
PORT: port = 3000,
@@ -15,7 +15,7 @@ const {
1515
'0x646ac6F1941CAb0ce3fE1368e9AD30364a9F51dA', // Miroslav
1616
'0x3ee4A552b1a6519A266AEFb0514633F289FF2A9F' // Julian
1717
].join(','),
18-
REDIS_URL: redisUrl = 'redis://localhost:6379'
18+
DATABASE_URL
1919
} = process.env
2020

2121
const logger = {
@@ -24,17 +24,28 @@ const logger = {
2424
request: ['1', 'true'].includes(requestLogging) ? console.info : () => {}
2525
}
2626

27-
const redisUrlParsed = new URL(redisUrl)
28-
const redis = new Redis({
29-
host: redisUrlParsed.hostname,
30-
port: redisUrlParsed.port,
31-
username: redisUrlParsed.username,
32-
password: redisUrlParsed.password,
33-
family: 6 // required for upstash
27+
const pgPool = new pg.Pool({
28+
connectionString: DATABASE_URL,
29+
// allow the pool to close all connections and become empty
30+
min: 0,
31+
// this values should correlate with service concurrency hard_limit configured in fly.toml
32+
// and must take into account the connection limit of our PG server, see
33+
// https://fly.io/docs/postgres/managing/configuration-tuning/
34+
max: 100,
35+
// close connections that haven't been used for one second
36+
idleTimeoutMillis: 1000,
37+
// automatically close connections older than 60 seconds
38+
maxLifetimeSeconds: 60
3439
})
35-
const redlock = new Redlock([redis])
40+
pgPool.on('error', err => {
41+
// Prevent crashing the process on idle client errors, the pool will recover
42+
// itself. If all connections are lost, the process will still crash.
43+
// https://github.com/brianc/node-postgres/issues/1324#issuecomment-308778405
44+
console.error('An idle client has experienced an error', err.stack)
45+
})
46+
await migrate(pgPool)
3647

37-
const handler = await createHandler({ logger, redis, redlock, signerAddresses })
48+
const handler = await createHandler({ logger, pgPool, signerAddresses })
3849
const server = http.createServer(handler)
3950
server.listen(port, host)
4051
await once(server, 'listening')

0 commit comments

Comments
 (0)