chore: add machine delete logic check for only new version

This commit is contained in:
bennykok 2024-01-27 22:33:25 +08:00
parent 9a1ab21e91
commit e5097a5ed5

View File

@ -30,10 +30,10 @@ export async function getMachines() {
: // make sure org_id is null : // make sure org_id is null
and( and(
eq(machinesTable.user_id, userId), eq(machinesTable.user_id, userId),
isNull(machinesTable.org_id) isNull(machinesTable.org_id),
),
eq(machinesTable.disabled, false),
), ),
eq(machinesTable.disabled, false)
)
); );
return machines; return machines;
} }
@ -50,10 +50,10 @@ export async function getMachineById(id: string) {
? eq(machinesTable.org_id, orgId) ? eq(machinesTable.org_id, orgId)
: and( : and(
eq(machinesTable.user_id, userId), eq(machinesTable.user_id, userId),
isNull(machinesTable.org_id) isNull(machinesTable.org_id),
),
eq(machinesTable.id, id),
), ),
eq(machinesTable.id, id)
)
); );
return machines[0]; return machines[0];
} }
@ -70,7 +70,7 @@ export const addMachine = withServerPromise(
}); });
revalidatePath("/machines"); revalidatePath("/machines");
return { message: "Machine Added" }; return { message: "Machine Added" };
} },
); );
export const updateCustomMachine = withServerPromise( export const updateCustomMachine = withServerPromise(
@ -121,7 +121,7 @@ export const updateCustomMachine = withServerPromise(
} }
return { message: "Machine Updated" }; return { message: "Machine Updated" };
} },
); );
export const buildMachine = withServerPromise( export const buildMachine = withServerPromise(
@ -147,7 +147,7 @@ export const buildMachine = withServerPromise(
// Perform custom build if there are changes // Perform custom build if there are changes
await _buildMachine(datas[0], currentMachine); await _buildMachine(datas[0], currentMachine);
redirect(`/machines/${id}`); redirect(`/machines/${id}`);
} },
); );
export const addCustomMachine = withServerPromise( export const addCustomMachine = withServerPromise(
@ -174,12 +174,12 @@ export const addCustomMachine = withServerPromise(
redirect(`/machines/${b.id}`); redirect(`/machines/${b.id}`);
// revalidatePath("/machines"); // revalidatePath("/machines");
return { message: "Machine Building" }; return { message: "Machine Building" };
} },
); );
async function _buildMachine( async function _buildMachine(
data: z.infer<typeof addCustomMachineSchema>, data: z.infer<typeof addCustomMachineSchema>,
b: MachineType b: MachineType,
) { ) {
const headersList = headers(); const headersList = headers();
@ -244,7 +244,7 @@ export const updateMachine = withServerPromise(
await db.update(machinesTable).set(data).where(eq(machinesTable.id, id)); await db.update(machinesTable).set(data).where(eq(machinesTable.id, id));
revalidatePath("/machines"); revalidatePath("/machines");
return { message: "Machine Updated" }; return { message: "Machine Updated" };
} },
); );
export const deleteMachine = withServerPromise( export const deleteMachine = withServerPromise(
@ -255,7 +255,12 @@ export const deleteMachine = withServerPromise(
if (machine?.type === "comfy-deploy-serverless") { if (machine?.type === "comfy-deploy-serverless") {
// Call remote builder to stop the app on modal // Call remote builder to stop the app on modal
const result = await fetch(`${process.env.MODAL_BUILDER_URL!}/stop-app`, { // Only check for deletion error upon new migration
const newUpgradeDate = new Date("2024-01-27T14:24:23.737Z");
if (machine.created_at > newUpgradeDate) {
const result = await fetch(
`${process.env.MODAL_BUILDER_URL!}/stop-app`,
{
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@ -263,18 +268,20 @@ export const deleteMachine = withServerPromise(
body: JSON.stringify({ body: JSON.stringify({
machine_id: machine_id, machine_id: machine_id,
}), }),
}); },
);
if (!result.ok) { if (!result.ok) {
const error_log = await result.text(); const error_log = await result.text();
throw new Error(`Error: ${result.statusText} ${error_log}`); throw new Error(`Error: ${result.statusText} ${error_log}`);
} }
} }
}
await db.delete(machinesTable).where(eq(machinesTable.id, machine_id)); await db.delete(machinesTable).where(eq(machinesTable.id, machine_id));
revalidatePath("/machines"); revalidatePath("/machines");
return { message: "Machine Deleted" }; return { message: "Machine Deleted" };
} },
); );
export const disableMachine = withServerPromise( export const disableMachine = withServerPromise(
@ -287,7 +294,7 @@ export const disableMachine = withServerPromise(
.where(eq(machinesTable.id, machine_id)); .where(eq(machinesTable.id, machine_id));
revalidatePath("/machines"); revalidatePath("/machines");
return { message: "Machine Disabled" }; return { message: "Machine Disabled" };
} },
); );
export const enableMachine = withServerPromise( export const enableMachine = withServerPromise(
@ -300,5 +307,5 @@ export const enableMachine = withServerPromise(
.where(eq(machinesTable.id, machine_id)); .where(eq(machinesTable.id, machine_id));
revalidatePath("/machines"); revalidatePath("/machines");
return { message: "Machine Enabled" }; return { message: "Machine Enabled" };
} },
); );