-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrigger.mjs
More file actions
57 lines (45 loc) · 1.38 KB
/
trigger.mjs
File metadata and controls
57 lines (45 loc) · 1.38 KB
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
import { readFileSync, existsSync } from "node:fs";
if (existsSync(".env")) {
for (const line of readFileSync(".env", "utf-8").split("\n")) {
const match = line.match(/^([^#=]+)=(.*)$/);
if (match && !process.env[match[1].trim()]) {
process.env[match[1].trim()] = match[2].trim();
}
}
}
const prUrl = process.argv[2];
if (!prUrl) {
console.error("Usage: node trigger.mjs <github-pr-url>");
process.exit(1);
}
const WORKER_URL =
process.env.WORKER_URL || "http://localhost:8787";
console.log(`Starting video generation for: ${prUrl}`);
const res = await fetch(`${WORKER_URL}/generate`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ prUrl }),
});
if (!res.ok) {
const err = await res.text();
console.error(`Failed to start job: ${err}`);
process.exit(1);
}
const { jobId } = await res.json();
console.log(`Job started: ${jobId}`);
let status = "running";
let videoUrl = null;
while (status !== "complete" && status !== "errored") {
await new Promise((r) => setTimeout(r, 5000));
const poll = await fetch(`${WORKER_URL}/status/${jobId}`);
const data = await poll.json();
status = data.status;
videoUrl = data.videoUrl;
console.log(`Status: ${status}`);
}
if (status === "complete" && videoUrl) {
console.log(`\nVideo ready: ${videoUrl}`);
} else {
console.error("\nJob failed.");
process.exit(1);
}