Skip to content
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

feat(core): experimental sharding support #259

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chatty-crews-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'robo.js': patch
---

refactor(core): moved data loading into robo.start() in production mode
5 changes: 5 additions & 0 deletions .changeset/cuddly-bulldogs-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'robo.js': patch
---

feat: experimental sharding support
2 changes: 2 additions & 0 deletions docs/docs/robojs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ Activate experimental features or revert to older behaviors for compatibility. T
- `buildDirectory`: Determine where to compile your code. The default is `.robo/build`, but you can specify another location.
- `disableBot`: Turn off bot features, allowing you to run Robo.js without a bot.
- `incrementalBuilds`: Enable incremental builds to improve build performance by only recompiling changed files.
- `shard`: Enable sharding support. Can be `true` or a `ShardingManagerOptions` object.
- `userInstall`: Optimize command registration for user-installed apps.

```js
experimental: {
buildDirectory: 'dist',
disableBot: true,
incrementalBuilds: true,
shard: true,
userInstall: true
}
```
Expand Down
34 changes: 6 additions & 28 deletions packages/robo/src/cli/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { logger } from '../../core/logger.js'
import { hasFilesRecursively } from '../utils/fs-helper.js'
import { color, composeColors } from '../../core/color.js'
import { loadConfig } from '../../core/config.js'
import { Flashcore, prepareFlashcore } from '../../core/flashcore.js'
import { FLASHCORE_KEYS, Indent } from '../../core/constants.js'
import { loadState } from '../../core/state.js'
import { Indent } from '../../core/constants.js'

const command = new Command('start')
.description('Starts your bot in production mode.')
Expand Down Expand Up @@ -57,12 +55,10 @@ async function startAction(_args: string[], options: StartCommandOptions) {
await fs.access(path.join('.robo', 'manifest.json'))
} catch (err) {
logger.error(
`The ${color.bold(
'.robo/manifest.json'
)} file is missing. Make sure your project structure is correct and run ${composeColors(
`The manifest file is missing. Make sure your project structure is correct and run ${composeColors(
color.bold,
color.blue
)('"robo build"')} again.`
color.cyan
)('robo build')} again.`
)
process.exit(1)
}
Expand All @@ -77,27 +73,9 @@ async function startAction(_args: string[], options: StartCommandOptions) {
logger.warn(`Experimental flags enabled: ${features}.`)
}

// Load state from Flashcore
const stateStart = Date.now()
const stateLoadPromise = new Promise<void>((resolve) => {
async function load() {
await prepareFlashcore()
const state = await Flashcore.get<Record<string, unknown>>(FLASHCORE_KEYS.state)
if (state) {
loadState(state)
}

logger.debug(`State loaded in ${Date.now() - stateStart}ms`)
resolve()
}
load()
})

// Imported dynamically to prevent multiple process hooks
// Start Roboooooooo!! :D (dynamic to avoid premature process hooks)
const { Robo } = await import('../../core/robo.js')

// Start Roboooooooo!! :D
Robo.start({
stateLoad: stateLoadPromise
shard: !!config.experimental?.shard
})
}
3 changes: 3 additions & 0 deletions packages/robo/src/cli/shard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
process.removeAllListeners('warning')
import { Robo } from '../core/robo.js'
Robo.start()
1 change: 1 addition & 0 deletions packages/robo/src/cli/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { IS_BUN } from './runtime-utils.js'
import type { Pod } from '../../roboplay/types.js'

export const __DIRNAME = path.dirname(fileURLToPath(import.meta.url))
export const PackageDir = path.resolve(__DIRNAME, '..', '..', '..')

const execAsync = promisify(nodeExec)

Expand Down
41 changes: 35 additions & 6 deletions packages/robo/src/core/robo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { color } from './color.js'
import { registerProcessEvents } from './process.js'
import { Client, Collection, Events } from 'discord.js'
import { getConfig, loadConfig } from './config.js'
import { discordLogger } from './constants.js'
import { FLASHCORE_KEYS, discordLogger } from './constants.js'
import { logger } from './logger.js'
import { loadManifest } from '../cli/utils/manifest.js'
import { env } from './env.js'
Expand All @@ -12,9 +12,11 @@ import {
executeContextHandler,
executeEventHandler
} from './handlers.js'
import { hasProperties } from '../cli/utils/utils.js'
import { prepareFlashcore } from './flashcore.js'
import { hasProperties, PackageDir } from '../cli/utils/utils.js'
import { Flashcore, prepareFlashcore } from './flashcore.js'
import { loadState } from './state.js'
import Portal from './portal.js'
import path from 'node:path'
import { isMainThread, parentPort } from 'node:worker_threads'
import type { PluginData } from '../types/index.js'
import type { AutocompleteInteraction, CommandInteraction } from 'discord.js'
Expand All @@ -32,11 +34,12 @@ let plugins: Collection<string, PluginData>

interface StartOptions {
client?: Client
shard?: string | boolean
stateLoad?: Promise<void>
}

async function start(options?: StartOptions) {
const { client: optionsClient, stateLoad } = options ?? {}
const { client: optionsClient, shard, stateLoad } = options ?? {}

// Important! Register process events before doing anything else
// This ensures the "ready" signal is sent to the parent process
Expand All @@ -51,15 +54,41 @@ async function start(options?: StartOptions) {
level: config?.logger?.level
}).debug('Starting Robo...')

// Wanna shard? Delegate to the shard manager and await recursive call
if (shard && config.experimental?.disableBot !== true) {
discordLogger.debug('Sharding is enabled. Delegating start to shard manager...')
const { ShardingManager } = await import('discord.js')
const shardPath = typeof shard === 'string' ? shard : path.join(PackageDir, 'dist', 'cli', 'shard.js')
const options = typeof config.experimental?.shard === 'object' ? config.experimental.shard : {}
const manager = new ShardingManager(shardPath, { ...options, token: env.discord.token })

manager.on('shardCreate', (shard) => discordLogger.debug(`Launched shard`, shard.id))
const result = await manager.spawn()
discordLogger.debug('Spawned', result.size, 'shard(s)')
return
}

// Get ready for persistent data requests
await prepareFlashcore()

// Wait for states to be loaded
if (stateLoad) {
// Await external state promise if provided
logger.debug('Waiting for state...')
await stateLoad
} else {
// Load state directly otherwise
const stateStart = Date.now()
const state = await Flashcore.get<Record<string, unknown>>(FLASHCORE_KEYS.state)

if (state) {
loadState(state)
}
logger.debug(`State loaded in ${Date.now() - stateStart}ms`)
}

// Load plugin options and start up Flashcore
// Load plugin options
const plugins = loadPluginData()
await prepareFlashcore()

// Create the new client instance (unless disabled)
if (config.experimental?.disableBot !== true) {
Expand Down
3 changes: 2 additions & 1 deletion packages/robo/src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { LogDrain, LogLevel } from '../core/logger.js'
import type { ClientOptions, PermissionsString } from 'discord.js'
import type { ClientOptions, PermissionsString, ShardingManagerOptions } from 'discord.js'
import type { Plugin, SageOptions } from './index.js'

export interface Config {
Expand All @@ -13,6 +13,7 @@ export interface Config {
buildDirectory?: string
disableBot?: boolean
incrementalBuilds?: boolean
shard?: boolean | ShardingManagerOptions
userInstall?: boolean
}
flashcore?: {
Expand Down
Loading