fix production issue

This commit is contained in:
root
2026-05-10 19:55:57 -04:00
parent a224d7704a
commit 2d28947b92
17 changed files with 107 additions and 81 deletions
+24 -22
View File
@@ -2,7 +2,9 @@ import { execFile } from 'child_process'
import { promisify } from 'util'
import * as fs from 'fs/promises'
import * as path from 'path'
import { prisma } from '../lib/prisma'
import { prisma as _prisma } from '../lib/prisma'
const db = _prisma as any
const execFileAsync = promisify(execFile)
let composeCommand: 'docker-compose' | 'docker' | null = null
@@ -72,12 +74,12 @@ async function composeFilesExist(): Promise<boolean> {
}
async function rebuildServicesFromDatabase(): Promise<ServicesMap> {
const records = await prisma.companyContainer.findMany({
const records = await db.companyContainer.findMany({
include: { company: { select: { slug: true } } },
})
return Object.fromEntries(
records.map((record) => [
records.map((record: any) => [
serviceName(record.company.slug),
{
companyId: record.companyId,
@@ -225,7 +227,7 @@ function mapDockerError(err: unknown) {
}
async function setContainerError(companyId: string, message: string): Promise<void> {
await prisma.companyContainer.update({
await db.companyContainer.update({
where: { companyId },
data: { status: 'ERROR', errorMessage: message },
}).catch(() => null)
@@ -239,7 +241,7 @@ async function runContainerAction(
preStatus?: Exclude<ServiceStatus, 'ERROR'>,
): Promise<void> {
if (preStatus) {
await prisma.companyContainer.update({ where: { companyId }, data: { status: preStatus, errorMessage: null } })
await db.companyContainer.update({ where: { companyId }, data: { status: preStatus, errorMessage: null } })
}
try {
@@ -248,7 +250,7 @@ async function runContainerAction(
? ['up', '-d', '--no-recreate', serviceName(slug)]
: [command, serviceName(slug)]
await compose(...args)
await prisma.companyContainer.update({
await db.companyContainer.update({
where: { companyId },
data: { status: successStatus, errorMessage: null },
})
@@ -260,7 +262,7 @@ async function runContainerAction(
}
async function allocatePort(): Promise<number> {
const last = await prisma.companyContainer.findFirst({
const last = await db.companyContainer.findFirst({
orderBy: { port: 'desc' },
select: { port: true },
})
@@ -304,7 +306,7 @@ export async function createCompanyContainer(company: {
const name = serviceName(company.slug)
const port = await allocatePort()
const record = await prisma.companyContainer.create({
const record = await db.companyContainer.create({
data: {
companyId: company.id,
containerName: containerName(company.slug),
@@ -322,12 +324,12 @@ export async function createCompanyContainer(company: {
await compose('up', '-d', '--no-recreate', name)
const dockerId = await getDockerServiceId(company.slug)
await prisma.companyContainer.update({
await db.companyContainer.update({
where: { id: record.id },
data: { dockerId, status: 'RUNNING' },
})
} catch (err) {
await prisma.companyContainer.update({
await db.companyContainer.update({
where: { id: record.id },
data: {
status: 'ERROR',
@@ -339,7 +341,7 @@ export async function createCompanyContainer(company: {
}
export async function startCompanyContainer(companyId: string): Promise<void> {
const record = await prisma.companyContainer.findUniqueOrThrow({
const record = await db.companyContainer.findUniqueOrThrow({
where: { companyId },
include: { company: { select: { slug: true } } },
})
@@ -348,7 +350,7 @@ export async function startCompanyContainer(companyId: string): Promise<void> {
}
export async function stopCompanyContainer(companyId: string): Promise<void> {
const record = await prisma.companyContainer.findUniqueOrThrow({
const record = await db.companyContainer.findUniqueOrThrow({
where: { companyId },
include: { company: { select: { slug: true } } },
})
@@ -357,7 +359,7 @@ export async function stopCompanyContainer(companyId: string): Promise<void> {
}
export async function restartCompanyContainer(companyId: string): Promise<void> {
const record = await prisma.companyContainer.findUniqueOrThrow({
const record = await db.companyContainer.findUniqueOrThrow({
where: { companyId },
include: { company: { select: { slug: true } } },
})
@@ -366,13 +368,13 @@ export async function restartCompanyContainer(companyId: string): Promise<void>
}
export async function removeCompanyContainer(companyId: string): Promise<void> {
const record = await prisma.companyContainer.findUniqueOrThrow({
const record = await db.companyContainer.findUniqueOrThrow({
where: { companyId },
include: { company: { select: { slug: true } } },
})
const name = serviceName(record.company.slug)
await prisma.companyContainer.update({ where: { companyId }, data: { status: 'REMOVING' } })
await db.companyContainer.update({ where: { companyId }, data: { status: 'REMOVING' } })
try {
await compose('stop', name)
@@ -386,7 +388,7 @@ export async function removeCompanyContainer(companyId: string): Promise<void> {
delete services[name]
await writeServices(services)
await prisma.companyContainer.delete({ where: { companyId } })
await db.companyContainer.delete({ where: { companyId } })
}
export async function redeployCompanyContainer(company: {
@@ -396,11 +398,11 @@ export async function redeployCompanyContainer(company: {
}): Promise<void> {
const name = serviceName(company.slug)
const existing = await prisma.companyContainer.findUnique({ where: { companyId: company.id } })
const existing = await db.companyContainer.findUnique({ where: { companyId: company.id } })
if (existing) {
try { await compose('stop', name) } catch { /* ok */ }
try { await compose('rm', '-f', name) } catch { /* ok */ }
await prisma.companyContainer.delete({ where: { companyId: company.id } })
await db.companyContainer.delete({ where: { companyId: company.id } })
}
// Remove from compose file too, then recreate
@@ -412,7 +414,7 @@ export async function redeployCompanyContainer(company: {
}
export async function getContainerLogs(companyId: string, tail = 150): Promise<string> {
const record = await prisma.companyContainer.findUniqueOrThrow({
const record = await db.companyContainer.findUniqueOrThrow({
where: { companyId },
include: { company: { select: { slug: true } } },
})
@@ -431,7 +433,7 @@ export async function getContainerLogs(companyId: string, tail = 150): Promise<s
}
export async function syncContainerStatuses(): Promise<void> {
const records = await prisma.companyContainer.findMany({
const records = await db.companyContainer.findMany({
where: { status: { notIn: ['PENDING', 'CREATING', 'REMOVING'] } },
include: { company: { select: { slug: true } } },
})
@@ -451,13 +453,13 @@ export async function syncContainerStatuses(): Promise<void> {
const stateByService = Object.fromEntries(rows.map((r) => [r.Service, r.State]))
await Promise.allSettled(
records.map(async (rec) => {
records.map(async (rec: any) => {
const name = serviceName(rec.company.slug)
const dockerState = stateByService[name]
const status = dockerState ? mapDockerState(dockerState) : 'STOPPED'
if (rec.status !== status) {
await prisma.companyContainer.update({ where: { id: rec.id }, data: { status } })
await db.companyContainer.update({ where: { id: rec.id }, data: { status } })
}
}),
)