feat(web): options to disable machine
This commit is contained in:
+14
-10
@@ -1,12 +1,12 @@
|
||||
"use server";
|
||||
|
||||
import { withServerPromise } from "./withServerPromise";
|
||||
import { db } from "@/db/db";
|
||||
import { workflowRunsTable } from "@/db/schema";
|
||||
import { machinesTable, workflowRunsTable } from "@/db/schema";
|
||||
import { ComfyAPI_Run } from "@/types/ComfyAPI_Run";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import "server-only";
|
||||
import { withServerPromise } from "./withServerPromise";
|
||||
|
||||
export const createRun = withServerPromise(
|
||||
async (
|
||||
@@ -14,20 +14,24 @@ export const createRun = withServerPromise(
|
||||
workflow_version_id: string,
|
||||
machine_id: string,
|
||||
inputs?: Record<string, string>,
|
||||
isManualRun?: boolean,
|
||||
isManualRun?: boolean
|
||||
) => {
|
||||
const machine = await db.query.machinesTable.findFirst({
|
||||
where: eq(workflowRunsTable.id, machine_id),
|
||||
where: and(
|
||||
eq(machinesTable.id, machine_id),
|
||||
eq(machinesTable.disabled, false)
|
||||
),
|
||||
});
|
||||
|
||||
if (!machine) {
|
||||
throw new Error("Machine not found");
|
||||
}
|
||||
|
||||
const workflow_version_data =
|
||||
await db.query.workflowVersionTable.findFirst({
|
||||
const workflow_version_data = await db.query.workflowVersionTable.findFirst(
|
||||
{
|
||||
where: eq(workflowRunsTable.id, workflow_version_id),
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
if (!workflow_version_data) {
|
||||
throw new Error("Workflow version not found");
|
||||
@@ -79,7 +83,7 @@ export const createRun = withServerPromise(
|
||||
workflow_version_id: workflow_version_data.id,
|
||||
workflow_inputs: inputs,
|
||||
machine_id,
|
||||
origin: isManualRun ? "manual" : "api"
|
||||
origin: isManualRun ? "manual" : "api",
|
||||
})
|
||||
.returning();
|
||||
|
||||
@@ -89,5 +93,5 @@ export const createRun = withServerPromise(
|
||||
workflow_run_id: workflow_run[0].id,
|
||||
message: "Successful workflow run",
|
||||
};
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
@@ -1,30 +1,22 @@
|
||||
"use server";
|
||||
|
||||
import { withServerPromise } from "./withServerPromise";
|
||||
import { db } from "@/db/db";
|
||||
import { machinesTable } from "@/db/schema";
|
||||
import { auth } from "@clerk/nextjs";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import "server-only";
|
||||
|
||||
// export async function addMachine(form: FormData) {
|
||||
// const name = form.get("name") as string;
|
||||
// const endpoint = form.get("endpoint") as string;
|
||||
|
||||
// await db.insert(machinesTable).values({
|
||||
// name,
|
||||
// endpoint,
|
||||
// });
|
||||
// revalidatePath("/machines");
|
||||
// }
|
||||
|
||||
export async function getMachines() {
|
||||
const { userId } = auth();
|
||||
if (!userId) throw new Error("No user id");
|
||||
const machines = await db
|
||||
.select()
|
||||
.from(machinesTable)
|
||||
.where(eq(machinesTable.user_id, userId));
|
||||
.where(
|
||||
and(eq(machinesTable.user_id, userId), eq(machinesTable.disabled, false))
|
||||
);
|
||||
return machines;
|
||||
}
|
||||
|
||||
@@ -40,10 +32,36 @@ export async function addMachine(name: string, endpoint: string) {
|
||||
revalidatePath("/machines");
|
||||
}
|
||||
|
||||
export async function deleteMachine(
|
||||
machine_id: string
|
||||
): Promise<{ message: string; error?: boolean }> {
|
||||
await db.delete(machinesTable).where(eq(machinesTable.id, machine_id));
|
||||
revalidatePath("/machines");
|
||||
return { message: "Machine Deleted" };
|
||||
}
|
||||
export const deleteMachine = withServerPromise(
|
||||
async (machine_id: string): Promise<{ message: string }> => {
|
||||
await db.delete(machinesTable).where(eq(machinesTable.id, machine_id));
|
||||
revalidatePath("/machines");
|
||||
return { message: "Machine Deleted" };
|
||||
}
|
||||
);
|
||||
|
||||
export const disableMachine = withServerPromise(
|
||||
async (machine_id: string): Promise<{ message: string }> => {
|
||||
await db
|
||||
.update(machinesTable)
|
||||
.set({
|
||||
disabled: true,
|
||||
})
|
||||
.where(eq(machinesTable.id, machine_id));
|
||||
revalidatePath("/machines");
|
||||
return { message: "Machine Disabled" };
|
||||
}
|
||||
);
|
||||
|
||||
export const enableMachine = withServerPromise(
|
||||
async (machine_id: string): Promise<{ message: string }> => {
|
||||
await db
|
||||
.update(machinesTable)
|
||||
.set({
|
||||
disabled: false,
|
||||
})
|
||||
.where(eq(machinesTable.id, machine_id));
|
||||
revalidatePath("/machines");
|
||||
return { message: "Machine Enabled" };
|
||||
}
|
||||
);
|
||||
|
||||
@@ -5,9 +5,8 @@ import { workflowTable } from "@/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import "server-only";
|
||||
|
||||
|
||||
export async function deleteWorkflow(workflow_id: string) {
|
||||
await db.delete(workflowTable).where(eq(workflowTable.id, workflow_id));
|
||||
revalidatePath("/");
|
||||
}
|
||||
|
||||
@@ -7,6 +7,6 @@ export async function wrapServerPromise<T>(result: Promise<T>) {
|
||||
}
|
||||
export function withServerPromise<T extends (...args: any[]) => Promise<any>>(
|
||||
fn: T
|
||||
): (...args: Parameters<T>) => Promise<ReturnType<T> | { error: string; }> {
|
||||
): (...args: Parameters<T>) => Promise<ReturnType<T> | { error: string }> {
|
||||
return (...args: Parameters<T>) => wrapServerPromise(fn(...args));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user