I am trying to use the model hooks plugin to do some additional database
writes to an audit logging table. The code is pretty simple, and all works
when written to execute in a single flow/thread.
def after_create
super
MyOtherModel.create(some_props)
end
However, what I would like to do is have this logic be in a fire and forget
kind of mode and just run asynchronously so that the after_create model
hook returns quicker and call time isn't blocking while making this extra
.create call. There is more going on than just the .create, but for the
sake of simplicity - I wanted to isolate the issue.
I tried using concurrent-ruby gem and wrapping the MyOtherModel.create call
inside the Future (which should run the code via a scheduler potentially on
an additional thread if I understand it properly) -but it doesn't work.
Well, it silently fails intermittently - and I only get about 20% of the
future block to succeed and write my additional data.
here is what the Future code looks like...
Concurrent::Future.execute(args: some_props, executor: :fast) do
|local_hash|
MyOtherModel.create(local_hash)
end
Again, pretty simple just wrapping the other .create call in a block so
that it executes at some other point in the future and the after_create
method can return sooner (at least that is what I am trying to achieve).
Any insights as to what might be happening? I am guessing it is something
silly on my part - but surely someone has tried to take a long(er) running
block of code inside a model hook and moved that to an async type of
operation via ActiveJob, Sidekiq or ConcurrentRuby right?
Thanks!
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.