tjmehta / coworkers

A RabbitMQ Microservice Framework in Node.js
MIT License
610 stars 36 forks source link

What to do if a want to ack asynchronously? #57

Closed dilame closed 7 years ago

dilame commented 7 years ago

I have long async task, that i want to ack/nack only after it finish. How can i implement this with coworkers? As i see, i can ony do it with context properties, but in case with RabbitMQ and JS this is unusable almost always. Or did I understand something wrong?

tjmehta commented 7 years ago

I am not sure what you mean exactly. Here’s a simple example acking after an async task:

app.use(function * () {
  yield Promise.resolve()
  this.ack = true
})
dilame commented 7 years ago

@tjmehta you understand me right. Thank you. But what if in the end i want to nack, depends on result of promise?

tjmehta commented 7 years ago
app.use(function * () {
  const result = yield Promise.resolve(‘foo’)
  if (result === ‘foo’) {
    this.ack = true
  } else {
    this.nack = true
  }
})

-or-

app.use(function * () {
  try {
    yield Promise.resolve(‘foo’)
    this.ack = true
  } catch (err) {
    this.nack = false
  }
})
tjmehta commented 7 years ago

I am going to close this issue, but please feel free to continue asking questions here and I will answer any you have :)

dilame commented 7 years ago

Thank you! Can i use https://github.com/nswbmw/koa-mongo for mongodb connection? If no, what is the best practice for this?

tjmehta commented 7 years ago

coworkers is not 100% compatible with koa plugins. But you could copy patterns you see in koa plugins. I typically just open up a single db connection on application start.