forked from golemfactory/music-on-golem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.ts
75 lines (70 loc) · 1.77 KB
/
service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { eq } from "drizzle-orm";
import { db } from "../db";
import { snippetTable } from "../db/schema";
import { golemClient } from "./client";
export function startWork({
prompt,
owner,
}: {
prompt: string;
owner: string;
}) {
const job = golemClient.createJob<string>({
package: {
imageTag: "severyn/musicgen:lite",
minStorageGib: 8,
minCpuCores: 4,
minMemGib: 8,
},
});
job.events.on("created", () => {
console.log("Job", job.id, "created");
db.insert(snippetTable)
.values({
id: job.id,
status: "waiting",
owner,
prompt,
})
.catch(console.error);
});
job.events.on("started", () => {
console.log("Job", job.id, "started");
db.update(snippetTable)
.set({ status: "in_progress" })
.where(eq(snippetTable.id, job.id))
.catch(console.error);
});
job.events.on("success", () => {
console.log("Job", job.id, "success", job.results);
db.update(snippetTable)
.set({ status: "done" })
.where(eq(snippetTable.id, job.id))
.catch(console.error);
});
job.events.on("error", () => {
console.log("Job", job.id, "error", job.error);
db.update(snippetTable)
.set({ status: "error" })
.where(eq(snippetTable.id, job.id))
.catch(console.error);
});
job.startWork(async (ctx) => {
await ctx.run(`python run.py --prompt "${prompt}" --duration 15`);
await ctx.downloadFile("/golem/output/out.wav", `public/${job.id}.wav`);
return `public/${job.id}.wav`;
});
return {
id: job.id,
status: "waiting",
owner,
prompt,
};
}
export async function cancelWork(id: string) {
const job = golemClient.getJobById(id);
if (!job) {
throw new Error("Job not found");
}
await job.cancel();
}