Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expired sessions remain in the database #97

Closed
iamdriz opened this issue Jan 18, 2017 · 4 comments
Closed

Expired sessions remain in the database #97

iamdriz opened this issue Jan 18, 2017 · 4 comments

Comments

@iamdriz
Copy link

iamdriz commented Jan 18, 2017

When a session expires through inactivity or the user closes their browser, the sessions remain in the database. How can I make sure that expired sessions are always deleted without having to run a background job to keep cleaning it out.

@iamdriz
Copy link
Author

iamdriz commented Jan 18, 2017

I've added this to the Session model:

def self.sweep(time = 15.minutes)
    if time.is_a?(String)
      time = time.split.inject { |count, unit| count.to_i.send(unit) }
    end
    delete_all "updated_at < '#{time.ago.to_s(:db)}'"
end

And then in the controller method for listing sessions, I first call it:

def index
  Session.sweep
  @sessions = Session.order('created_at desc')
end

Which looks to work... except that due to another issue I have found: #98 it means sessions can be deleted early because the updated_at hasn't changed.

@mvz
Copy link

mvz commented Feb 13, 2017

@iamdriz see the db:sessions:trim task and #89.

@rafaelfranca
Copy link
Member

rafaelfranca commented May 11, 2017

The supported wait to remove expired session is to use the db:sessions:trim tasks

@eliotsykes
Copy link
Contributor

eliotsykes commented May 15, 2017

For developers working in environments where running rake tasks on a schedule is not possible/desirable, you can borrow the code from the db:sessions:trim task.

You should check what the latest code is but currently it is:

cutoff_period = (ENV['SESSION_DAYS_TRIM_THRESHOLD'] || 30).to_i.days.ago
ActiveRecord::SessionStore::Session.
  where("updated_at < ?", cutoff_period).
  delete_all

cutoff_period = (ENV['SESSION_DAYS_TRIM_THRESHOLD'] || 30).to_i.days.ago
ActiveRecord::SessionStore::Session.
where("updated_at < ?", cutoff_period).
delete_all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants