Skip to content

Commit

Permalink
support optional migrations in bsync service, docker build fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Jan 16, 2024
1 parent 6913ec2 commit cf622c3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/bsync/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'node:assert'
import { envInt, envStr, envList } from '@atproto/common'
import { envInt, envStr, envList, envBool } from '@atproto/common'

export const envToCfg = (env: ServerEnvironment): ServerConfig => {
const serviceCfg: ServerConfig['service'] = {
Expand All @@ -15,6 +15,7 @@ export const envToCfg = (env: ServerEnvironment): ServerConfig => {
poolSize: env.dbPoolSize,
poolMaxUses: env.dbPoolMaxUses,
poolIdleTimeoutMs: env.dbPoolIdleTimeoutMs,
migrate: env.dbMigrate,
}

assert(env.apiKeys.length > 0, 'missing api keys')
Expand Down Expand Up @@ -47,6 +48,7 @@ type DatabaseConfig = {
poolSize?: number
poolMaxUses?: number
poolIdleTimeoutMs?: number
migrate?: boolean
}

type AuthConfig = {
Expand All @@ -65,6 +67,7 @@ export const readEnv = (): ServerEnvironment => {
dbPoolSize: envInt('BSYNC_DB_POOL_SIZE'),
dbPoolMaxUses: envInt('BSYNC_DB_POOL_MAX_USES'),
dbPoolIdleTimeoutMs: envInt('BSYNC_DB_POOL_IDLE_TIMEOUT_MS'),
dbMigrate: envBool('BSYNC_DB_MIGRATE'),
// secrets
apiKeys: envList('BSYNC_API_KEYS'),
}
Expand All @@ -81,6 +84,7 @@ export type ServerEnvironment = {
dbPoolSize?: number
dbPoolMaxUses?: number
dbPoolIdleTimeoutMs?: number
dbMigrate?: boolean
// secrets
apiKeys: string[]
}
3 changes: 2 additions & 1 deletion services/bsync/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ COPY ./packages/bsync ./packages/bsync
COPY ./packages/common ./packages/common
COPY ./packages/common-web ./packages/common-web
COPY ./packages/syntax ./packages/syntax
COPY ./services/bsync ./services/bsync


# install all deps
Expand Down Expand Up @@ -37,7 +38,7 @@ WORKDIR /app/services/bsync
COPY --from=build /app /app

EXPOSE 3000
ENV PORT=3000
ENV BSYNC_PORT=3000
ENV NODE_ENV=production

# https://github.com/nodejs/docker-node/blob/master/docs/BestPractices.md#non-root-user
Expand Down
5 changes: 5 additions & 0 deletions services/bsync/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const main = async () => {
const env = readEnv()
const cfg = envToCfg(env)
const bsync = await BsyncService.create(cfg)
if (bsync.ctx.cfg.db.migrate) {
httpLogger.info('bsync db is migrating')
await bsync.ctx.db.migrateToLatestOrThrow()
httpLogger.info('bsync db migration complete')
}
await bsync.start()
httpLogger.info('bsync is running')
process.on('SIGTERM', async () => {
Expand Down

0 comments on commit cf622c3

Please sign in to comment.