Home > Net >  (Async/MultiThread) How can I tell ruby to save to my database, but to continue processing and not t
(Async/MultiThread) How can I tell ruby to save to my database, but to continue processing and not t

Time:01-14

in my API i'm doing something that has become a scaling issue and will likely be implemented in a different way in the future

whenever my API is hit I update the users last activity timestamp at the end of the request, currently the request will not complete and send the response to the user until after this line of code finishes, is it possible to kick it off to a different thread and detach it so the code keeps going without waiting for the save event to complete?

I'm trying to avoid sending it to rabbitMQ for background processing or to redis since this data is saved to the DB

after do
  unless @user.nil?
    @user.last_activity = Time.now
    @user.save
  end
  log :info, "#{request.ip} - #{@method.upcase} [#{status}] #{@path}"
end

CodePudding user response:

I found something that may help you in your case https://github.com/challengepost/activeasync

There is also a simple way I searched. so far haven't implemented and that is to use thread

https://guides.rubyonrails.org/threading_and_code_execution.html

It should be like in your case

Thread.new { your task }

Or

Thread.new do
  //Your code

end

  •  Tags:  
  • Related