Class: SessionCleanerTask

Inherits:
Scheduler::SchedulerTask show all
Defined in:
lib/scheduler_daemon/rails/generators/scheduler/templates/lib/scheduled_tasks/session_cleaner_task.rb

Instance Attribute Summary

Attributes inherited from Scheduler::SchedulerTask

#daemon_scheduler, #rufus_scheduler

Instance Method Summary collapse

Methods inherited from Scheduler::SchedulerTask

add_to, at, cron, environments, every, in, #log, should_run_in_current_environment?

Instance Method Details

#remove_old_sessionsObject



10
11
12
13
14
15
16
17
18
# File 'lib/scheduler_daemon/rails/generators/scheduler/templates/lib/scheduled_tasks/session_cleaner_task.rb', line 10

def remove_old_sessions
  log "running the session cleaner"
  if ActionController::Base.session_store == session_store_class
    ActiveRecord::Base.connection.execute("DELETE FROM #{session_table_name} WHERE updated_at < '#{7.days.ago.to_s(:db)}'")
    log "old sessions are gone!"
  else
    log "sessions are not stored in the database; nothing to do."
  end
end

#runObject



6
7
8
# File 'lib/scheduler_daemon/rails/generators/scheduler/templates/lib/scheduled_tasks/session_cleaner_task.rb', line 6

def run
  remove_old_sessions
end

#session_store_classObject



20
21
22
23
24
# File 'lib/scheduler_daemon/rails/generators/scheduler/templates/lib/scheduled_tasks/session_cleaner_task.rb', line 20

def session_store_class
  return ActiveRecord::SessionStore if defined?(ActiveRecord::SessionStore)
  # pre rails 2.3 support...
  return CGI::Session::ActiveRecordStore if defined?(CGI::Session::ActiveRecordStore)
end

#session_table_nameObject



26
27
28
# File 'lib/scheduler_daemon/rails/generators/scheduler/templates/lib/scheduled_tasks/session_cleaner_task.rb', line 26

def session_table_name
  ActiveRecord::Base.pluralize_table_names ? :sessions : :session
end