Skip to content

Commit

Permalink
fix: cluster mode duplicate forks
Browse files Browse the repository at this point in the history
remove: youtube download feature (#160)
  • Loading branch information
RainyXeon committed Sep 6, 2024
1 parent 291cfc1 commit 48ac5e1
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 48 deletions.
16 changes: 9 additions & 7 deletions src/cluster/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import readdirRecursive from 'recursive-readdir'
import { resolve } from 'path'
import { join, dirname } from 'path'
import { fileURLToPath, pathToFileURL } from 'url'
import { ClusterCommand } from '../@types/Cluster.js'
import { ClusterCommand, WorkerResponse } from '../@types/Cluster.js'
const __dirname = dirname(fileURLToPath(import.meta.url))
config()

Expand All @@ -23,23 +23,25 @@ export class ClusterManager {
public readonly commands: Collection<ClusterCommand> = new Collection<ClusterCommand>()
public readonly clusterShardList: Record<string, number[]> = {}
public readonly totalShards: number = 0
public customData?: {
id: number
shard: number[]
shardCount: number
}
constructor(public readonly options: ClusterManagerOptions) {
this.totalShards = this.options.totalClusters * this.options.shardsPerClusters
const shardArrayID = this.arrayRange(0, this.totalShards - 1, 1)
this.arrayChunk<number>(shardArrayID, this.options.shardsPerClusters).map((value, index) => {
this.clusterShardList[String(index + 1)] = value
})
console.log(this.options.totalClusters)
}

public async start() {
if (cluster.isPrimary) {
this.log('INFO', `Primary process ${process.pid} is running`)

await this.commandLoader()
for (let i = 0; i < this.options.totalClusters; i++) {
const worker = cluster.fork()
this.workerPID.set(String(worker.id), worker)
}

cluster.on('exit', (worker) => {
this.log('WARN', `worker ${worker.process.pid} / ${worker.id} died x.x`)
Expand All @@ -63,7 +65,7 @@ export class ClusterManager {
this.log('WARN', `worker ${worker.process.pid} / ${worker.id} died`)
})
} else {
bootBot(this)
await bootBot(this)

this.log('INFO', `Worker ${process.pid} / ${cluster.worker.id} started`)
}
Expand Down Expand Up @@ -91,7 +93,7 @@ export class ClusterManager {
public async sendMaster(
cmd: string,
args: Record<string, unknown> = {}
): Promise<Record<string, any> | null> {
): Promise<WorkerResponse> {
return new Promise((resolve, reject) => {
const fullData = { cmd, args }
cluster.worker.on('message', (message) => {
Expand Down
13 changes: 6 additions & 7 deletions src/events/track/trackStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ export default class {
const getData = await autoreconnect.get(player.guildId)
if (!getData) await autoreconnect.playerBuild(player.guildId)
else {
await client.db.autoreconnect.set(`${player.guildId}.current`, player.queue.current?.uri)
player.queue.current
? await client.db.autoreconnect.set(
`${player.guildId}.current`,
player.queue.current?.uri
)
: true
await client.db.autoreconnect.set(`${player.guildId}.config.loop`, player.loop)

function queueUri() {
Expand Down Expand Up @@ -102,17 +107,11 @@ export default class {
value: `${song!.requester}`,
inline: true,
},
{
name: `${client.i18n.get(language, 'event.player', 'download_title')}`,
value: `**[${song!.title} - 000tube.com](https://www.000tube.com/watch?v=${song?.identifier})**`,
inline: false,
},
])
.setColor(client.color)
.setThumbnail(
track.artworkUrl ?? `https://img.youtube.com/vi/${track.identifier}/hqdefault.jpg`
)
.setTimestamp()

const playing_channel = (await client.channels
.fetch(player.textId)
Expand Down
34 changes: 18 additions & 16 deletions src/events/websocket/trackStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@ import { RainlinkPlayer } from 'rainlink'
export default class {
async execute(client: Manager, player: RainlinkPlayer) {
const song = player.queue.current
const requesterQueue = song!.requester as User
const requesterQueue = song && song.requester ? (song!.requester as User) : null

const currentData = {
title: song!.title,
uri: song!.uri,
length: song!.duration,
thumbnail: song!.artworkUrl,
author: song!.author,
requester: requesterQueue
? {
id: requesterQueue.id,
username: requesterQueue.username,
globalName: requesterQueue.globalName,
defaultAvatarURL: requesterQueue.defaultAvatarURL ?? null,
}
: null,
}
const currentData = song
? {
title: song!.title,
uri: song!.uri,
length: song!.duration,
thumbnail: song!.artworkUrl,
author: song!.author,
requester: requesterQueue
? {
id: requesterQueue.id,
username: requesterQueue.username,
globalName: requesterQueue.globalName,
defaultAvatarURL: requesterQueue.defaultAvatarURL ?? null,
}
: null,
}
: null

client.wsl.get(player.guildId)?.send({
op: 'trackStart',
Expand Down
4 changes: 2 additions & 2 deletions src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function getShard(clusterManager: ClusterManager) {
}

export class Manager extends Client {
public cluster: { id: number | 0, data: Cluster | null }
public cluster: { id: number | 0; data: Cluster | null }
public metadata: Metadata
public logger: LoggerService
public db!: DatabaseTable
Expand Down Expand Up @@ -113,7 +113,7 @@ export class Manager extends Client {
const __dirname = dirname(fileURLToPath(import.meta.url))
this.cluster = {
data: clusterManager ? cluster : null,
id: clusterManager ? cluster.worker.id : 0
id: clusterManager ? cluster.worker.id : 0,
}
this.logger = new LoggerService(this, this.cluster.id)
this.metadata = new ManifestService().data.metadata.bot
Expand Down
6 changes: 3 additions & 3 deletions src/structures/ExtendedPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RainlinkEvents, RainlinkLoopMode, RainlinkPlayer } from "rainlink";
import { RainlinkEvents, RainlinkLoopMode, RainlinkPlayer } from 'rainlink'

export class ExtendedPlayer extends RainlinkPlayer {
public clear(emitEmpty: boolean): void {
Expand Down Expand Up @@ -35,7 +35,7 @@ export class ExtendedPlayer extends RainlinkPlayer {
},
})
this.manager.emit(RainlinkEvents.TrackEnd, this, this.queue.current)
this.manager.emit("playerStop" as RainlinkEvents.PlayerDestroy, this)
this.manager.emit('playerStop' as RainlinkEvents.PlayerDestroy, this)
return this
}
}
}
9 changes: 2 additions & 7 deletions src/structures/Rainlink.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { Manager } from '../manager.js'
import {
Library,
Rainlink,
RainlinkAdditionalOptions,
RainlinkPlugin,
} from 'rainlink'
import { Library, Rainlink, RainlinkAdditionalOptions, RainlinkPlugin } from 'rainlink'
import { SpotifyPlugin } from 'rainlink-spotify'
import { NicoPlugin } from 'rainlink-nico'
import { ApplePlugin } from 'rainlink-apple'
Expand Down Expand Up @@ -37,7 +32,7 @@ export class RainlinkInit {
retryTimeout: 3000,
defaultSearchEngine: 'youtube',
structures: {
player: ExtendedPlayer
player: ExtendedPlayer,
},
searchFallback: {
enable: true,
Expand Down
8 changes: 2 additions & 6 deletions src/structures/YTConverterPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { RainlinkTrack } from 'rainlink'
import { RainlinkPluginType } from 'rainlink'
import {
RainlinkSearchOptions,
RainlinkSearchResult,
RainlinkSearchResultType,
} from 'rainlink'
import { RainlinkSearchOptions, RainlinkSearchResult, RainlinkSearchResultType } from 'rainlink'
import { Rainlink } from 'rainlink'
import { RainlinkPlugin as Plugin } from 'rainlink'

Expand Down Expand Up @@ -114,4 +110,4 @@ export class RainlinkPlugin extends Plugin {
type: type ?? RainlinkSearchResultType.SEARCH,
}
}
}
}

0 comments on commit 48ac5e1

Please sign in to comment.