-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[engine] Worker를 활용한 깃 로그 받아오기 #713
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,13 @@ | |
import { deleteGithubToken, getGithubToken, setGithubToken } from "./setting-repository"; | ||
import { mapClusterNodesFrom } from "./utils/csm.mapper"; | ||
import { | ||
fetchGitLogInParallel, | ||
findGit, | ||
getBranches, | ||
getCurrentBranchName, | ||
getDefaultBranchName, | ||
getGitConfig, | ||
getGitLog, | ||
getRepo, | ||
} from "./utils/git.util"; | ||
import WebviewLoader from "./webview-loader"; | ||
|
@@ -74,7 +75,15 @@ | |
|
||
const initialBaseBranchName = await fetchCurrentBranch(); | ||
const fetchClusterNodes = async (baseBranchName = initialBaseBranchName) => { | ||
const gitLog = await getGitLog(gitPath, currentWorkspacePath); | ||
console.time('Multi log') | ||
const gitLog = await fetchGitLogInParallel(gitPath, currentWorkspacePath); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네, |
||
console.timeEnd('Multi log') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 추후(feature 개발이 끝난 이후) 에는 이 부분도 삭제하면 좋겠습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 마지막에 브랜치 병합할 때 삭제하도록하겠습니다 |
||
|
||
// console.time('Single log') | ||
// const testGitLog = await getGitLog(gitPath, currentWorkspacePath); | ||
// console.timeEnd('Single log') | ||
Comment on lines
+82
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요거 지워도 될 것 같아요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 위 코드는 기존 방식으로 깃 로그를 받아오는 코드입니다. 추후에 사용할 가능성이 있어, 주석으로 처리해두었습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다시 생각해보니 굳이 살려둘 필요도 없겠네요, 지우도록 하겠습니다~ |
||
|
||
|
||
const gitConfig = await getGitConfig(gitPath, currentWorkspacePath, "origin"); | ||
const { owner, repo: initialRepo } = getRepo(gitConfig); | ||
webLoader.setGlobalOwnerAndRepo(owner, initialRepo); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import * as cp from "child_process"; | ||
import * as fs from "fs"; | ||
import os from 'os'; | ||
import * as path from "path"; | ||
import { Worker } from 'worker_threads'; | ||
|
||
|
||
|
||
export interface GitExecutable { | ||
readonly path: string; | ||
|
@@ -111,7 +115,7 @@ | |
if (await isExecutable(file)) { | ||
try { | ||
return await getGitExecutable(file); | ||
} catch (_) {} | ||
} | ||
} | ||
return Promise.reject<GitExecutable>(); | ||
|
@@ -147,13 +151,14 @@ | |
for (let i = 0; i < paths.length; i++) { | ||
try { | ||
return await getGitExecutable(paths[i]); | ||
} catch (_) {} | ||
} | ||
throw new Error("None of the provided paths are a Git executable"); | ||
} | ||
|
||
export async function getGitLog(gitPath: string, currentWorkspacePath: string): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
|
||
const args = [ | ||
"--no-pager", | ||
"log", | ||
|
@@ -182,6 +187,68 @@ | |
}); | ||
} | ||
|
||
export async function getLogCount(gitPath: string, currentWorkspacePath: string): Promise<number> { | ||
return new Promise((resolve, reject) => { | ||
const args = [ | ||
"rev-list", | ||
"--count", | ||
"--all", | ||
]; | ||
|
||
resolveSpawnOutput( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. os module을 사용하는 경우, multi OS에서의 테스트도 필요합니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 공부해보고 테스트 케이스 작성해보겠습니다! |
||
cp.spawn(gitPath, args, { | ||
cwd: currentWorkspacePath, | ||
env: Object.assign({}, process.env), | ||
}) | ||
).then(([status, stdout, stderr]) => { | ||
const { code, error } = status; | ||
|
||
if (code === 0 && !error) { | ||
const commitCount = parseInt(stdout.toString().trim(), 10); // Buffer를 문자열로 변환 후 숫자로 변환 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵! |
||
resolve(commitCount); // 숫자를 반환 | ||
} else { | ||
reject(stderr); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
|
||
export async function fetchGitLogInParallel(gitPath: string, currentWorkspacePath: string): Promise<string> { | ||
const numCores = os.cpus().length; | ||
const numberOfThreads = Math.max(1, numCores - 1) | ||
const totalCnt = await getLogCount(gitPath, currentWorkspacePath); | ||
console.log("total logs", totalCnt); | ||
const chunkSize = Math.ceil(totalCnt/ numberOfThreads); | ||
const promises: Promise<string>[] = []; | ||
|
||
for (let i = 0; i < numberOfThreads; i++) { | ||
const skipCount = i * chunkSize; | ||
const limitCount = chunkSize; | ||
|
||
console.log('__dirname:', __dirname); | ||
const worker = new Worker(path.resolve(__dirname, './worker.js'), { | ||
workerData: { | ||
gitPath, | ||
currentWorkspacePath, | ||
skipCount, | ||
limitCount, | ||
}, | ||
}); | ||
|
||
promises.push( | ||
new Promise((resolve, reject) => { | ||
worker.on('message', resolve); | ||
worker.on('error', reject); | ||
}) | ||
); | ||
} | ||
|
||
return Promise.all(promises).then((logs) => logs.join('\n')); | ||
} | ||
|
||
|
||
|
||
export async function getGitConfig( | ||
gitPath: string, | ||
currentWorkspacePath: string, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as cp from 'child_process'; | ||
import { parentPort, workerData } from 'worker_threads'; | ||
|
||
import { resolveSpawnOutput } from './git.util' | ||
|
||
const { gitPath, currentWorkspacePath, skipCount, limitCount } = workerData; | ||
|
||
async function getPartialGitLog() { | ||
const args = [ | ||
'--no-pager', | ||
'log', | ||
'--all', | ||
'--parents', | ||
'--numstat', | ||
'--date-order', | ||
'--pretty=fuller', | ||
'--decorate', | ||
'-c', | ||
`--skip=${skipCount}`, | ||
`-n ${limitCount}`, | ||
]; | ||
|
||
resolveSpawnOutput( | ||
cp.spawn(gitPath, args, { | ||
cwd: currentWorkspacePath, | ||
env: Object.assign({}, process.env), | ||
}) | ||
).then(([status, stdout, stderr]) => { | ||
const { code, error } = status; | ||
|
||
if (code === 0 && !error && parentPort !== null) { | ||
parentPort.postMessage(stdout.toString()); | ||
} else { | ||
if (parentPort !== null) parentPort.postMessage(stderr); | ||
} | ||
}).catch(error => { | ||
console.error('Spawn Error:', error); | ||
}); | ||
} | ||
|
||
getPartialGitLog(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,15 @@ const extensionConfig = { | |
target: "node", // vscode extensions run in a Node.js-context 📖 -> https://webpack.js.org/configuration/node/ | ||
mode: "none", // this leaves the source code as close as possible to the original (when packaging we set this to 'production') | ||
|
||
entry: "./src/extension.ts", // the entry point of this extension, 📖 -> https://webpack.js.org/configuration/entry-context/ | ||
entry: { | ||
extension: "./src/extension.ts", // the entry point of this extension, 📖 -> https://webpack.js.org/configuration/entry-context/ | ||
worker: "./src/utils/git.worker.ts" | ||
}, | ||
output: { | ||
// the bundle is stored in the 'dist' folder (check package.json), 📖 -> https://webpack.js.org/configuration/output/ | ||
path: path.resolve(__dirname, "dist"), | ||
filename: "extension.js", | ||
// filename: "extension.js", | ||
filename: "[name].js", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (질문) 설정을 이렇게 해두면 extension.js, worker.js 이렇게 두벌 나오는 건가용? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 소현님 예리하시군요!! 저도 궁금합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵, 정확하십니다! const worker = new Worker(path.resolve(__dirname, './worker.js'), {
workerData: {
gitPath,
currentWorkspacePath,
skipCount,
limitCount,
},
}); |
||
libraryTarget: "commonjs2", | ||
}, | ||
externals: { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
추가하신 목적이 궁금합니다~.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분은 사실 초기에 Worker가 빌드되지 않은(dist에 있지 않은) 외부 파일을 실행할 때 발생한 에러를 고치기 위해 삽입한 코드입니다.
import
대신require
를 사용해야해서 위 코드를 삽입했는데 기억은 잘 나지 않지만require
를 사용해도 문제가 해결되지 않았습니다.해당 문제는 외부 파일(git.worker.ts)를 따로 번들링하여 해결했습니다.
뭔가 말이 많았는데 해당 코드는 오류 해결을 위해 시도하다가 생긴 코드로 지금은 필요없습니다!
삭제하도록 하겠습니다!