Jeff-Lewis / cls-hooked

cls-hooked : CLS using AsynWrap or async_hooks instead of async-listener for node 4.7+
BSD 2-Clause "Simplified" License
761 stars 89 forks source link

Cls with library using bluebird (knex) #16

Open froxCZ opened 7 years ago

froxCZ commented 7 years ago

Hey guys, thanks a lot for this lib, I managed to get it running on node 8.6.0 with express and async/await. However, I would like to ask if it is possible to use it with other library that is using Bluebird promises - knexjs.org. My ultimate goal is to wrap knex.transaction with my own function that is going to set the transaction to namespace so the transaction can be accessed from other places.

I tried something with cls-bluebird or using ns.run or ns.bind without any success. It fails on Unhandled rejection Error: No context available. ns.run() or ns.bind() must be called first.. Accessing the namespace out of knex.transaction() works fine.

Thanks for any kind of help!

Jeff-Lewis commented 6 years ago

@froxCZ Sorry for not commenting earlier. I am eager to use cls-hooked with Bluebird as well, natively that is, without monkey patching. There's been work to add AsyncResource to Bluebird which would make Bluebird compatible with async_hooks and cls-hooked, although it has somewhat stalled because of its performance scare. (IMHO, I'd take a 10x slower node with reliable cls anyday...)

Alternatively, you can try cls-bluebird which has very good test test coverage, although you can't use async/await with it as async/await uses native promises.

mrmurphy commented 6 years ago

Hi! I'm just doing research about which approach is going to be most reliable, cls-hooked, or the old style of continuation-local-storage? I was specifically looking around for mentions cls-hooked of having to wrap a bunch of 3rd party modules in order to keep the context around. This is the only issue I could find that mentions something like that.

Does CLS-hooked work only with native promises? Will I have to wrap any library that uses a non-native promise implementation?

I'm guessing I'd just run my server, look at my logs, and see which logs are missing the request ID. Then I'd figure out which library to wrap.