sindresorhus/eslint-plugin-unicorn

no-process-exit in worker_threads #328

lagden posted onGitHub

Is there a solution for these cases?

image

'use strict'

const {workerData, parentPort} = require('worker_threads')
const debug = require('./debug')
const sleep = require('./sleep')

const {data, seq} = workerData
sleep(3)
  .then(() => {
    // some data processing...
    const res = `modified ${data}`
    parentPort.postMessage(`Data processed - ${seq}: ${res}`)
  })
  .catch(error => {
    debug.error(error)
    process.exit(1)
  })

My worker is waiting for an exit event.

'use strict'

const {Worker} = require('worker_threads')

function runService(service, workerData) {
  return new Promise((resolve, reject) => {
    const worker = new Worker(service, {workerData})
    worker.on('message', resolve)
    worker.on('error', reject)
    worker.on('exit', code => {
      if (code !== 0) {
        reject(new Error(`Worker stopped with exit code ${code}`))
      }
    })
  })
}

module.exports = runService

@issuehunt has funded $30.00 to this issue.


posted by issuehunt-app[bot] over 5 years ago

Can I work on this on this weekend?

posted by lubien over 5 years ago

Yup :)

posted by sindresorhus over 5 years ago

I guess we just need to support the worker name there in addition to process.

I'm affraid this is not the solution for this case. The process.exit happens in the fork, not in the master's worker.on.

posted by lubien over 5 years ago

Either we just disable the rule if worker_threads is imported, or maybe more specific also check for parentPort or workerData?

posted by sindresorhus over 5 years ago

Either we just disable the rule if worker_threads is imported

This is something I feel that is the best tho I confess I haven't done work with threads in node so I don't know if it would be the best solution but I can implement it if you're okay with it.

posted by lubien over 5 years ago

@lubien Lets go with my solution for now.

posted by sindresorhus over 5 years ago

@sindresorhus has rewarded $27.00 to @lubien. See it on IssueHunt

  • :moneybag: Total deposit: $30.00
  • :tada: Repository reward(0%): $0.00
  • :wrench: Service fee(10%): $3.00
posted by issuehunt-app[bot] over 5 years ago

👏 👏 👏

posted by lagden over 5 years ago

Fund this Issue

$30.00
Rewarded

Rewarded pull request

Recent activities

lubien was rewarded by sindresorhus for sindresorhus/eslint-plugin-unicorn# 328
over 5 years ago