Session clean at  RAILS server restart

Posted on 2011-10-19
Last Modified: 2012-05-12
I have some user informations stored in the session.
If i use Ctrl-c to kill the RAILS server and restart the server, the informations remains in the session which is stored in database.
I want the session informations gone, once i restart the server
Any suggestions?
How do I know when server has finised loading the enviroment. Will an event be trigger? if yes how can I catch this event?
Question by:Syberye
    LVL 24

    Expert Comment

    I guess you can create an initialized that will delete all records from the sessions table upon start.

    Author Comment

    I can't delete the records from session table (because of bussiness rules) I just want to delete one element from the session. I have created a varible in application.rb and I check for that variable to see if the server has been restarted. Could You give me a better (optimaze) solution.

    I want to modify the session without deleting it. Is there in RAILS an event that is triggered when the server finished restrating? I want to catch the moment when the server finised starting.

    LVL 24

    Expert Comment


    Author Comment

    I read the arcticle. Its nice , but unfortunatly it will not resolve my problem.
    I will describe my problem more detailed.
    The session is stored in the DB. After I close the server with ctrl+c and restart it , the session is restored from the DB.
    My issue is, that i keep some data in the session that i want to keep, but some of the data needs to be deleted from it.
    Ex: i want to keep the user related stuffs, but i want to delete the some data that is not important anymore.
    Deleting all the records from the session table is not an option.
    If i place a file in the "config/initializers/" as described, it will be loaded, but in that moment i dont have the session.
    So if i place a "puts session.inspect" or "puts session[:user_id]" i get an "undefined local variable or method `session' for main:Object" error.
    So that means that the session was not retrieved yet.
    The main problem is that in the application_controller i have a call that will return the session data , like this: session[:something].
    That must be nil after the server has been restarted.
    What i want to do is the following:
    - catch an event that is triggered when the server has been restarted , but after the session is restored so i can delete my ":something"
    - or write a methode that will be called only once in the application after the server has been restarted.

    For now i have implemented a quick fixed (its a dirty one): have added in the application.rb config.server_start = true.
    Every time that i will call the function from application_controller i will check if this flag is true and set it to false and delete the ":something".
    I need a clean version , a nice implementation

    LVL 10

    Expert Comment

    by:Andrew Doades
    I would created an step in perhaps either your application controller or 'root' controller like the below:

    def clean_data
      session[:userdata] = nil

    Basically you just need to nil the session data you want to remove.
    I do this in my login controller, to nil the user_id, and other data stored throughout the system, in case one user logs out and another logs in from the same browser session, the previous user's data is no longer available.

    Hope this helps,

    Author Comment


    Thanks for the info .But unfortunatly this will not resolve my problem because at server restart the user will not be asked to login again, because the session is restored (with the user_id in the session, so automaticly the application will see as a logged in user).
    I searched for a solution to catch the moment the server has loaded the session from the DB. Is there something ? An event ? A file that is loaded at that moment ? Or an event that is triggered when the server has finished loading the environment (and where i have a session) ?

    LVL 10

    Accepted Solution

    I guess what you wan then, if you're using the db to store the sessions, is some code to fire off in the environment.rb file to wipe them out?
    Something like the below (Its not the best but you get the idea)

    #top of environment.rb - after require File.join(File.dirname(__FILE__), 'boot')

    def kill_old_sessions
      @sessions = Session.find(:all)
      for sess in @sessions

    #very bottom of the file

    You could of course remove the "def....." bits and just let this run regardless.



    Author Comment

    It's not realy okay

    I want to get only the session which was create by that instance of server - which was restart. I don't want to tuch other session from other instance of servers. Basic I want to modify some elements from session when the one server was restarted (only the session which was create by that instance of server). Where can I put some code and how can I get only the sessions created by my instant of restarted server ?

    Author Closing Comment

    I't not do what realy want.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Recently, I was asked to look into SCCM 2007 by my employer, having a degree of experience of earlier versions of SMS and some previous SCCM knowledge I didn't expect the procedure to involve to much time. I read a number of guides concerning it…
    I have been working as System Administrators since 2003. I recently started working as a FreeLancer and was amazed to find out that very few people are taking full advantage of their Windows Server Machines. Microsoft Windows Server comes with so…
    This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
    This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now