Skip to content

Commit

Permalink
clustering
Browse files Browse the repository at this point in the history
  • Loading branch information
Dougley committed Oct 31, 2021
1 parent 78f452d commit 3b37fc6
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 36 deletions.
34 changes: 31 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.2.0",
"configurations": [
{
"name": "Launch Program - TSC Compiled",
"name": "Cluster - TSC Compiled",
"program": "${workspaceFolder}/dist/index.js",
"request": "launch",
"skipFiles": [
Expand All @@ -19,7 +19,7 @@
"type": "pwa-node"
},
{
"name": "Launch Program - ts-node with Debugger",
"name": "Cluster - ts-node with Debugger",
"type": "pwa-node",
"request": "launch",
"runtimeArgs": [
Expand All @@ -31,6 +31,34 @@
"args": [
"${workspaceFolder}/src/index.ts"
]
}
},
{
"name": "Plain - TSC Compiled",
"program": "${workspaceFolder}/dist/entry.js",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"runtimeArgs": [
"-r",
"dotenv/config"
],
"preLaunchTask": "tsc: build - tsconfig.json",
"type": "pwa-node"
},
{
"name": "Plain - ts-node with Debugger",
"type": "pwa-node",
"request": "launch",
"runtimeArgs": [
"-r",
"ts-node/register",
"-r",
"dotenv/config"
],
"args": [
"${workspaceFolder}/src/entry.ts"
]
},
]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"main": "dist/index.js",
"scripts": {
"start": "node --enable-source-maps -r dotenv/config dist/index.js",
"start:plain": "node --enable-source-maps -r dotenv/config dist/entry.js",
"test": "eslint src/**/*.ts",
"lint": "eslint src/**/*.ts --fix",
"compile": "tsc",
Expand Down
32 changes: 32 additions & 0 deletions src/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { fatal, info } from './utils/logger'
import client from './structures/client'
import * as Sentry from '@sentry/node'
import { RewriteFrames } from '@sentry/integrations'
import { promisify } from 'util'
import { exec } from 'child_process'
import dirImport from './utils/dir-import'

info('Starting up...', 'Preflight');

(async () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const revision = (await promisify(exec)('git rev-parse HEAD').catch(() => { return { stdout: require('../package.json').version } })).stdout.toString().trim()
info(`Initialzing Sentry, using revision ${revision as string}`, 'Preflight')
Sentry.init({
dsn: process.env.SENTRY_DSN,
integrations: function (integrations) {
return integrations
.concat(new RewriteFrames({ root: __dirname ?? process.cwd() }))
.filter(function (integration) {
return integration.name !== 'Console'
})
},
release: revision
})
await dirImport('@(dist|src)/languages/**/*.[?jt]s')
await dirImport('@(dist|src)/events/**/*.[?jt]s')
await client.addMultipleIn('./interactions')
await client.run()
await dirImport('@(dist|src)/jobs/**/*.client.[?jt]s')
await client.checkAndUploadCommands()
})().catch(e => fatal(e, 'Startup'))
41 changes: 14 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
import { fatal, info } from './utils/logger'
import client from './structures/client'
import * as Sentry from '@sentry/node'
import { RewriteFrames } from '@sentry/integrations'
import { promisify } from 'util'
import { exec } from 'child_process'
import cluster from './structures/cluster'
import dirImport from './utils/dir-import'
import { fatal, info, warn } from './utils/logger'

info('Starting up...', 'Preflight');
cluster.on('clusterProcess', (payload) => {
payload.clusterProcess.on('ready', () => {
info('Cluster has reported ready', `Cluster ${payload.clusterProcess.clusterId}`)
})
payload.clusterProcess.on('warn', ctx => {
warn(ctx.error, `Cluster ${payload.clusterProcess.clusterId}`)
})
});

(async () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const revision = (await promisify(exec)('git rev-parse HEAD').catch(() => { return { stdout: require('../package.json').version } })).stdout.toString().trim()
info(`Initialzing Sentry, using revision ${revision as string}`, 'Preflight')
Sentry.init({
dsn: process.env.SENTRY_DSN,
integrations: function (integrations) {
return integrations
.concat(new RewriteFrames({ root: __dirname ?? process.cwd() }))
.filter(function (integration) {
return integration.name !== 'Console'
})
},
release: revision
})
await dirImport('@(dist|src)/languages/**/*.[?jt]s')
await dirImport('@(dist|src)/events/**/*.[?jt]s')
await client.addMultipleIn('./interactions')
await client.run()
await client.checkAndUploadCommands()
})().catch(e => fatal(e, 'Startup'))
await cluster.run()
info(`Now running ${cluster.clusterCount} clusters with ${cluster.shardsPerCluster} shards on each one`, 'Clustering')
await dirImport('@(dist|src)/jobs/**/*.cluster.[?jt]s')
})().catch(e => fatal(e, 'Clustering'))
6 changes: 0 additions & 6 deletions src/structures/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,4 @@ const client = new InteractionCommandClient(process.env.BOT_TOKEN ?? '', {
}
}) as ModClient

if ((process.env.WILDBEAST_SHARDING_START != null) && (process.env.WILDBEAST_SHARDING_END != null) && (process.env.WILDBEAST_SHARDING_TOTAL != null)) {
client.client.setShardCount(+process.env.WILDBEAST_SHARDING_TOTAL)
client.client.setShardStart(+process.env.WILDBEAST_SHARDING_START)
client.client.setShardEnd(+process.env.WILDBEAST_SHARDING_END)
}

export default client
10 changes: 10 additions & 0 deletions src/structures/cluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ClusterManager } from 'detritus-client'

const IS_TS_NODE = Symbol.for('ts-node.register.instance') in process

// the filename is NOT relative to this file
// but to where the file is being run first
// which in this case is the index.ts file
const manager = new ClusterManager(IS_TS_NODE ? './entry.ts' : './entry.js', process.env.BOT_TOKEN ?? '')

export default manager

0 comments on commit 3b37fc6

Please sign in to comment.