Skip to content

Commit

Permalink
fix: jobs/worker - pass through job error from worker process (#3822)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkho authored Apr 12, 2021
1 parent a20f70e commit 71aa0c9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
14 changes: 11 additions & 3 deletions server/core/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,22 @@ class Job {
`--job=${this.name}`,
`--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) => {
proc.on('exit', (code, signal) => {
const data = Buffer.concat(stderr).toString()
if (code === 0) {
resolve()
resolve(data)
} 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()
})
Expand Down
9 changes: 7 additions & 2 deletions server/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ WIKI.logger = require('./logger').init('JOB')
const args = require('yargs').argv

;(async () => {
await require(`../jobs/${args.job}`)(args.data)
process.exit(0)
try {
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)
}
})()
2 changes: 2 additions & 0 deletions server/jobs/rebuild-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,7 @@ module.exports = async (pageId) => {
} catch (err) {
WIKI.logger.error(`Rebuilding page tree: [ FAILED ]`)
WIKI.logger.error(err.message)
// exit process with error code
throw err
}
}
2 changes: 2 additions & 0 deletions server/jobs/render-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,7 @@ module.exports = async (pageId) => {
} catch (err) {
WIKI.logger.error(`Rendering page ID ${pageId}: [ FAILED ]`)
WIKI.logger.error(err.message)
// exit process with error code
throw err
}
}

0 comments on commit 71aa0c9

Please sign in to comment.