-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
129 lines (115 loc) · 3.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const dotenv = require('dotenv')
dotenv.config()
const { Config, System } = require('simplyapi')
const { Util } = require('simplyapi')
const { initializeCache, determineAction, swapFront, insertFront, removeFront, updateCustomStatus } = require('./dataManager')
const {
isMainThread,
BroadcastChannel,
Worker
} = require('node:worker_threads')
let e
main = () => {
initiateWorkerPool()
}
// Queue
const async = require('async')
const queue = async.queue((task, completed) => {
let error = { status: false, message: '' }
update(task.data)
.catch(err => {
error.status = true
error.message = err
})
completed(error, task)
}, Config.max_workers)
initiateWorkerPool = () => {
// Worker Pool
const bc = new BroadcastChannel('plural')
if (isMainThread) {
openWebSocket()
bc.onmessage = (event) => {
//console.log('::SimplyWS:: received message from worker')
queue.push(event.data, (error, task) => {
// task completed
if (error.status) {
console.log(`An error occurred while processing task ${error.message}`)
}
})
}
for (let n = 0; n < Config.max_workers; n++)
new Worker(__filename)
}
}
// Socket
openWebSocket = () => {
const WebSocketClient = require('./WebsocketClient')
const wss = new WebSocketClient(Config.socket)
let initialPacket = { "op": "authenticate", "token": Config.token }
wss.onOpen = (_) => { wss.send(JSON.stringify(initialPacket)); }
wss.onClose = (e) => { console.log('SimplyWS/onClose :: %s', e); e = '' }
wss.onError = (e) => { console.log('SimplyWS/onError :: %s', e) }
const bc = new BroadcastChannel('plural')
let first_auth = true
wss.onMessage = (raw) => {
e = raw
let data = JSON.parse(e)
if (Object.keys(data).length === 0) return
switch (data.msg) {
case "Successfully authenticated":
if (!process.env.silence_connections || first_auth) console.log('::SimplyWS:: authenticated')
first_auth = false
// cache current front
initializeCache()
break
case "Authentication violation: Token is missing or invalid. Goodbye :)":
console.error('::SimplyWS:: invalid token, exiting..')
process.exit(1)
case "update":
bc.postMessage({data: data})
break
default:
//unrecognizedMessage(data.msg)
break
}
}
}
// Data Processing
update = async (data) => {
let target = data.target
switch (target) {
case 'frontHistory':
//response += 'Front has changed!'
await Util.asyncForEach(data.results, async (o) => {
let system = new System(Config)
let member = await system.getMemberById(o.content.member)
let swap = Config.full_swap
// insert
if (swap) {
swapFront()
}
else if (o.operationType == "insert") {
insertFront(member)
}
else {
// get current fronters and patch the list
let frontData = await system.getFronters()
let action = await determineAction(o, frontData)
// if delete operation, remove the member from the list
switch (action) {
case "remove":
removeFront(member)
break
case "customStatus":
updateCustomStatus(member)
break
}
}
})
break
default:
//unknownTarget(data.target)
break
}
}
main()