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


source to share





All Articles