Skip to content

Commit

Permalink
Collect and store module metrics. Closes #15 #16
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Feb 28, 2023
1 parent 08705f0 commit b410899
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 78 deletions.
67 changes: 48 additions & 19 deletions lib/saturn-node.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { fileURLToPath } from 'node:url'
import { createWriteStream } from 'node:fs'
import fs from 'node:fs/promises'
import timers from 'node:timers/promises'
import { execa } from 'execa'
import { join, dirname } from 'node:path'
import { arch, platform } from 'node:os'
import { fetch } from 'undici'

const modules = join(dirname(fileURLToPath(import.meta.url)), '..', 'modules')
const archOverwritten = platform() === 'darwin' ? 'x64' : arch()
Expand All @@ -15,23 +17,22 @@ function forwardChunkFromSaturn (chunk, log) {
}
}

function formatLog (text) {
return text
.trimEnd()
.split(/\n/g)
.map(line => `[${new Date().toLocaleTimeString()}] ${line}`)
.join('\n') + '\n'
}

async function start ({ ROOT, FIL_WALLET_ADDRESS }) {
const logStream = createWriteStream(
join(ROOT, 'logs', 'modules', 'saturn-L2-node.log'),
{ flags: 'a' }
)

function appendToChildLog (text) {
logStream.write(text
.trimEnd()
.split(/\n/g)
.map(line => `[${new Date().toLocaleTimeString()}] ${line}`)
.join('\n')
)
}

console.log('Starting Saturn node...')
appendToChildLog('Starting Saturn node')
logStream.write(formatLog('Starting Saturn node'))

const childProcess = execa(
join(
Expand All @@ -46,19 +47,17 @@ async function start ({ ROOT, FIL_WALLET_ADDRESS }) {
}
)

let apiUrl

const readyPromise = new Promise((resolve, reject) => {
childProcess.stdout.setEncoding('utf-8')
childProcess.stdout.on('data', data => {
forwardChunkFromSaturn(data, console.log)
appendToChildLog(data)
logStream.write(formatLog(data))
})

childProcess.stderr.setEncoding('utf-8')
childProcess.stderr.on('data', data => {
forwardChunkFromSaturn(data, console.error)
appendToChildLog(data)
logStream.write(formatLog(data))
})

let output = ''
Expand All @@ -68,11 +67,17 @@ async function start ({ ROOT, FIL_WALLET_ADDRESS }) {

const apiMatch = output.match(/^API: (http.*)$/m)
if (apiMatch) {
apiUrl = apiMatch[1]
const apiUrl = apiMatch[1]

appendToChildLog('Saturn node is up and ready')
logStream.write(formatLog('Saturn node is up and ready'))
console.log('Saturn node is up and ready (API URL: %s)', apiUrl)
childProcess.stdout.off('data', readyHandler)
setInterval(() => {
pollStats({ ROOT, apiUrl })
.catch(err => {
console.warn('Cannot fetch Saturn module stats.', err)
})
}, 1000)
resolve()
}
}
Expand All @@ -90,7 +95,7 @@ async function start ({ ROOT, FIL_WALLET_ADDRESS }) {
const reason = signal ? `via signal ${signal}` : `with code: ${code}`
const msg = `Saturn node exited ${reason}`
console.log(msg)
appendToChildLog(msg)
logStream.write(formatLog(msg))
})

try {
Expand All @@ -101,11 +106,35 @@ async function start ({ ROOT, FIL_WALLET_ADDRESS }) {
} catch (err) {
const errorMsg = err instanceof Error ? err.message : '' + err
const message = `Cannot start Saturn node: ${errorMsg}`
appendToChildLog(message)
logStream.write(formatLog(message))
console.error('Cannot start Saturn node:', err)
}
}

async function pollStats ({ ROOT, apiUrl }) {
const res = await fetch(apiUrl.replace('localhost', '127.0.0.1') + 'stats')
if (!res.ok) {
const msg = 'Cannot fetch Saturn node stats: ' +
`${res.status}\n${await res.text().catch(noop)}`
throw new Error(msg)
}

const stats = await res.json()

const totalJobsCompleted = stats?.NSuccessfulRetrievals
if (typeof totalJobsCompleted !== 'number') {
const msg = 'Unexpected stats response - NSuccessfulRetrievals is not a ' +
'number. Stats: ' + JSON.stringify(stats)
throw new Error(msg)
}
await fs.appendFile(
join(ROOT, 'logs', 'metrics.log'),
formatLog(`${JSON.stringify({ totalJobsCompleted })}\n`)
)
}

return apiUrl
function noop () {
// no-op
}

export { start }
Loading

0 comments on commit b410899

Please sign in to comment.