Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Feb 21, 2024
1 parent e2096f4 commit 40b67bd
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ export class Worker {
const databaseName = getRandomDatabaseName()

// Create database
const { postgresClient, container } = await this.startContainerPromise
const { postgresClient, container, pgbouncerContainer } = await this
.startContainerPromise

await postgresClient.query(`CREATE DATABASE ${databaseName};`)

const msg = message.reply({
Expand Down Expand Up @@ -246,16 +248,25 @@ export class Worker {
}

private async getConnectionDetails(databaseName: string) {
const { container, network } = await this.startContainerPromise
const { container, network, pgbouncerContainer } = await this
.startContainerPromise
const externalDatabaseUrl = `postgresql://postgres:@${container.getHost()}:${container.getMappedPort(
5432
)}/${databaseName}`

let pgbouncerConnectionString
if (pgbouncerContainer) {
pgbouncerConnectionString = `postgresql://postgres:@${pgbouncerContainer
.getName()
.replace("/", "")}:5432/${databaseName}`
}

return {
connectionString: externalDatabaseUrl,
connectionStringDocker: `postgresql://postgres:@${container
.getName()
.replace("/", "")}:5432/${databaseName}`,
pgbouncerConnectionString,
dockerNetworkId: network.getId(),
host: container.getHost(),
port: container.getMappedPort(5432),
Expand Down Expand Up @@ -300,9 +311,11 @@ export class Worker {
)
}

let startedPgbouncerContainer
if (this.initialData.pgbouncerOptions?.enabled) {
const pgbouncerContainer = new GenericContainer("edoburu/pgbouncer")
.withExposedPorts(6432)
.withName(getRandomDatabaseName())
.withEnvironment({
PGBOUNCER_LISTEN_PORT: "6432",
PGBOUNCER_POOL_MODE: "transaction",
Expand All @@ -315,10 +328,12 @@ export class Worker {
})
.withStartupTimeout(120_000)
.withNetwork(network)
startedPgbouncerContainer = await pgbouncerContainer.start()
}

return {
container: startedContainer,
pgbouncerContainer: startedPgbouncerContainer,
network,
postgresClient: new pg.Pool({
connectionString: `postgresql://postgres:@${startedContainer.getHost()}:${startedContainer.getMappedPort(
Expand Down

0 comments on commit 40b67bd

Please sign in to comment.