Skip to content

Commit

Permalink
prepare for multiple-region deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7d8 committed Dec 5, 2024
1 parent c45004f commit b12b9f3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/crontabs/update-prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let isRunning = false, blockRunning = false
export default new Crontab()
.cron(env.LOG_LEVEL === 'debug' ? '*/3 * * * * *' : '0 */6 * * *')
.listen(async(ctx) => {
if (isRunning || blockRunning) return
if (isRunning || blockRunning || !ctx.env.UPDATE_PRICES) return
isRunning = true

if (env.LOG_LEVEL === 'debug') blockRunning = true
Expand Down Expand Up @@ -62,7 +62,7 @@ export default new Crontab()
}

if (JSON.stringify(platforms) !== JSON.stringify(extension.platforms)) {
await ctx.database.update(ctx.database.schema.extensions)
await ctx.database.write.update(ctx.database.schema.extensions)
.set({
platforms
})
Expand Down
15 changes: 13 additions & 2 deletions src/globals/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,29 @@ const pool = new Pool({
connectionString: env.DATABASE_URL
})

const writePool = env.DATABASE_URL_PRIMARY ? new Pool({
connectionString: env.DATABASE_URL_PRIMARY
}) : pool

const db = drizzle(pool, { schema }),
writeDb = drizzle(writePool, { schema }),
startTime = performance.now()

db.$client.connect().then(() => {
Promise.all([
db.$client.connect(),
env.DATABASE_URL_PRIMARY ? writeDb.$client.connect() : Promise.resolve()
]).then(() => {
logger()
.text('Database', (c) => c.cyan)
.text('Connection established!')
.text(`(${(performance.now() - startTime).toFixed(1)}ms)`, (c) => c.gray)
.info()
})

export default Object.assign(db, {
type DbWithoutWrite = Omit<typeof db, 'insert' | 'update' | 'delete'>

export default Object.assign(db as DbWithoutWrite, {
write: writeDb,
schema,

fields: Object.freeze({
Expand Down
3 changes: 3 additions & 0 deletions src/globals/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ try {

const infos = z.object({
DATABASE_URL: z.string(),
DATABASE_URL_PRIMARY: z.string().optional(),

UPDATE_PRICES: z.enum(['true', 'false']).transform((str) => str === 'true').default('false'),

PORT: z.string().transform((str) => parseInt(str)).optional(),
INTERNAL_KEY: z.string(),
Expand Down
4 changes: 2 additions & 2 deletions src/globals/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const process = async(): Promise<void> => {

const panels = new Set(telemetry.map((t) => t.panelId))

await Promise.all(Array.from(panels).map((id) => cache.use(`panel:${id}`, () => database.insert(schema.telemetryPanels)
await Promise.all(Array.from(panels).map((id) => cache.use(`panel:${id}`, () => database.write.insert(schema.telemetryPanels)
.values({ id, version: telemetry.find((t) => t.panelId === id)!.version })
.onConflictDoUpdate({
target: schema.telemetryPanels.id,
Expand All @@ -60,7 +60,7 @@ const process = async(): Promise<void> => {
})
)))

await database.insert(schema.telemetryData)
await database.write.insert(schema.telemetryData)
.values(telemetry.map((t) => Object.assign(t, { ip: string.hash(t.ip, { algorithm: 'sha256' }) })))
.onConflictDoNothing()
} catch (err) {
Expand Down

0 comments on commit b12b9f3

Please sign in to comment.