File size: 1,435 Bytes
9b06c8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const jobs = {}; // In-memory store

// Job Structure:
// {
//   id: "batch-123-0",
//   url: "https://...",
//   status: "queued" | "downloading" | "ready_to_crop" | "processing" | "completed" | "error",
//   videoPath: "/tmp/vid.mp4",
//   framePath: "/tmp/frame.jpg",
//   outputPath: null,
//   error: null
// }

function createBatchJob(url, batchId) {
  const id = `${batchId}-${Date.now()}-${Math.random().toString(36).substr(2, 5)}`;
  jobs[id] = {
    id,
    batchId,
    url,
    status: "queued",
    timestamp: Date.now()
  };
  return id;
}

function getJob(id) {
  return jobs[id];
}

function getBatchStatus(batchId) {
  return Object.values(jobs)
    .filter(j => j.batchId === batchId)
    .sort((a, b) => a.timestamp - b.timestamp); // Maintain order
}

function updateJob(id, updates) {
  if (jobs[id]) {
    Object.assign(jobs[id], updates);
  }
}

function getNextReadyJob(batchId) {
  return Object.values(jobs)
    .filter(j => j.batchId === batchId && j.status === "ready_to_crop")
    .sort((a, b) => a.timestamp - b.timestamp)[0];
}

// Cleanup old jobs (prevent memory leak)
setInterval(() => {
  const now = Date.now();
  Object.keys(jobs).forEach(id => {
    if (now - jobs[id].timestamp > 2 * 60 * 60 * 1000) { // 2 hours
        // Ideally delete files here too
        delete jobs[id];
    }
  });
}, 60000 * 30);

module.exports = { createBatchJob, getJob, getBatchStatus, updateJob, getNextReadyJob };