Skip to content

Commit

Permalink
added ability to configure passthrough output for tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed May 11, 2024
1 parent 1a75ac2 commit 530a785
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/scripts/export/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ program.option(
'--no-ws',
"don't connect to websocket"
)
program.option(
'-o, --output <path>',
'optionally write streamed state to another file, acting as a passthrough'
)
program.parse()
const { config: _config, ws: webSocketEnabled } = program.opts()
const { config: _config, output, ws: webSocketEnabled } = program.opts()

// Load config with config option.
const config = loadConfig(_config)
Expand All @@ -59,14 +63,14 @@ if (config.sentryDsn) {
const traceFile = path.join(config.home, 'trace.pipe')

const main = async () => {
// Ensure trace and update files exist.
// Ensure trace file exists.
if (!fs.existsSync(traceFile)) {
throw new Error(
`Trace file not found: ${traceFile}. Create it with "mkfifo ${traceFile}".`
)
}

// Verify trace and update files are FIFOs.
// Verify trace file is FIFOs.
const stat = fs.statSync(traceFile)
if (!stat.isFIFO()) {
throw new Error(`Trace file is not a FIFO: ${traceFile}.`)
Expand All @@ -84,9 +88,16 @@ const trace = async () => {
// Initialize state.
await State.createSingletonIfMissing()

// Setup meilisearch.
// Set up meilisearch.
await setupMeilisearch()

// Create write stream for output if configured.
const outputStream = output
? fs.createWriteStream(output, {
flags: 'a',
})
: null

const exportQueue = getBullQueue<{ data: ExportQueueData[] }>(
QueueName.Export
)
Expand Down Expand Up @@ -416,6 +427,10 @@ const trace = async () => {
file: traceFile,
onData: (data) => {
const tracedEvent = data as TracedEvent

// Output if configured.
outputStream?.write(JSON.stringify(tracedEvent) + '\n')

// Ensure this is a traced write or delete event.
if (
!objectMatchesStructure(tracedEvent, {
Expand Down

0 comments on commit 530a785

Please sign in to comment.