Repository navigation
Expand file tree
/
Copy pathqueue.py
More file actions
314 lines (256 loc) · 10.7 KB
/
Copy pathqueue.py
File metadata and controls
314 lines (256 loc) · 10.7 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python3
"""
queue.py — Sequential pipeline runner with Discord notifications
Processes all jobs in the jobs/ folder in order.
Each job gets its own heartbeat update and a Discord DM to the user when done.
Usage:
python queue.py # run all jobs in jobs/ folder
python queue.py --dry-run # show what would run, don't execute
python queue.py jobs/001_foo.json # run specific job(s)
python queue.py --add # interactive: create a new job from prompts
"""
import os
import sys
import json
import subprocess
import argparse
import glob
import re
from datetime import datetime
PIPELINE_DIR = os.path.dirname(os.path.abspath(__file__))
LOG_DIR = "/tmp"
HEARTBEAT_PATH = os.path.expanduser("~/HEARTBEAT.md")
DISCORD_CHANNEL_ID = "1487830336636195009" # #base-video-editor channel
# ── Discord notification ─────────────────────────────────────────────────────
def notify(message: str):
"""Send a Discord message via openclaw CLI."""
cmd = ["openclaw", "message", "send",
"--channel", "discord",
"--target", DISCORD_CHANNEL_ID,
"--message", message]
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=15)
if result.returncode != 0:
print(f"[notify] failed: {result.stderr[:200]}")
except Exception as e:
print(f"[notify] error: {e}")
# ── Heartbeat ────────────────────────────────────────────────────────────────
def write_heartbeat(status: str, progress: str, eta: str = ""):
content = f"""# HEARTBEAT
task: base_video_editor queue
agent: engineer
started: {datetime.now().isoformat()}
status: {status}
progress: {progress}
eta: {eta}
"""
try:
with open(HEARTBEAT_PATH, "w") as f:
f.write(content)
except Exception as e:
print(f"[heartbeat] write failed: {e}")
def clear_heartbeat():
try:
if os.path.exists(HEARTBEAT_PATH):
os.remove(HEARTBEAT_PATH)
except Exception:
pass
# ── Job helpers ──────────────────────────────────────────────────────────────
def load_job(path: str) -> dict:
with open(path) as f:
return json.load(f)
def load_result(output_dir: str) -> dict | None:
rp = os.path.join(output_dir, "pipeline_result.json")
if os.path.exists(rp):
with open(rp) as f:
return json.load(f)
return None
def format_duration(seconds: float) -> str:
if seconds < 60:
return f"{seconds:.0f}s"
return f"{seconds//60:.0f}m {seconds%60:.0f}s"
# ── Pipeline runner ──────────────────────────────────────────────────────────
def run_job(job: dict, job_path: str, log_path: str) -> dict:
"""Run pipeline.py for one job, stream output to log."""
job_name = job.get("name", os.path.basename(job_path))
print(f"\n{'='*60}")
print(f" STARTING: {job_name}")
print(f" Source: {job['source_dir']}")
print(f" Mode: {job['mode']} | Target: {job['target_duration']}s")
print(f"{'='*60}")
write_heartbeat(
"in-progress",
f"Running {job_name}... Transcription step starting.",
"See Discord for updates on completion."
)
# Write job as active job_config.json for pipeline.py
config_path = os.path.join(PIPELINE_DIR, "job_config.json")
with open(config_path, "w") as f:
json.dump(job, f, indent=2)
proc = subprocess.Popen(
[sys.executable, "pipeline.py", "--config", config_path],
cwd=PIPELINE_DIR,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
last_step = "transcribe"
step_times = {}
with open(log_path, "w") as logf:
logf.write(f"Job: {job_name}\nConfig: {job_path}\n\n")
for line in proc.stdout:
decoded = line.decode("utf-8", errors="replace")
logf.write(decoded)
# Detect step transitions
for step in ("TRANSCRIBE", "SEGMENT", "SCORE", "SELECT", "EXPORT"):
if f"Step: {step}" in decoded:
last_step = step
step_times[step] = datetime.now()
write_heartbeat(
"in-progress",
f"[{job_name}] {step} step running...",
""
)
print(decoded, end="")
proc.wait()
result = load_result(job.get("output_dir", ""))
return {
"job_name": job_name,
"job_path": job_path,
"returncode": proc.returncode,
"result": result,
"log_path": log_path,
}
# ── Queue summary ─────────────────────────────────────────────────────────────
def build_summary(results: list[dict]) -> str:
lines = ["**Pipeline Queue Complete**\n"]
all_ok = True
for r in results:
status = "✅" if r["returncode"] == 0 else "❌"
name = r["job_name"]
if r["returncode"] == 0:
res = r["result"]
if res and res.get("step_results", {}).get("export", {}).get("exported"):
exp = res["step_results"]["export"]
out_files = []
export_data = res.get("job_state", {}).get("export", {}) if "job_state" in res else {}
# Re-read from result JSON for cleaner output
rp_path = None
for k, v in (res.get("job_state", {}) or {}).items():
if isinstance(v, dict) and v.get("success") and v.get("output_path"):
out_files.append(os.path.basename(v["output_path"]))
lines.append(f"{status} **{name}**")
lines.append(f" → {len(out_files)} clip(s) exported")
else:
lines.append(f"{status} **{name}**")
all_ok = False
else:
lines.append(f"{status} **{name}** — FAILED (see log)")
all_ok = False
return "\n".join(lines)
# ── Main ──────────────────────────────────────────────────────────────────────
def main():
parser = argparse.ArgumentParser(description="Pipeline queue runner")
parser.add_argument("--dry-run", action="store_true", help="Show jobs without running")
parser.add_argument("--add", action="store_true", help="Interactively add a new job")
parser.add_argument("jobs", nargs="*", help="Specific job files to run")
args = parser.parse_args()
# Collect jobs
if args.jobs:
jobs = args.jobs
else:
jobs = sorted(glob.glob(os.path.join(PIPELINE_DIR, "jobs", "*.json")))
if not jobs:
print("No jobs found in jobs/ folder.")
print("Use --add to create a new job, or add JSON files to jobs/")
sys.exit(1)
# Load and validate
loaded = []
for jp in jobs:
try:
j = load_job(jp)
if "source_dir" not in j:
print(f"SKIP {jp}: missing source_dir")
continue
j["_path"] = jp
loaded.append(j)
except Exception as e:
print(f"ERROR loading {jp}: {e}")
if not loaded:
print("No valid jobs to run.")
sys.exit(1)
# Dry run
if args.dry_run:
print(f"Queue has {len(loaded)} job(s):")
for j in loaded:
print(f" [{j['name']}] mode={j['mode']} target={j['target_duration']}s {j['source_dir']}")
print("\nRemove --dry-run to execute.")
sys.exit(0)
# Add job
if args.add:
print("Add new job interactively...")
name = input("Job name (e.g. my_folder): ").strip()
source = input("Source dir: ").strip()
output = input("Output dir [/path/source/processed]: ").strip()
duration = int(input("Target duration (seconds): ").strip() or "120")
mode = input("Mode [single]: ").strip() or "single"
if not output:
output = os.path.join(os.path.dirname(source.rstrip("/")), name, "processed")
job = {
"name": name,
"source_dir": source,
"output_dir": output,
"template": "classrecap",
"mode": mode,
"target_duration": duration,
"resolution": "720p",
"bitrate": "1M",
"templates_dir": os.path.join(PIPELINE_DIR, "templates"),
}
# Assign next number
existing = glob.glob(os.path.join(PIPELINE_DIR, "jobs", "*.json"))
next_num = len(existing) + 1
path = os.path.join(PIPELINE_DIR, "jobs", f"{next_num:03d}_{name}.json")
with open(path, "w") as f:
json.dump(job, f, indent=2)
print(f"Saved: {path}")
sys.exit(0)
# Execute
total = len(loaded)
print(f"\n{'#'*60}")
print(f" QUEUE: {total} job(s)")
for i, j in enumerate(loaded, 1):
print(f" {i}. {j['name']} [{j['mode']}] {j['target_duration']}s — {j['source_dir']}")
print(f"{'#'*60}\n")
results = []
start_time = datetime.now()
for i, job in enumerate(loaded, 1):
job_name = job.get("name", f"job_{i}")
log_path = os.path.join(LOG_DIR, f"queue_{i}_{job_name}.log")
write_heartbeat(
"in-progress",
f"Queue running: {job_name} ({i}/{total}). Whisper is the slow step.",
f"~15-25 min per video. See log: {log_path}"
)
r = run_job(job, job["_path"], log_path)
results.append(r)
if r["returncode"] != 0:
print(f"\n❌ JOB FAILED: {job_name}")
print(f" Log: {r['log_path']}")
notify(
f"⚠️ **Queue stopped**\n"
f"Job `{job_name}` failed (exit {r['returncode']}).\n"
f"Log: {r['log_path']}"
)
write_heartbeat("error", f"Job {job_name} failed. Queue stopped.", "")
break
elapsed = (datetime.now() - start_time).total_seconds()
remaining = total - i
print(f"\n✅ Completed {i}/{total}: {job_name} ({elapsed/60:.0f}m elapsed)")
# Queue complete
clear_heartbeat()
summary = build_summary(results)
print(f"\n{summary}")
notify(summary)
print(f"\nAll logs: /tmp/queue_*.log")
if __name__ == "__main__":
main()