From 79abbd89e13d1a1cf387963d33d242024713133d Mon Sep 17 00:00:00 2001 From: AnxietyisReal <96593068+AnxietyisReal@users.noreply.github.com> Date: Mon, 16 Oct 2023 04:32:29 +1100 Subject: [PATCH] Offload `commitHashes()` to worker thread --- src/commands/statistics.ts | 23 ++++++++++++----------- src/helpers/CommitHashes.ts | 10 ++++++++++ 2 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 src/helpers/CommitHashes.ts diff --git a/src/commands/statistics.ts b/src/commands/statistics.ts index 0403b1e..cb9a2c3 100644 --- a/src/commands/statistics.ts +++ b/src/commands/statistics.ts @@ -1,3 +1,7 @@ +interface CommitHashes { + localHash: string, + remoteHash: string +} import Discord from 'discord.js'; import pkg from 'typescript'; import MessageTool from '../helpers/MessageTool.js'; @@ -10,14 +14,11 @@ import os from 'node:os'; import {Octokit} from '@octokit/rest'; import {createTokenAuth} from '@octokit/auth-token'; import {readFileSync} from 'node:fs'; -import {execSync} from 'node:child_process'; +import {Worker} from 'node:worker_threads'; const packageJson = JSON.parse(readFileSync('package.json', 'utf8')); -function commitHashes() { - const localHash = execSync('git rev-parse HEAD').toString().trim().slice(0, 7); - const remoteHash = execSync('git ls-remote origin HEAD').toString().split('\t')[0].slice(0, 7); - return { localHash, remoteHash }; -} +const workerThread = new Worker(new URL('../helpers/CommitHashes.js', import.meta.url)); +const hashData = await new Promise(resolve=>workerThread.on('message', (data:CommitHashes)=>resolve(data))); export default { async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){ @@ -59,15 +60,15 @@ export default { let githubRepo = {owner: 'AnxietyisReal', repo: 'Daggerbot-TS', ref: 'HEAD'}; const octokit = new Octokit({auth: token, timeZone: 'Australia/NSW', userAgent: 'Daggerbot-TS'}); const github = { - remoteCommit: await octokit.repos.getCommit({...githubRepo, ref: commitHashes().remoteHash}), - localCommit: await octokit.repos.getCommit({...githubRepo, ref: commitHashes().localHash}), + remoteCommit: await octokit.repos.getCommit({...githubRepo, ref: hashData.remoteHash}), + localCommit: await octokit.repos.getCommit({...githubRepo, ref: hashData.localHash}), } embed.addFields( { name: '> __Repository__', value: MessageTool.concatMessage( - `**Local:** [${commitHashes().localHash}](${github.localCommit.data.html_url})`, - `**Remote:** [${commitHashes().remoteHash}](${github.remoteCommit.data.html_url})`, + `**Local:** [${hashData.localHash}](${github.localCommit.data.html_url})`, + `**Remote:** [${hashData.remoteHash}](${github.remoteCommit.data.html_url})` ) }, {name: '> __Dependencies__', value: MessageTool.concatMessage( @@ -90,4 +91,4 @@ export default { data: new Discord.SlashCommandBuilder() .setName('statistics') .setDescription('See a list of commands ordered by their usage or host stats') -} \ No newline at end of file +} diff --git a/src/helpers/CommitHashes.ts b/src/helpers/CommitHashes.ts new file mode 100644 index 0000000..31c0099 --- /dev/null +++ b/src/helpers/CommitHashes.ts @@ -0,0 +1,10 @@ +import {parentPort} from 'node:worker_threads'; +import {execSync} from 'node:child_process'; + +async function commitHashes() { + const localHash = execSync('git rev-parse HEAD').toString().trim().slice(0, 7); + const remoteHash = execSync('git ls-remote origin HEAD').toString().split('\t')[0].slice(0, 7); + return { localHash, remoteHash }; +} + +commitHashes().then(data=>parentPort.postMessage(data))