How to delete Rails session (ActiveRecord) at the end of the request

We are using Rails with gems and cancancan. Our user sessions are stored using ActiveScripts SessionStore.

In our front-end (JavaScript), we recently started using web workers to make Ajax requests on our server. This means that these requests no longer end up in the same session as requests from the main thread.

This, in turn, means that our session table table is growing rapidly with "temporary" sessions from web worker requests that don't need to be stored. The growth rates of these temporary records are much higher than the growth rates that we had before, so we want to get rid of these unnecessary records.

We can identify web worker requests when they are received at the back.

One way to remove unwanted entries is

  • create your own SessionStore class and add an attribute to indicate the temporary nature of specific sessions.
  • you have a scheduled task to delete these records.

We will consider this as "plan B".

We would prefer to delete the temporary session at the end of the request processing. We added code to destroy the session in the after_action of the application controller;

# application_controller.rb
after_action :clean_temporary_session

def clean_temporary_session
  session.destroy if is_temporary_session?
end

      

this deletes the session record, however a new record is created immediately after the record is deleted, so the number of records is still growing.

How can I delete a temporary session record at the end of a request?

+3
ruby ruby-on-rails rails-activerecord devise


source to share


No one has answered this question yet

Check out similar questions:

1339
How can I rename a database column in Ruby on Rails migrations?
1023
How do I get the current absolute url in Ruby on Rails?
592
How to remove columns using Rails migrations
574
How can I "nicely" format the JSON output in Ruby on Rails?
470
How to redirect to 404 in Rails?
401
How can I set default values ​​in ActiveRecord?
351
How do I write a migration to rename an ActiveRecord model and its table in Rails?
287
How do I delete an ActiveRecord object?
183
How to express NOT IN request with ActiveRecord / Rails?
152
Rails extending ActiveRecord :: Base



All Articles
Loading...
X
Show
Funny
Dev
Pics