fix: jobs/worker - pass through job error from worker process (#3822)

This commit is contained in:
LK HO 2021-04-12 23:41:36 +08:00 committed by GitHub
parent a20f70ed8d
commit 71aa0c9346
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 5 deletions

View File

@ -55,14 +55,22 @@ class Job {
`--job=${this.name}`, `--job=${this.name}`,
`--data=${data}` `--data=${data}`
], { ], {
cwd: WIKI.ROOTPATH cwd: WIKI.ROOTPATH,
stdio: ['inherit', 'inherit', 'pipe', 'ipc']
}) })
const stderr = [];
proc.stderr.on('data', chunk => stderr.push(chunk))
this.finished = new Promise((resolve, reject) => { this.finished = new Promise((resolve, reject) => {
proc.on('exit', (code, signal) => { proc.on('exit', (code, signal) => {
const data = Buffer.concat(stderr).toString()
if (code === 0) { if (code === 0) {
resolve() resolve(data)
} else { } else {
reject(signal) const err = new Error(`Error when running job ${this.name}: ${data}`)
err.exitSignal = signal
err.exitCode = code
err.stderr = data
reject(err)
} }
proc.kill() proc.kill()
}) })

View File

@ -14,6 +14,11 @@ WIKI.logger = require('./logger').init('JOB')
const args = require('yargs').argv const args = require('yargs').argv
;(async () => { ;(async () => {
await require(`../jobs/${args.job}`)(args.data) try {
process.exit(0) await require(`../jobs/${args.job}`)(args.data)
process.exit(0)
} catch (e) {
await new Promise(resolve => process.stderr.write(e.message, resolve))
process.exit(1)
}
})() })()

View File

@ -74,5 +74,7 @@ module.exports = async (pageId) => {
} catch (err) { } catch (err) {
WIKI.logger.error(`Rebuilding page tree: [ FAILED ]`) WIKI.logger.error(`Rebuilding page tree: [ FAILED ]`)
WIKI.logger.error(err.message) WIKI.logger.error(err.message)
// exit process with error code
throw err
} }
} }

View File

@ -90,5 +90,7 @@ module.exports = async (pageId) => {
} catch (err) { } catch (err) {
WIKI.logger.error(`Rendering page ID ${pageId}: [ FAILED ]`) WIKI.logger.error(`Rendering page ID ${pageId}: [ FAILED ]`)
WIKI.logger.error(err.message) WIKI.logger.error(err.message)
// exit process with error code
throw err
} }
} }