|
| 1 | +import timers from 'node:timers/promises' |
| 2 | +import { execa } from 'execa' |
| 3 | +import * as Sentry from '@sentry/node' |
| 4 | +import { installBinaryModule, downloadSourceFiles, getBinaryModuleExecutable } from './modules.js' |
| 5 | +import { moduleBinaries } from './paths.js' |
| 6 | + |
| 7 | +const ZINNIA_DIST_TAG = 'v0.8.0' |
| 8 | +const ZINNIA_MODULES = [ |
| 9 | + { |
| 10 | + module: 'peer-checker', |
| 11 | + repo: 'filecoin-station/mod-peer-checker', |
| 12 | + distTag: 'v1.0.0' |
| 13 | + } |
| 14 | +] |
| 15 | + |
| 16 | +export async function install () { |
| 17 | + await Promise.all([ |
| 18 | + installBinaryModule({ |
| 19 | + module: 'zinnia', |
| 20 | + repo: 'filecoin-station/zinnia', |
| 21 | + distTag: ZINNIA_DIST_TAG, |
| 22 | + executable: 'zinniad', |
| 23 | + targets: [ |
| 24 | + { platform: 'darwin', arch: 'arm64', asset: 'zinniad-macos-arm64.zip' }, |
| 25 | + { platform: 'darwin', arch: 'x64', asset: 'zinniad-macos-x64.zip' }, |
| 26 | + { platform: 'linux', arch: 'arm64', asset: 'zinniad-linux-arm64.tar.gz' }, |
| 27 | + { platform: 'linux', arch: 'x64', asset: 'zinniad-linux-x64.tar.gz' }, |
| 28 | + { platform: 'win32', arch: 'x64', asset: 'zinniad-windows-x64.zip' } |
| 29 | + ] |
| 30 | + }), |
| 31 | + |
| 32 | + ...Object.values(ZINNIA_MODULES).map(downloadSourceFiles) |
| 33 | + ]) |
| 34 | +} |
| 35 | + |
| 36 | +async function start ({ |
| 37 | + FIL_WALLET_ADDRESS, |
| 38 | + STATE_ROOT, |
| 39 | + CACHE_ROOT, |
| 40 | + metricsStream, |
| 41 | + activityStream, |
| 42 | + logStream |
| 43 | +}) { |
| 44 | + logStream.write('Starting Zinnia') |
| 45 | + |
| 46 | + const zinniadExe = getBinaryModuleExecutable({ module: 'zinnia', executable: 'zinniad' }) |
| 47 | + const modules = [ |
| 48 | + // all paths are relative to `moduleBinaries` |
| 49 | + 'peer-checker/peer-checker.js' |
| 50 | + ] |
| 51 | + const childProcess = execa(zinniadExe, modules, { |
| 52 | + cwd: moduleBinaries, |
| 53 | + env: { |
| 54 | + FIL_WALLET_ADDRESS, |
| 55 | + STATE_ROOT, |
| 56 | + CACHE_ROOT |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + const readyPromise = new Promise((resolve, reject) => { |
| 61 | + childProcess.stdout.setEncoding('utf-8') |
| 62 | + childProcess.stdout.on('data', data => { |
| 63 | + logStream.write(data) |
| 64 | + handleEvents({ activityStream, metricsStream }, data) |
| 65 | + }) |
| 66 | + |
| 67 | + childProcess.stderr.setEncoding('utf-8') |
| 68 | + childProcess.stderr.on('data', data => { |
| 69 | + logStream.write(data) |
| 70 | + }) |
| 71 | + |
| 72 | + childProcess.stdout.once('data', _data => { |
| 73 | + // This is based on an implicit assumption that zinniad reports an info activity |
| 74 | + // after it starts |
| 75 | + resolve() |
| 76 | + }) |
| 77 | + childProcess.catch(reject) |
| 78 | + }) |
| 79 | + |
| 80 | + childProcess.on('close', code => { |
| 81 | + logStream.write(`Zinnia closed all stdio with code ${code ?? '<no code>'}`) |
| 82 | + childProcess.stderr.removeAllListeners() |
| 83 | + childProcess.stdout.removeAllListeners() |
| 84 | + Sentry.captureException('Zinnia exited') |
| 85 | + }) |
| 86 | + |
| 87 | + childProcess.on('exit', (code, signal) => { |
| 88 | + const reason = signal ? `via signal ${signal}` : `with code: ${code}` |
| 89 | + const msg = `Zinnia exited ${reason}` |
| 90 | + logStream.write(msg) |
| 91 | + activityStream.write({ type: 'info', message: msg }) |
| 92 | + }) |
| 93 | + |
| 94 | + try { |
| 95 | + await Promise.race([ |
| 96 | + readyPromise, |
| 97 | + timers.setTimeout(500) |
| 98 | + ]) |
| 99 | + } catch (err) { |
| 100 | + const errorMsg = err instanceof Error ? err.message : '' + err |
| 101 | + const message = `Cannot start Zinnia: ${errorMsg}` |
| 102 | + logStream.write(message) |
| 103 | + activityStream.write({ type: 'error', message }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function handleEvents ({ activityStream, metricsStream }, text) { |
| 108 | + text |
| 109 | + .trimEnd() |
| 110 | + .split(/\n/g) |
| 111 | + .forEach(line => { |
| 112 | + try { |
| 113 | + const event = JSON.parse(line) |
| 114 | + switch (event.type) { |
| 115 | + case 'activity:info': |
| 116 | + activityStream.write({ |
| 117 | + type: 'info', |
| 118 | + message: event.message.replace(/Module Runtime/, 'Zinnia'), |
| 119 | + source: event.module ?? 'Zinnia' |
| 120 | + }) |
| 121 | + break |
| 122 | + |
| 123 | + case 'activity:error': |
| 124 | + activityStream.write({ |
| 125 | + type: 'error', |
| 126 | + message: event.message.replace(/Module Runtime/, 'Zinnia'), |
| 127 | + source: event.module ?? 'Zinnia' |
| 128 | + }) |
| 129 | + break |
| 130 | + |
| 131 | + case 'jobs-completed': |
| 132 | + metricsStream.write({ totalJobsCompleted: event.total, totalEarnings: '0' }) |
| 133 | + break |
| 134 | + |
| 135 | + default: |
| 136 | + console.error('Ignoring Zinnia event of unknown type:', event) |
| 137 | + } |
| 138 | + } catch (err) { |
| 139 | + console.error('Ignoring malformed Zinnia event:', line) |
| 140 | + } |
| 141 | + }) |
| 142 | +} |
| 143 | + |
| 144 | +export { start } |
0 commit comments