safeCatch updates

This commit is contained in:
Ben Allfree 2022-11-15 04:08:20 +00:00
parent 2c6b66414f
commit e19f26dc3a
5 changed files with 59 additions and 50 deletions

View File

@ -9,40 +9,42 @@ import {
} from '../constants'
import { createPbClient, PocketbaseClientApi } from '../db/PbClient'
import { mkInternalUrl } from '../util/internal'
import { safeCatch } from '../util/safeAsync'
import { spawnInstance } from '../util/spawnInstance'
import { tryFetch } from '../util/tryFetch'
import { schema } from './schema'
export const applyDbMigrations = async (
cb: (client: PocketbaseClientApi) => Promise<void>
) => {
// Add `platform` and `bin` required columns (migrate db json)
try {
const mainProcess = await spawnInstance({
subdomain: PUBLIC_PB_SUBDOMAIN,
slug: PUBLIC_PB_SUBDOMAIN,
port: DAEMON_PB_PORT_BASE,
bin: binFor('lollipop'),
})
export const applyDbMigrations = safeCatch(
`applyDbMigrations`,
async (cb: (client: PocketbaseClientApi) => Promise<void>) => {
// Add `platform` and `bin` required columns (migrate db json)
try {
const coreInternalUrl = mkInternalUrl(DAEMON_PB_PORT_BASE)
const client = createPbClient(coreInternalUrl)
await tryFetch(coreInternalUrl)
await client.adminAuthViaEmail(DAEMON_PB_USERNAME, DAEMON_PB_PASSWORD)
await client.applySchema(schema)
await cb(client)
const mainProcess = await spawnInstance({
subdomain: PUBLIC_PB_SUBDOMAIN,
slug: PUBLIC_PB_SUBDOMAIN,
port: DAEMON_PB_PORT_BASE,
bin: binFor('lollipop'),
})
try {
const coreInternalUrl = mkInternalUrl(DAEMON_PB_PORT_BASE)
const client = createPbClient(coreInternalUrl)
await tryFetch(coreInternalUrl)
await client.adminAuthViaEmail(DAEMON_PB_USERNAME, DAEMON_PB_PASSWORD)
await client.applySchema(schema)
await cb(client)
} catch (e) {
console.error(
`***WARNING*** CANNOT AUTHENTICATE TO ${PUBLIC_PB_PROTOCOL}://${PUBLIC_PB_SUBDOMAIN}.${PUBLIC_PB_DOMAIN}/_/`
)
console.error(
`***WARNING*** LOG IN MANUALLY, ADJUST .env, AND RESTART DOCKER`
)
} finally {
console.log(`Exiting process`)
mainProcess.kill()
}
} catch (e) {
console.error(
`***WARNING*** CANNOT AUTHENTICATE TO ${PUBLIC_PB_PROTOCOL}://${PUBLIC_PB_SUBDOMAIN}.${PUBLIC_PB_DOMAIN}/_/`
)
console.error(
`***WARNING*** LOG IN MANUALLY, ADJUST .env, AND RESTART DOCKER`
)
} finally {
console.log(`Exiting process`)
mainProcess.kill()
console.error(`${e}`)
}
} catch (e) {
console.error(`${e}`)
}
}
)

View File

@ -8,12 +8,13 @@ import {
} from '../constants'
import { backupInstance } from '../util/backupInstance'
import { error } from '../util/dbg'
import { safeCatch } from '../util/safeAsync'
import { applyDbMigrations } from './applyDbMigrations'
import { pexec } from './pexec'
const PB_BIN = resolve(DAEMON_PB_BIN_DIR, binFor('lollipop'))
;(async () => {
safeCatch(`root`, async () => {
await backupInstance(
PUBLIC_PB_SUBDOMAIN,
`${+new Date()}`,

View File

@ -1,6 +1,7 @@
import { exec } from 'child_process'
import { safeCatch } from '../util/safeAsync'
export const pexec = (cmd: string) => {
export const pexec = safeCatch(`pexec`, (cmd: string) => {
return new Promise<void>((resolve, reject) => {
console.log(cmd)
exec(cmd, (err, stdout, stderr) => {
@ -13,4 +14,4 @@ export const pexec = (cmd: string) => {
resolve()
})
})
}
})

View File

@ -4,6 +4,7 @@ import { AsyncReturnType } from 'type-fest'
import { DAEMON_PB_BIN_DIR, DAEMON_PB_DATA_DIR } from '../constants'
import { dbg } from './dbg'
import { mkInternalAddress, mkInternalUrl } from './internal'
import { safeCatch } from './safeAsync'
import { tryFetch } from './tryFetch'
export type PocketbaseProcess = AsyncReturnType<typeof spawnInstance>
@ -15,7 +16,7 @@ export type Config = {
onUnexpectedStop?: (code: number | null) => void
}
export const spawnInstance = async (cfg: Config) => {
export const spawnInstance = safeCatch(`spawnInstance`, async (cfg: Config) => {
const { subdomain, port, bin, onUnexpectedStop, slug } = cfg
const cmd = `${DAEMON_PB_BIN_DIR}/${bin}`
if (!existsSync(cmd)) {
@ -64,4 +65,4 @@ export const spawnInstance = async (cfg: Config) => {
pid: ls.pid,
kill: () => ls.kill(),
}
}
})

View File

@ -1,18 +1,22 @@
import { dbg, error } from './dbg'
import { safeCatch } from './safeAsync'
export const tryFetch = (url: string) =>
new Promise<void>((resolve, reject) => {
const tryFetch = () => {
dbg(`Trying to connect to instance ${url} `)
fetch(url)
.then(() => {
dbg(`Connection to ${url} successful`)
resolve()
})
.catch((e) => {
error(`Could not connect to ${url}`)
setTimeout(tryFetch, 1000)
})
}
tryFetch()
})
export const tryFetch = safeCatch(
`tryFetch`,
(url: string) =>
new Promise<void>((resolve, reject) => {
const tryFetch = () => {
dbg(`Trying to connect to instance ${url} `)
fetch(url)
.then(() => {
dbg(`Connection to ${url} successful`)
resolve()
})
.catch((e) => {
error(`Could not connect to ${url}`)
setTimeout(tryFetch, 1000)
})
}
tryFetch()
})
)