-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathzinnia.js
394 lines (366 loc) · 10.7 KB
/
zinnia.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import { execa } from 'execa'
import * as Sentry from '@sentry/node'
import { installBinaryModule, updateSourceFiles, getBinaryModuleExecutable } from './modules.js'
import os from 'node:os'
import pRetry from 'p-retry'
import timers from 'node:timers/promises'
import { join } from 'node:path'
const ZINNIA_DIST_TAG = 'v0.20.2'
const ZINNIA_MODULES = [
{
module: 'spark',
ipnsKey: 'k51qzi5uqu5dlej5gtgal40sjbowuau5itwkr6mgyuxdsuhagjxtsfqjd6ym3g'
}
]
const {
TARGET_ARCH = os.arch(),
MODULE_FILTER = ''
} = process.env
export const install = () => installBinaryModule({
module: 'zinnia',
repo: 'filecoin-station/zinnia',
distTag: ZINNIA_DIST_TAG,
executable: 'zinniad',
arch: TARGET_ARCH,
targets: [
{ platform: 'darwin', arch: 'arm64', asset: 'zinniad-macos-arm64.zip' },
{ platform: 'darwin', arch: 'x64', asset: 'zinniad-macos-x64.zip' },
{ platform: 'linux', arch: 'arm64', asset: 'zinniad-linux-arm64.tar.gz' },
{ platform: 'linux', arch: 'x64', asset: 'zinniad-linux-x64.tar.gz' },
{ platform: 'win32', arch: 'x64', asset: 'zinniad-windows-x64.zip' }
]
})
let lastErrorReportedAt = 0
const maybeReportErrorToSentry = (/** @type {unknown} */ err) => {
const now = Date.now()
if (now - lastErrorReportedAt < 4 /* HOURS */ * 3600_000) return
lastErrorReportedAt = now
/** @type {Parameters<Sentry.captureException>[1]} */
const hint = { extra: {} }
if (typeof err === 'object') {
if ('reportToSentry' in err && err.reportToSentry === false) {
return
}
Object.assign(err, { reportToSentry: false })
if ('details' in err && typeof err.details === 'string') {
// Quoting from https://develop.sentry.dev/sdk/data-handling/
// > Messages are limited to 8192 characters.
// > Individual extra data items are limited to 16kB. Total extra data is limited to 256kb.
// Let's store the additional details (e.g. stdout && stderr) in an extra field
const tail = err.details.split(/\n/g).slice(-50).join('\n')
hint.extra.details = tail
}
if ('moduleName' in err && typeof err.moduleName === 'string') {
hint.extra.moduleName = err.moduleName
}
}
console.error('Reporting the problem to Sentry for inspection by the Station team.')
Sentry.captureException(err, hint)
}
const matchesModuleFilter = module =>
MODULE_FILTER === '' || module === MODULE_FILTER
const capitalize = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`
const updateAllSourceFiles = async ({
moduleVersionsDir,
moduleSourcesDir,
signal
}) => {
const modules = await Promise.all(
Object
.values(ZINNIA_MODULES)
.filter(({ module }) => matchesModuleFilter(module))
.map(({ module, ipnsKey }) =>
pRetry(
attemptNumber => updateSourceFiles({
module,
ipnsKey,
moduleVersionsDir,
moduleSourcesDir,
noCache: attemptNumber > 1
}),
{
signal,
retries: 10,
onFailedAttempt: err => {
console.error(err)
const msg = `Failed to download ${module} source. Retrying...`
console.error(msg)
if (String(err).includes('You are being rate limited')) {
const delaySeconds = 30 + (Math.random() * 60)
// Don't DDOS the w3name services
console.error(
`Rate limited. Waiting ${delaySeconds} seconds...`
)
return timers.setTimeout(delaySeconds * 1000)
}
}
}
)
)
)
const hasUpdated = modules.find(updated => updated === true)
return hasUpdated
}
const runUpdateSourceFilesLoop = async ({
controller,
signal,
onActivity,
moduleVersionsDir,
moduleSourcesDir
}) => {
while (true) {
if (signal.aborted) {
return
}
const delay = 10 * 60 * 1000 // 10 minutes
const jitter = Math.random() * 20_000 - 10_000 // +- 10 seconds
try {
await timers.setTimeout(delay + jitter, null, { signal })
} catch (err) {
if (err.name === 'AbortError') return
throw err
}
try {
const shouldRestart = await updateAllSourceFiles({
moduleVersionsDir,
moduleSourcesDir,
signal
})
if (shouldRestart) {
onActivity({
type: 'info',
message: 'Updated Zinnia module source code, restarting...'
})
controller.abort()
return
}
} catch (err) {
onActivity({
type: 'error',
message: 'Failed to update Zinnia module source code'
})
console.error(err)
maybeReportErrorToSentry(err)
}
}
}
const catchChildProcessExit = async ({
childProcesses,
controller,
onActivity
}) => {
try {
const tasks = childProcesses.map(p => (async () => {
try {
await p
onActivity({ type: 'info', message: `${capitalize(module)} exited` })
} catch (err) {
// When the child process crash, attach the module name & the exit reason to the error object
const exitReason = p.exitCode
? `with exit code ${p.exitCode}`
: p.signalCode ? `via signal ${p.signalCode}` : undefined
throw Object.assign(err, { moduleName: p.moduleName, exitReason })
}
})())
await Promise.race(tasks)
} catch (err) {
if (err.name === 'AbortError') {
Object.assign(err, { reportToSentry: false })
} else {
const moduleName = capitalize(err.moduleName ?? 'Zinnia')
const exitReason = err.exitReason ?? 'for unknown reason'
const message = `${moduleName} crashed ${exitReason}`
onActivity({ type: 'error', message })
const moduleErr = new Error(message, { cause: err })
// Store the full error message including stdout & stder in the top-level `details` property
Object.assign(moduleErr, { details: err.message })
// Apply a custom rule to force Sentry to group all issues with the same module & exit code
// See https://docs.sentry.io/platforms/node/usage/sdk-fingerprinting/#basic-example
Sentry.withScope(scope => {
scope.setFingerprint([message])
maybeReportErrorToSentry(moduleErr)
})
}
throw err
} finally {
controller.abort()
}
}
export async function run ({
provider,
STATION_ID,
FIL_WALLET_ADDRESS,
ethAddress,
STATE_ROOT,
CACHE_ROOT,
moduleVersionsDir,
moduleSourcesDir,
onActivity,
onMetrics,
isUpdated = false
}) {
const zinniadExe = getBinaryModuleExecutable({ module: 'zinnia', executable: 'zinniad' })
if (!isUpdated) {
try {
onActivity({
type: 'info',
message: 'Updating source code for Zinnia modules...'
})
await updateAllSourceFiles({
moduleVersionsDir,
moduleSourcesDir,
signal: null
})
onActivity({
type: 'info',
message: 'Zinnia module source code up to date'
})
} catch (err) {
onActivity({
type: 'error',
message: 'Failed to download latest Zinnia module source code'
})
throw err
}
}
const controller = new AbortController()
const { signal } = controller
const childProcesses = []
for (const { module } of ZINNIA_MODULES) {
if (!matchesModuleFilter(module)) continue
// all paths are relative to `moduleBinaries`
const childProcess = execa(
zinniadExe,
[join(module, 'main.js')],
{
cwd: moduleSourcesDir,
env: {
STATION_ID,
FIL_WALLET_ADDRESS,
STATE_ROOT,
CACHE_ROOT
},
cancelSignal: signal
}
)
childProcesses.push(Object.assign(childProcess, { moduleName: module }))
let timeoutId
const resetTimeout = () => {
clearTimeout(timeoutId)
timeoutId = setTimeout(() => {
onActivity({
type: 'error',
message: `${capitalize(module)} has been inactive for 5 minutes, restarting...`
})
controller.abort()
}, 5 * 60 * 1000)
}
resetTimeout()
signal.addEventListener('abort', () => clearTimeout(timeoutId))
childProcess.stdout.setEncoding('utf-8')
childProcess.stdout.on('data', data => {
resetTimeout()
handleEvents({
module,
onActivity,
onMetrics,
text: data
}).catch(err => {
console.error(err)
Sentry.captureException(err)
})
})
childProcess.stderr.setEncoding('utf-8')
childProcess.stderr.on('data', data => {
resetTimeout()
process.stderr.write(data)
})
}
try {
await Promise.all([
runUpdateSourceFilesLoop({
controller,
signal,
onActivity,
moduleVersionsDir,
moduleSourcesDir
}),
catchChildProcessExit({ childProcesses, onActivity, controller })
])
console.error('Zinnia main loop ended')
} catch (err) {
console.error('Zinnia main loop errored', err)
maybeReportErrorToSentry(err)
} finally {
controller.abort()
}
// This infinite recursion has no risk of exceeding the maximum call stack
// size, as awaiting promises unwinds the stack
return run({
provider,
STATION_ID,
FIL_WALLET_ADDRESS,
ethAddress,
STATE_ROOT,
CACHE_ROOT,
moduleVersionsDir,
moduleSourcesDir,
onActivity,
onMetrics,
isUpdated: true
})
}
const jobsCompleted = {}
async function handleEvents ({
module,
onActivity,
onMetrics,
text
}) {
for (const line of text.trimEnd().split(/\n/g)) {
let event
try {
event = JSON.parse(line)
} catch (err) {
console.error('Ignoring malformed Zinnia event:', line)
}
try {
switch (event.type) {
case 'activity:started':
onActivity({
type: 'info',
message: `${capitalize(module)} started`,
source: module
})
break
case 'activity:info':
onActivity({
type: 'info',
message:
event.message.replace(/Module Runtime/, capitalize(module)),
source: event.module
})
break
case 'activity:error':
onActivity({
type: 'error',
message:
event.message.replace(/Module Runtime/, capitalize(module)),
source: event.module
})
break
case 'jobs-completed': {
jobsCompleted[module] = event.total
const totalJobsCompleted = Object.values(jobsCompleted).reduce((a, b) => a + b, 0)
onMetrics({ totalJobsCompleted })
break
}
default:
console.error('Ignoring Zinnia event of unknown type:', event)
}
} catch (err) {
console.error('Cannot handle Zinnia event: %s', line)
console.error(err)
}
}
}