• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 883
  • Last Modified:

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?
0
Syberye
Asked:
Syberye
  • 5
  • 2
  • 2
1 Solution
 
johanntagleCommented:
I guess you can create an initialized that will delete all records from the sessions table upon start.
0
 
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.

Tanks,
0
 
johanntagleCommented:
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Thanks,
0
 
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
end

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,
Andrew
0
 
SyberyeAuthor Commented:
Hi,

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

Thanks
0
 
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
    sess.destroy
  end
end

#very bottom of the file
kill_old_sessions

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

Andrew

0
 
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 ?
0
 
SyberyeAuthor Commented:
I't not do what realy want.
0

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now