1
1
import { ClusterClient , ShardClient } from 'detritus-client' ;
2
+ import { ClientEvents } from 'detritus-client/lib/constants' ;
2
3
import { EventSubscription , Timers } from 'detritus-utils' ;
3
4
4
5
import { Listener } from './listener' ;
@@ -9,109 +10,88 @@ import { RedisSpewer } from '../redis';
9
10
import { RedisPayloads } from '../types' ;
10
11
11
12
13
+ const MAX_TIME_BETWEEN = 1500 ;
14
+
12
15
class ClusterInfoInterval extends Listener {
13
- interval = new Timers . Interval ( ) ;
14
- intervalTime = 15000 ;
16
+ lastUpload = 0 ;
17
+
18
+ async maybeUploadInformation ( cluster : ClusterClient , force : boolean = false ) : Promise < void > {
19
+ const lastUpload = this . lastUpload ;
20
+ if ( ! force && Date . now ( ) - lastUpload <= MAX_TIME_BETWEEN ) {
21
+ return ;
22
+ }
23
+
24
+ const shard = cluster . shards . first ( ) ;
25
+ if ( shard ) {
26
+ try {
27
+ const usage = process . memoryUsage ( ) ;
28
+
29
+ this . lastUpload = Date . now ( ) ;
30
+ await putInfoDiscord ( { client : shard } , {
31
+ clusterId : cluster . clusterId ,
32
+ ramUsage : usage . heapUsed + usage . external + Math . max ( 0 , usage . rss - usage . heapTotal ) ,
33
+ shardCount : cluster . shardCount ,
34
+ shardsPerCluster : ( cluster . manager ) ? cluster . manager . clusterShardsPer : 1 ,
35
+ shards : cluster . shards . map ( ( shard ) => {
36
+ return {
37
+ shardId : shard . shardId ,
38
+ status : shard . gateway . state ,
39
+
40
+ applications : shard . applications . length ,
41
+ channels : shard . channels . length ,
42
+ channelThreads : shard . channels . filter ( ( channel ) => channel . isGuildThread ) . length ,
43
+ emojis : shard . emojis . length ,
44
+ events : shard . gateway . sequence ,
45
+ guilds : shard . guilds . length ,
46
+ members : shard . members . length ,
47
+ memberCount : shard . guilds . reduce ( ( x , guild ) => x + guild . memberCount , 0 ) ,
48
+ messages : shard . messages . length ,
49
+ permissionOverwrites : shard . channels . reduce ( ( x , channel ) => x + channel . permissionOverwrites . length , 0 ) ,
50
+ presences : shard . presences . length ,
51
+ presenceActivities : shard . presences . reduce ( ( x , presence ) => x + presence . activities . length , 0 ) ,
52
+ roles : shard . roles . length ,
53
+ stageInstances : shard . stageInstances . length ,
54
+ typings : shard . typings . length ,
55
+ users : shard . users . length ,
56
+ voiceStates : shard . voiceStates . length ,
57
+ } ;
58
+ } ) ,
59
+ } ) ;
60
+ } catch ( error ) {
61
+ this . lastUpload = lastUpload ;
62
+ }
63
+ }
64
+ }
15
65
16
66
create ( cluster : ClusterClient , redis : RedisSpewer ) {
17
67
const subscriptions : Array < EventSubscription > = [ ] ;
18
68
19
69
{
20
70
const subscription = redis . subscribe ( RedisChannels . INFO_DISCORD_REQUEST , async ( payload : RedisPayloads . InfoDiscordRequest ) => {
21
- const shard = cluster . shards . first ( ) ;
22
- if ( shard ) {
23
- try {
24
- const usage = process . memoryUsage ( ) ;
25
-
26
- await putInfoDiscord ( { client : shard } , {
27
- clusterId : cluster . clusterId ,
28
- ramUsage : usage . heapUsed + usage . external + Math . max ( 0 , usage . rss - usage . heapTotal ) ,
29
- shardCount : cluster . shardCount ,
30
- shardsPerCluster : ( cluster . manager ) ? cluster . manager . clusterShardsPer : 1 ,
31
- shards : cluster . shards . map ( ( shard ) => {
32
- return {
33
- shardId : shard . shardId ,
34
- status : shard . gateway . state ,
35
-
36
- applications : shard . applications . length ,
37
- channels : shard . channels . length ,
38
- channelThreads : shard . channels . filter ( ( channel ) => channel . isGuildThread ) . length ,
39
- emojis : shard . emojis . length ,
40
- events : shard . gateway . sequence ,
41
- guilds : shard . guilds . length ,
42
- members : shard . members . length ,
43
- memberCount : shard . guilds . reduce ( ( x , guild ) => x + guild . memberCount , 0 ) ,
44
- messages : shard . messages . length ,
45
- permissionOverwrites : shard . channels . reduce ( ( x , channel ) => x + channel . permissionOverwrites . length , 0 ) ,
46
- presences : shard . presences . length ,
47
- presenceActivities : shard . presences . reduce ( ( x , presence ) => x + presence . activities . length , 0 ) ,
48
- roles : shard . roles . length ,
49
- stageInstances : shard . stageInstances . length ,
50
- typings : shard . typings . length ,
51
- users : shard . users . length ,
52
- voiceStates : shard . voiceStates . length ,
53
- } ;
54
- } ) ,
55
- } ) ;
56
- } catch ( error ) {
57
-
58
- }
59
- }
71
+ this . maybeUploadInformation ( cluster , true ) ;
60
72
} ) ;
61
73
subscriptions . push ( subscription ) ;
62
74
}
63
75
64
- /*
65
- this.interval.start(this.intervalTime, async () => {
66
- const shard = cluster.shards.first();
67
- if (shard) {
68
- try {
69
- const usage = process.memoryUsage();
70
-
71
- await putInfoDiscord({client: shard}, {
72
- clusterId: cluster.clusterId,
73
- ramUsage: usage.heapUsed + usage.external + Math.max(0, usage.rss - usage.heapTotal),
74
- shardCount: cluster.shardCount,
75
- shardsPerCluster: 8,
76
- shards: cluster.shards.map((shard) => {
77
- return {
78
- shardId: shard.shardId,
79
- status: shard.gateway.state,
80
-
81
- applications: shard.applications.length,
82
- channels: shard.channels.length,
83
- channelThreads: shard.channels.filter((channel) => channel.isGuildThread).length,
84
- emojis: shard.emojis.length,
85
- events: shard.gateway.sequence,
86
- guilds: shard.guilds.length,
87
- members: shard.members.length,
88
- memberCount: shard.guilds.reduce((x, guild) => x + guild.memberCount, 0),
89
- messages: shard.messages.length,
90
- permissionOverwrites: shard.channels.reduce((x, channel) => x + channel.permissionOverwrites.length, 0),
91
- presences: shard.presences.length,
92
- presenceActivities: shard.presences.reduce((x, presence) => x + presence.activities.length, 0),
93
- roles: shard.roles.length,
94
- stageInstances: shard.stageInstances.length,
95
- typings: shard.typings.length,
96
- users: shard.users.length,
97
- voiceStates: shard.voiceStates.length,
98
- };
99
- }),
100
- });
101
- } catch(error) {
102
-
103
- }
76
+ if ( cluster . shards . length ) {
77
+ for ( let [ shardId , shard ] of cluster . shards ) {
78
+ const gatewaySubscription = shard . gateway . subscribe ( 'state' , ( { state} ) => {
79
+ this . maybeUploadInformation ( cluster ) ;
80
+ } ) ;
81
+ subscriptions . push ( gatewaySubscription ) ;
104
82
}
105
- });
106
- */
83
+ } else {
84
+ const subscription = cluster . subscribe ( ClientEvents . SHARD , ( { shard} ) => {
85
+ const gatewaySubscription = shard . gateway . subscribe ( 'state' , ( { state} ) => {
86
+ this . maybeUploadInformation ( cluster ) ;
87
+ } ) ;
88
+ subscriptions . push ( gatewaySubscription ) ;
89
+ } ) ;
90
+ subscriptions . push ( subscription ) ;
91
+ }
107
92
108
93
return subscriptions ;
109
94
}
110
-
111
- stop ( cluster : ClusterClient | ShardClient ) {
112
- super . stop ( cluster ) ;
113
- this . interval . stop ( ) ;
114
- }
115
95
}
116
96
117
97
export default new ClusterInfoInterval ( ) ;
0 commit comments