2024-07-01 11:35:23 +00:00
|
|
|
import { ChildProcessWithoutNullStreams, spawn } from 'child_process'
|
|
|
|
import kleur from 'kleur'
|
2024-07-02 10:53:27 +00:00
|
|
|
import { lock } from 'proper-lockfile'
|
2024-07-01 11:35:23 +00:00
|
|
|
import stripAnsi from 'strip-ansi'
|
|
|
|
|
2024-07-02 10:53:27 +00:00
|
|
|
const lockfileName = __dirname
|
|
|
|
|
|
|
|
/**
|
|
|
|
* a long time ago, workerd would regularly crash with a segfault but the error is not caught by the
|
|
|
|
* process, so it will just hang. this script wraps the process, tailing the logs and restarting the
|
|
|
|
* process if we encounter the string 'Segmentation fault'. we think this error is probably fixed
|
|
|
|
* now.
|
|
|
|
*
|
|
|
|
* there's a separate issue where spawning multiple workerd instances at once would cause them to
|
|
|
|
* fight and crash. we use a lockfile to start our wrokerd instances one at a time, waiting for the
|
|
|
|
* previous one to be ready before we start the next.
|
|
|
|
*/
|
2024-01-16 14:38:05 +00:00
|
|
|
class MiniflareMonitor {
|
|
|
|
private process: ChildProcessWithoutNullStreams | null = null
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private command: string,
|
|
|
|
private args: string[] = []
|
|
|
|
) {}
|
|
|
|
|
2024-07-02 10:53:27 +00:00
|
|
|
public async start(): Promise<void> {
|
|
|
|
await this.stop() // Ensure any existing process is stopped
|
|
|
|
await this.lock()
|
|
|
|
await console.log(`Starting wrangler...`)
|
2024-01-16 14:38:05 +00:00
|
|
|
this.process = spawn(this.command, this.args, {
|
|
|
|
env: {
|
|
|
|
NODE_ENV: 'development',
|
|
|
|
...process.env,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
this.process.stdout.on('data', (data: Buffer) => {
|
|
|
|
this.handleOutput(stripAnsi(data.toString().replace('\r', '').trim()))
|
|
|
|
})
|
|
|
|
|
|
|
|
this.process.stderr.on('data', (data: Buffer) => {
|
|
|
|
this.handleOutput(stripAnsi(data.toString().replace('\r', '').trim()), true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private handleOutput(output: string, err = false): void {
|
|
|
|
if (!output) return
|
2024-07-02 10:53:27 +00:00
|
|
|
|
|
|
|
if (output.includes('Ready on') && this.isLocked()) {
|
|
|
|
this.release()
|
|
|
|
}
|
|
|
|
|
2024-01-16 14:38:05 +00:00
|
|
|
if (output.includes('Segmentation fault')) {
|
|
|
|
console.error('Segmentation fault detected. Restarting Miniflare...')
|
|
|
|
this.restart()
|
|
|
|
} else if (!err) {
|
2024-07-01 11:35:23 +00:00
|
|
|
console.log(output.replace('[mf:inf]', '')) // or handle the output differently
|
2024-07-12 10:36:31 +00:00
|
|
|
} else {
|
|
|
|
console.error(output.replace('[mf:err]', '')) // or handle the output differently
|
2024-01-16 14:38:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-02 10:53:27 +00:00
|
|
|
private async restart(): Promise<void> {
|
2024-07-01 11:35:23 +00:00
|
|
|
console.log('Restarting wrangler...')
|
2024-07-02 10:53:27 +00:00
|
|
|
await this.stop()
|
2024-01-16 14:38:05 +00:00
|
|
|
setTimeout(() => this.start(), 3000) // Restart after a short delay
|
|
|
|
}
|
|
|
|
|
2024-07-02 10:53:27 +00:00
|
|
|
private async stop(): Promise<void> {
|
|
|
|
if (this.isLocked()) await this.release()
|
2024-01-16 14:38:05 +00:00
|
|
|
if (this.process) {
|
|
|
|
this.process.kill()
|
|
|
|
this.process = null
|
|
|
|
}
|
|
|
|
}
|
2024-07-02 10:53:27 +00:00
|
|
|
|
|
|
|
private _lockPromise?: Promise<() => Promise<void>>
|
|
|
|
private isLocked() {
|
|
|
|
return !!this._lockPromise
|
|
|
|
}
|
|
|
|
private async lock() {
|
|
|
|
if (this.isLocked()) throw new Error('Already locked')
|
|
|
|
console.log('Locking...')
|
|
|
|
this._lockPromise = lock(lockfileName, {
|
|
|
|
retries: {
|
|
|
|
minTimeout: 500,
|
|
|
|
retries: 10,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await this._lockPromise
|
|
|
|
console.log('Locked')
|
|
|
|
}
|
|
|
|
private async release() {
|
|
|
|
if (!this.isLocked()) throw new Error('Not locked')
|
|
|
|
console.log('Releasing...')
|
|
|
|
const lockPromise = this._lockPromise!
|
|
|
|
this._lockPromise = undefined
|
|
|
|
const release = await lockPromise
|
|
|
|
await release()
|
|
|
|
console.log('Released')
|
|
|
|
}
|
2024-01-16 14:38:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-01 11:35:23 +00:00
|
|
|
class SizeReporter {
|
|
|
|
lastLineTime = Date.now()
|
|
|
|
nextTick?: NodeJS.Timeout
|
|
|
|
|
|
|
|
size = 0
|
|
|
|
|
|
|
|
start() {
|
|
|
|
console.log('Spawning size reporter...')
|
|
|
|
const proc = spawn('yarn', [
|
|
|
|
'run',
|
|
|
|
'-T',
|
|
|
|
'esbuild',
|
|
|
|
'src/worker.ts',
|
|
|
|
'--bundle',
|
|
|
|
'--minify',
|
|
|
|
'--watch',
|
|
|
|
'--external:cloudflare:*',
|
|
|
|
'--target=esnext',
|
|
|
|
'--format=esm',
|
|
|
|
])
|
|
|
|
// listen for lines on stdin
|
|
|
|
proc.stdout.on('data', (data) => {
|
|
|
|
this.size += data.length
|
|
|
|
this.lastLineTime = Date.now()
|
|
|
|
clearTimeout(this.nextTick)
|
|
|
|
this.nextTick = setTimeout(() => {
|
|
|
|
console.log(
|
|
|
|
kleur.bold(kleur.yellow('worker')),
|
|
|
|
'is roughly',
|
|
|
|
kleur.bold(kleur.cyan(Math.floor(this.size / 1024) + 'kb')),
|
|
|
|
'(minified)\n'
|
|
|
|
)
|
|
|
|
this.size = 0
|
|
|
|
}, 10)
|
|
|
|
})
|
|
|
|
proc.stderr.on('data', (data) => {
|
|
|
|
console.log(data.toString())
|
|
|
|
})
|
|
|
|
process.on('SIGINT', () => {
|
|
|
|
console.log('Int')
|
|
|
|
proc.kill()
|
|
|
|
})
|
|
|
|
process.on('SIGTERM', () => {
|
|
|
|
console.log('Term')
|
|
|
|
proc.kill()
|
|
|
|
})
|
|
|
|
process.on('exit', () => {
|
|
|
|
console.log('Exiting')
|
|
|
|
proc.kill()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new MiniflareMonitor('wrangler', [
|
2024-01-16 14:38:05 +00:00
|
|
|
'dev',
|
|
|
|
'--env',
|
|
|
|
'dev',
|
|
|
|
'--test-scheduled',
|
|
|
|
'--log-level',
|
|
|
|
'info',
|
|
|
|
'--var',
|
|
|
|
'IS_LOCAL:true',
|
2024-07-02 10:53:27 +00:00
|
|
|
...process.argv.slice(2),
|
2024-07-01 11:35:23 +00:00
|
|
|
]).start()
|
|
|
|
|
|
|
|
new SizeReporter().start()
|