question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extend Bree from EventEmitter and emit events when workers start, etc

See original GitHub issue

I want to call the method in the main thread in the worker, and I found comlink will be a good choice, but I don’t know how to use it with bree. I need to call Comlink.wrap(worker) whenever bree starts a new worker, but there is no hooks or event to listen to. 🤔

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
niftylettucecommented, Jul 18, 2020

v1.1.17 released to npm with this added:

https://github.com/breejs/bree/releases/tag/v1.1.17

See the README for more information and examples on how to listen for when they start:

https://github.com/breejs/bree#listening-for-events

Let us know if you need anything else.

1reaction
tiaodcommented, Jul 19, 2020

Here is an example used with comlink.

// main.js
const Bree = require('bree')
const Comlink = require('comlink')
const nodeEndpoint  = require('comlink/dist/umd/node-adapter')
const bree = new Bree({
  jobs:[
    {
      name:'test',
      interval: '15s'
    }
  ]
})
const api = {
  doMath() {
    return 4;
  },
};
bree.on('worker created', name=>{
  Comlink.expose(api, nodeEndpoint(bree.workers[name]))
})
bree.start()
// jobs/test.js
const { parentPort } = require('worker_threads');
const Comlink = require('comlink')
const nodeEndpoint = require('comlink/dist/umd/node-adapter')
// get api from parent
const api = Comlink.wrap(nodeEndpoint(parentPort))
function wait(t){
  return new Promise(resolve=>setTimeout(resolve, t))
}
async function main(){
  console.log("doMath:", await api.doMath()) // call the api
  await wait(10000)
  console.log('done!')
}
main()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Bree is a Node.js and JavaScript job task scheduler ... - GitHub
Listening for events. Bree extends from EventEmitter and emits two events: worker created with an argument of name; worker deleted with ...
Read more >
Using Event Emitters in Node.js - DigitalOcean
Event emitters are objects in Node.js that trigger an event by sending a message to signal that an action was completed.
Read more >
Extend EventEmitter and call emit - typescript - Stack Overflow
The error is because you are passing one argument to emit , which declares zero parameters. There is another problem. The signature of...
Read more >
EventEmitter - Angular
Creates an instance of this class that can deliver events synchronously or asynchronously. This class is "final" and should not be extended.
Read more >
Event Emitters and Event Targets- Scaler Topics
The emit() method is used to raise specified events with the supplied arguments. All the registered event listeners are called synchronously. It ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found