Spaces:
Sleeping
Sleeping
File size: 4,760 Bytes
e890ceb |
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 |
// const { Queue, Worker, QueueScheduler, Job } = require('bullmq');
// const IORedis = require('ioredis');
// const fs = require('fs-extra');
// const path = require('path');
// const { v4: uuidv4 } = require('uuid');
// const { generateShadow } = require('./shadowGenerator'); // your existing shadow logic
// // Redis connection
// const connection = new IORedis();
// // Queue setup
// const shadowQueue = new Queue('shadowQueue', { connection });
// new QueueScheduler('shadowQueue', { connection });
// // RAM and disk buffers
// const RAM_LIMIT = 20; // max tasks in RAM
// const RAM_BUFFER = [];
// const DISK_BUFFER = path.join(__dirname, 'diskQueue');
// fs.ensureDirSync(DISK_BUFFER);
// // In-memory results
// const RESULTS = {};
// // ---------------- Add Task ----------------
// async function addTask(fileBuffer, options = {}) {
// const taskId = uuidv4(); // unique ID for each task
// const task = { id: taskId, file: fileBuffer, options };
// if (RAM_BUFFER.length < RAM_LIMIT) {
// RAM_BUFFER.push(task);
// processRAMBuffer();
// } else {
// // Move to disk
// const filePath = path.join(DISK_BUFFER, `${taskId}.json`);
// await fs.writeJson(filePath, task, { spaces: 0 });
// }
// return taskId;
// }
// // ---------------- Process RAM tasks ----------------
// let processing = false;
// async function processRAMBuffer() {
// if (processing || RAM_BUFFER.length === 0) return;
// processing = true;
// while (RAM_BUFFER.length > 0) {
// const task = RAM_BUFFER.shift();
// try {
// const imgBuffer = await generateShadow(task.file, task.options);
// RESULTS[task.id] = imgBuffer;
// } catch (err) {
// console.error(`Task ${task.id} failed:`, err);
// RESULTS[task.id] = null;
// }
// // Move tasks from disk to RAM if space available
// const diskFiles = await fs.readdir(DISK_BUFFER);
// while (RAM_BUFFER.length < RAM_LIMIT && diskFiles.length > 0) {
// const diskFile = diskFiles.shift();
// const taskFromDisk = await fs.readJson(path.join(DISK_BUFFER, diskFile));
// RAM_BUFFER.push(taskFromDisk);
// await fs.remove(path.join(DISK_BUFFER, diskFile));
// }
// }
// processing = false;
// }
// // ---------------- Check Task Status ----------------
// function getTaskStatus(taskId) {
// if (RESULTS[taskId] === undefined) return 'pending';
// if (RESULTS[taskId] === null) return 'failed';
// return 'done';
// }
// // ---------------- Fetch Result ----------------
// function getTaskResult(taskId) {
// return RESULTS[taskId] || null;
// }
// module.exports = {
// addTask,
// getTaskStatus,
// getTaskResult
// };
const { v4: uuidv4 } = require('uuid');
const { generateShadow } = require('../core/shadowGenerator');
const RAM_LIMIT = 20;
const RAM_BUFFER = [];
const RESULTS = {};
const CLEANUP_TIME = 10 * 60 * 1000; // 10 minutes
let processing = false;
// ---------------- Add Task ----------------
async function addTask(fileBuffer, options = {}) {
const taskId = uuidv4();
const task = { id: taskId, file: fileBuffer, options };
if (RAM_BUFFER.length < RAM_LIMIT) {
RAM_BUFFER.push(task);
processRAMBuffer();
} else {
return Promise.reject(new Error('Server busy, try again'));
}
return taskId;
}
// ---------------- Process RAM tasks ----------------
async function processRAMBuffer() {
if (processing || RAM_BUFFER.length === 0) return;
processing = true;
while (RAM_BUFFER.length > 0) {
const task = RAM_BUFFER.shift();
try {
const imgBuffer = await generateShadow(task.file, task.options);
RESULTS[task.id] = imgBuffer;
// Auto-clean after CLEANUP_TIME
setTimeout(() => {
delete RESULTS[task.id];
}, CLEANUP_TIME);
} catch (err) {
console.error(`Task ${task.id} failed:`, err);
RESULTS[task.id] = null;
}
}
processing = false;
}
// ---------------- Check Task Status ----------------
function getTaskStatus(taskId) {
if (RESULTS[taskId] === undefined) return 'pending';
if (RESULTS[taskId] === null) return 'failed';
return 'done';
}
// ---------------- Fetch Result ----------------
function getTaskResult(taskId) {
return RESULTS[taskId] || null;
}
// ---------------- Queue Length ----------------
function getQueueLength() {
return RAM_BUFFER.length;
}
module.exports = {
addTask,
getTaskStatus,
getTaskResult,
getQueueLength
};
|