🐎 Add task manager to avoid using too much CPU
This commit is contained in:
parent
93c47e2fbc
commit
19f00442b5
2 changed files with 66 additions and 29 deletions
|
@ -4,9 +4,10 @@ import downloadUrlContents from '../cdn/downloadUrlContents';
|
|||
import createDirRecursively from '../cdn/funcs/createDirRecursively';
|
||||
import getUrlContents from '../cdn/getUrlContents';
|
||||
import { Product } from '../interfaces/ISettings';
|
||||
import { SsnDiffType } from '../interfaces/ISsnFileEntry';
|
||||
import { ISsnFileEntry, SsnDiffType } from '../interfaces/ISsnFileEntry';
|
||||
import getSolidpkg from './getSolidpkg';
|
||||
import launch from './patcher-installer/launch';
|
||||
import taskManager from './patcher-installer/taskManager';
|
||||
import readSsnFile from './reader/readSsnFile';
|
||||
import verifyPatch from './verify/verifyPatch';
|
||||
import verifyProductName from './verify/verifyProductName';
|
||||
|
@ -78,8 +79,7 @@ export default async function getPatch({ product, from, to, sourceDirectory, tar
|
|||
//--------------------------------------------------------------------------------------------------------------
|
||||
//Perform the patching
|
||||
|
||||
//Extract newly added files
|
||||
fileEntries.filter((file) => file.diffType === SsnDiffType.NewFile).forEach(async (file) => {
|
||||
const extractAdded = async (file: ISsnFileEntry) => {
|
||||
try {
|
||||
//create file write stream
|
||||
const outputName = path.join(targetDir, file.name);
|
||||
|
@ -93,10 +93,9 @@ export default async function getPatch({ product, from, to, sourceDirectory, tar
|
|||
} catch (error) {
|
||||
console.error(`Could not extract file "${file.name}"`, error);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
//Extract changed files
|
||||
fileEntries.filter((file) => file.diffType === SsnDiffType.Changed).forEach(async (file) => {
|
||||
const extractChanged = async (file: ISsnFileEntry) => {
|
||||
try {
|
||||
const sourceFile = path.join(sourceDir, 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);
|
||||
//TODO: need to delete .tmp file
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
//Need to delete deleted files
|
||||
if (sourceDir === targetDir) {
|
||||
fileEntries.filter((file) => file.diffType === SsnDiffType.Deleted).forEach((file) => {
|
||||
const deleteRemoved = (file: ISsnFileEntry) => {
|
||||
//delete file
|
||||
const fileName = path.join(targetDir, file.name);
|
||||
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
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
//Need to copy unchanged files (if we are patching into a different directory)
|
||||
if (sourceDir !== targetDir) {
|
||||
fileEntries.filter((file) => file.diffType === SsnDiffType.Unchanged).forEach(async (file) => {
|
||||
const copyUnchanged = async (file: ISsnFileEntry) => {
|
||||
//copy file
|
||||
const sourceName = path.join(sourceDir, 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);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
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
|
||||
}
|
||||
|
|
23
src/ssn/patcher-installer/taskManager.ts
Normal file
23
src/ssn/patcher-installer/taskManager.ts
Normal 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();
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue