🐎 Add task manager to avoid using too much CPU

This commit is contained in:
C-3PO 2018-09-14 06:11:00 +02:00
parent 93c47e2fbc
commit 19f00442b5
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
2 changed files with 66 additions and 29 deletions

View file

@ -4,9 +4,10 @@ import downloadUrlContents from '../cdn/downloadUrlContents';
import createDirRecursively from '../cdn/funcs/createDirRecursively'; import createDirRecursively from '../cdn/funcs/createDirRecursively';
import getUrlContents from '../cdn/getUrlContents'; import getUrlContents from '../cdn/getUrlContents';
import { Product } from '../interfaces/ISettings'; import { Product } from '../interfaces/ISettings';
import { SsnDiffType } from '../interfaces/ISsnFileEntry'; import { ISsnFileEntry, SsnDiffType } from '../interfaces/ISsnFileEntry';
import getSolidpkg from './getSolidpkg'; import getSolidpkg from './getSolidpkg';
import launch from './patcher-installer/launch'; import launch from './patcher-installer/launch';
import taskManager from './patcher-installer/taskManager';
import readSsnFile from './reader/readSsnFile'; import readSsnFile from './reader/readSsnFile';
import verifyPatch from './verify/verifyPatch'; import verifyPatch from './verify/verifyPatch';
import verifyProductName from './verify/verifyProductName'; import verifyProductName from './verify/verifyProductName';
@ -78,8 +79,7 @@ export default async function getPatch({ product, from, to, sourceDirectory, tar
//-------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------
//Perform the patching //Perform the patching
//Extract newly added files const extractAdded = async (file: ISsnFileEntry) => {
fileEntries.filter((file) => file.diffType === SsnDiffType.NewFile).forEach(async (file) => {
try { try {
//create file write stream //create file write stream
const outputName = path.join(targetDir, file.name); const outputName = path.join(targetDir, file.name);
@ -93,10 +93,9 @@ export default async function getPatch({ product, from, to, sourceDirectory, tar
} catch (error) { } catch (error) {
console.error(`Could not extract file "${file.name}"`, error); console.error(`Could not extract file "${file.name}"`, error);
} }
}); };
//Extract changed files const extractChanged = async (file: ISsnFileEntry) => {
fileEntries.filter((file) => file.diffType === SsnDiffType.Changed).forEach(async (file) => {
try { try {
const sourceFile = path.join(sourceDir, file.name); const sourceFile = path.join(sourceDir, file.name);
const outputName = path.join(targetDir, file.name); const outputName = path.join(targetDir, file.name);
@ -132,11 +131,9 @@ export default async function getPatch({ product, from, to, sourceDirectory, tar
console.error(`Could not extract file "${file.name}"`, error); console.error(`Could not extract file "${file.name}"`, error);
//TODO: need to delete .tmp file //TODO: need to delete .tmp file
} }
}); };
//Need to delete deleted files const deleteRemoved = (file: ISsnFileEntry) => {
if (sourceDir === targetDir) {
fileEntries.filter((file) => file.diffType === SsnDiffType.Deleted).forEach((file) => {
//delete file //delete file
const fileName = path.join(targetDir, file.name); const fileName = path.join(targetDir, file.name);
fs.unlink(fileName, (error) => { fs.unlink(fileName, (error) => {
@ -145,12 +142,9 @@ export default async function getPatch({ product, from, to, sourceDirectory, tar
} }
}); });
//TODO: delete folder (and parent folders) that are empty //TODO: delete folder (and parent folders) that are empty
}); };
}
//Need to copy unchanged files (if we are patching into a different directory) const copyUnchanged = async (file: ISsnFileEntry) => {
if (sourceDir !== targetDir) {
fileEntries.filter((file) => file.diffType === SsnDiffType.Unchanged).forEach(async (file) => {
//copy file //copy file
const sourceName = path.join(sourceDir, file.name); const sourceName = path.join(sourceDir, file.name);
const targetName = path.join(targetDir, file.name); const targetName = path.join(targetDir, file.name);
@ -160,8 +154,28 @@ export default async function getPatch({ product, from, to, sourceDirectory, tar
console.error(`Could not copy unchanged file "${file.name}"`, error); console.error(`Could not copy unchanged file "${file.name}"`, error);
} }
}); });
}); };
const tasks: Array<() => Promise<void>> = [];
//Extract newly added files
tasks.push(...fileEntries.filter((file) => file.diffType === SsnDiffType.NewFile).map((file) => extractAdded.bind(null, file)));
//Extract changed files
tasks.push(...fileEntries.filter((file) => file.diffType === SsnDiffType.Changed).map((file) => extractChanged.bind(null, file)));
//Need to delete removed files
if (sourceDir === targetDir) {
tasks.push(...fileEntries.filter((file) => file.diffType === SsnDiffType.Deleted).map((file) => deleteRemoved.bind(null, file)));
} }
//Need to copy unchanged files (if we are patching into a different directory)
if (sourceDir !== targetDir) {
tasks.push(...fileEntries.filter((file) => file.diffType === SsnDiffType.Unchanged).map((file) => copyUnchanged.bind(null, file)));
}
//run tasks
taskManager(tasks, 5);
//TODO: add option to delete downloaded files once patching is complete //TODO: add option to delete downloaded files once patching is complete
} }

View file

@ -0,0 +1,23 @@
export default function taskManager(tasks: Array<() => Promise<void>>, maxConcurrentTasks: number): Promise<void> {
return new Promise((resolve, reject) => {
const remainingTasks = tasks;
//const currentlyRunningTasks: Array<Promise<void>> = [];
const startNewTask = () => {
if (remainingTasks.length === 0) {
return resolve();
}
const curTask = remainingTasks.pop() as () => Promise<void>;
const curPromise = curTask();
//currentlyRunningTasks.push(curPromise);
curPromise.then(() => {
return startNewTask();
});
};
for (let i = 0; i < maxConcurrentTasks; i += 1) {
startNewTask();
}
});
}