| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- import { readFileSync, writeFileSync, existsSync, mkdirSync } from "node:fs";
- import { join } from "node:path";
- import { tmpdir } from "node:os";
- export interface JobData {
- id: string;
- status: "pending" | "running" | "completed" | "failed";
- input: {
- text: string;
- template: string;
- platform?: string;
- platforms?: string[];
- ttsProvider: string;
- voiceId?: string;
- };
- error?: string;
- outputFiles?: Array<{
- filePath: string;
- fileSizeBytes: number;
- width: number;
- height: number;
- durationSeconds: number;
- }>;
- createdAt: number;
- updatedAt: number;
- }
- const STORE_DIR = join(tmpdir(), "pipeline-jobs");
- const STORE_FILE = join(STORE_DIR, "jobs.json");
- function ensureStore(): Map<string, JobData> {
- if (!existsSync(STORE_DIR)) {
- mkdirSync(STORE_DIR, { recursive: true });
- }
- if (!existsSync(STORE_FILE)) {
- writeFileSync(STORE_FILE, "{}", "utf-8");
- }
- try {
- const raw = readFileSync(STORE_FILE, "utf-8");
- const obj = JSON.parse(raw) as Record<string, JobData>;
- return new Map(Object.entries(obj));
- } catch {
- return new Map();
- }
- }
- function flushStore(jobs: Map<string, JobData>): void {
- const obj: Record<string, JobData> = {};
- for (const [k, v] of jobs) {
- obj[k] = v;
- }
- writeFileSync(STORE_FILE, JSON.stringify(obj), "utf-8");
- }
- export function getAllJobs(): JobData[] {
- const jobs = ensureStore();
- return Array.from(jobs.values()).sort(
- (a, b) => b.createdAt - a.createdAt
- );
- }
- export function getJob(id: string): JobData | undefined {
- const jobs = ensureStore();
- return jobs.get(id);
- }
- export function createJob(input: JobData["input"]): JobData {
- const jobs = ensureStore();
- const now = Date.now();
- const job: JobData = {
- id: crypto.randomUUID(),
- status: "pending",
- input,
- createdAt: now,
- updatedAt: now,
- };
- jobs.set(job.id, job);
- flushStore(jobs);
- return job;
- }
- export function updateJob(id: string, updates: Partial<JobData>): JobData | undefined {
- const jobs = ensureStore();
- const job = jobs.get(id);
- if (!job) return undefined;
- Object.assign(job, updates, { updatedAt: Date.now() });
- flushStore(jobs);
- return job;
- }
|