Skip to content

Instantly share code, notes, and snippets.

@LeonanCarvalho
Last active March 12, 2021 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeonanCarvalho/1d96dfb43ac564f98f206a92fe501d3b to your computer and use it in GitHub Desktop.
Save LeonanCarvalho/1d96dfb43ac564f98f206a92fe501d3b to your computer and use it in GitHub Desktop.
Simple demonstration how to use Workers on electron main process
//tasks/fileCheck.js
const {parentPort, workerData} = require('worker_threads');
const fs = require('fs')
const crypto = require('crypto')
const sendMessage = (type, value) => {
const msg = {type: type, value: value};
parentPort.postMessage(msg);
}
let corruptedFiles = [];
const {cwd, files,hashType} = workerData;
files.forEach(file => {
try{
sendMessage("StartCheck", {file: file})
const filePath = cwd + file.path;
if (fs.existsSync(filePath)) {
console.info("Checking", filePath)
let handler = fs.readFileSync(filePath);
let stats = fs.statSync(filePath)
let hash = crypto.createHash(hashType).update(handler).digest("hex");
if(hash != file.hash){ //Incorrect Hash
corruptedFiles.push(file);
}
}else{//Not found
corruptedFiles.push(file);
}
sendMessage("endCheck", {file: file})
}catch (err) {
sendMessage("errorCheck", {err:err,file: file})
}
});
sendMessage("done", corruptedFiles);
//Task.js
const path = require('path');
const fs = require('fs');
const { Worker } = require('worker_threads')
function checkFileHashTask(files, cwd = "./", hashType = 'md5', msgHandler){
return new Promise((resolve, reject) => {
const workerData = {cwd: cwd, files:files,hashType:hashType};
//This Work on develop mode running electron . for example, but doesn't im production (electron-builder)
let worker = new Worker(path.join(__dirname, 'tasks', fileName), {workerData});
console.info(`Starting Task FileCheck ${worker.threadId}`);
worker.on('message', msg => {
if(msg.type == "done"){
resolve(msg.value)
}
if(typeof msgHandler == 'function'){
msgHandler(msg);
}
});
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0)
reject(new Error(`Checkfiles stopped with exit code ${code}`));
})
})
}
//Run the worker:
const fileList = [
{"path": "/path/to/cats.gif", hash:"1236546s5d4s6a5d4321123"}
]; //This array will be splited in number of cpus to make paralell hash checking.
checkFileHashTask(fileList, "./", 'md5', msg => {
console.debug('Received Message', msg);
}).then(corruptedFiles => {
console.info(`There are ${corruptedFiles.length} file(s)`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment