Session clean at RAILS server restart

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I guess you can create an initialized that will delete all records from the sessions table upon start.
SyberyeAuthor Commented:
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.

Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

SyberyeAuthor Commented:
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

Andrew DoadesIT TechnicianCommented:
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,
SyberyeAuthor Commented:

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) ?

Andrew DoadesIT TechnicianCommented:
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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SyberyeAuthor Commented:
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 ?
SyberyeAuthor Commented:
I't not do what realy want.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2008

From novice to tech pro — start learning today.