Skip to content

Commit

Permalink
wip: fix(devnet): pinpointed weird exit bug
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Jan 13, 2024
1 parent b0b750a commit 486fc6f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 30 deletions.
2 changes: 1 addition & 1 deletion devnet/devnet-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export async function startDevnetContainer (
if (e.code !== 304) throw e
}
devnet.log.debug('Waiting for container to say:', bold(devnet.waitString))
await devnet.container.waitLog(devnet.waitString, FILTER, true)
await devnet.container.waitLog(devnet.waitString, (_)=>true, true)
devnet.log.debug('Waiting for', bold(String(devnet.waitMore)), 'seconds...')
await new Promise(resolve=>setTimeout(resolve, devnet.waitMore))
//await Dock.Docker.waitSeconds(devnet.waitMore)
Expand Down
47 changes: 47 additions & 0 deletions oci/oci-impl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Docker from 'dockerode'
import type { OCIContainer } from './oci'
import { OCIError } from './oci-base'
import { bold, Console } from '@fadroma/agent'

export function toDockerodeOptions (container: OCIContainer): Docker.ContainerCreateOptions {

Expand Down Expand Up @@ -54,3 +55,49 @@ export function toDockerodeOptions (container: OCIContainer): Docker.ContainerCr
return Object.assign(config, JSON.parse(JSON.stringify(extra)))

}

/* Is this equivalent to follow() and, if so, which implementation to keep? */
export function waitStream (
stream: { on: Function, off: Function, destroy: Function },
expected: string,
thenDetach: boolean = true,
trail: (data: string) => unknown = ()=>{},
{ log }: Console = new Console()
): Promise<void> {

return new Promise((resolve, reject)=>{

try {
stream.on('error', waitStream_onError)
stream.on('data', waitStream_onData)
} catch (e) {
waitStream_onError(e)
}

function waitStream_onError (error: any) {
reject(error)
stream.off('error', waitStream_onError)
stream.off('data', waitStream_onData)
}

function waitStream_onData (data: any) {
try {
console.log("wat")
const dataStr = String(data).trim()
if (trail) {
trail(dataStr)
}
if (dataStr.indexOf(expected) > -1) {
log(`Found expected message:`, bold(expected))
stream.off('data', waitStream_onData)
if (thenDetach) stream.destroy()
resolve()
}
} catch (e) {
waitStream_onError(e)
}
}

})

}
36 changes: 7 additions & 29 deletions oci/oci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { OCIError, OCIConsole } from './oci-base'
import type { DockerHandle } from './oci-base'
import * as Mock from './oci-mock'
import { toDockerodeOptions } from './oci-impl'
import { toDockerodeOptions, waitStream } from './oci-impl'

/** Defaults to the `DOCKER_HOST` environment variable. */
export const defaultSocketPath = process.env.DOCKER_HOST || '/var/run/docker.sock'
Expand Down Expand Up @@ -401,8 +401,13 @@ export class OCIContainer extends ContractInstance {
this.log.debug(data)
}
}
console.log({stream})
return await waitStream(
stream as any, expected, thenDetach, logFiltered, this.log
stream as any,
expected,
thenDetach,
logFiltered,
this.log
)
}

Expand Down Expand Up @@ -477,33 +482,6 @@ export async function follow (
})
}

/* Is this equivalent to follow() and, if so, which implementation to keep? */
export function waitStream (
stream: { on: Function, off: Function, destroy: Function },
expected: string,
thenDetach: boolean = true,
trail: (data: string) => unknown = ()=>{},
{ log }: Console = console
): Promise<void> {
return new Promise((resolve, reject)=>{
stream.on('error', (error: any) => {
reject(error)
stream.off('data', waitStream_onData)
})
stream.on('data', waitStream_onData)
function waitStream_onData (data: any) {
const dataStr = String(data).trim()
if (trail) trail(dataStr)
if (dataStr.indexOf(expected)>-1) {
log(`Found expected message:`, bold(expected))
stream.off('data', waitStream_onData)
if (thenDetach) stream.destroy()
resolve()
}
}
})
}

/** Based on: Line Transform Stream by Nick Schwarzenberg <nick@bitfasching.de>
* https://github.com/bitfasching/node-line-transform-stream#readme
* Used under MIT license. */
Expand Down

0 comments on commit 486fc6f

Please sign in to comment.