CcSimple / electron-hiprint

MIT License
76 stars 39 forks source link

队列优化建议 #33

Closed wslsq closed 2 months ago

wslsq commented 2 months ago

现在用的队列concurrent-tasks是阻塞的, 假设一次打印用5秒,于10.00.00发了2个news:A和B,日志如下, [10.00.00]收到A [10.00.00]执行A [10.00.05]收到B [10.00.05]执行B

理想状态下 [10.00.00]收到A [10.00.00]收到B [10.00.00]执行A [10.00.05]执行B

这样如果需要在收到任务做相关操作,会非常不方便。 一般插入任务不影响,另外起一个单并发消费任务会好一点。 如这个库 https://github.com/hajipy/embedded-queue

谢谢

wslsq commented 2 months ago

main.js

const EmbeddedQueue = require("embedded-queue");
const sleep = require('util').promisify(setTimeout);
// global.PRINT_RUNNER
// global.PRINT_RUNNER_DONE
global.PRINT_QUEUE = null;
// 队列
EmbeddedQueue.Queue.createQueue({ inMemoryOnly: true }).then((queue)=>{
    queue.process(
        "print",
        async (job) => {
            data = job.data;
            data.taskId = job.id;
            // 0为失败,1为开始,99为成功(可以在具体执行中设置进度)
            for (let i =0; i<=10000; i++){
                job = await PRINT_QUEUE.findJob(job.id);
                if (i == 0){
                    job.setProgress(1, 100);
                    PRINT_WINDOW.webContents.send("print-new", job.data);
                    MAIN_WINDOW.webContents.send("printTask", true);
                }
                console.log('jobProgress=====>'+job.progress)
                if (job.progress == 99 || job.progress == 0){
                    PRINT_QUEUE.listJobs('INACTIVE').then((waitJobs)=>{
                        MAIN_WINDOW.webContents.send("printTask", waitJobs.length > 0);
                    });
                    break;
                }
                await sleep(6000); // 根据实际优化
            }
        },
        1 //并发固定1
    );
    // handle job complete event
    queue.on(
        EmbeddedQueue.Event.Complete,
        (job, result) => {
            console.log("Job Completed.");
            console.log(`    job.id: ${job.id}`);
            console.log(`    job.type: ${job.type}`);
            console.log(`    job.data: ${JSON.stringify(job.data)}`);
            console.log(`    result: ${result}`);
        }
    );
    global.PRINT_QUEUE = queue;
});

utils.js

client.on("news", (data) => {
    if (data) {
      console.log('收到news');
      data.socketId = client.id;
      data.clientType = "transit";
      // 注意删掉了taskId
      PRINT_QUEUE.createJob({
        type: "print",
        data: { ...data },
      }).then((job)=>{ });
    }
});

print.js

printXX(data).then(()=>{
    PRINT_QUEUE.findJob(data.taskId).then((job)=>{
        job.setProgress(99, 100)
    });
}).catch((err) => {
    PRINT_QUEUE.findJob(data.taskId).then((job)=>{
        job.setProgress(0, 100)
    });
})
.finally(() => {
    //原操作删除
});

printXX = (data)=>{
    return new Promise((resolve, reject) => {
        // 转码成功
        PRINT_QUEUE.findJob(data.taskId).then((job)=>{
            job.setProgress(50, 100)
        });
        // 下载成功
        PRINT_QUEUE.findJob(data.taskId).then((job)=>{
            job.setProgress(60, 100)
        });
        // 发送打印成功
        PRINT_QUEUE.findJob(data.taskId).then((job)=>{
            job.setProgress(70, 100)
        });
    })
}   
Xavier9896 commented 2 months ago

如果可以的话请提交pr

wslsq commented 2 months ago

如果可以的话请提交pr

你好,我对PRINT_FRAGMENTS_MAPPING的功能不太了解,我改好其他地方,你补充修改PRINT_FRAGMENTS_MAPPING如何?

wslsq commented 2 months ago

似乎PRINT_FRAGMENTS_MAPPING并不影响队列