Skip to content

Instantly share code, notes, and snippets.

@ches
Last active September 3, 2015 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ches/225746 to your computer and use it in GitHub Desktop.
Save ches/225746 to your computer and use it in GitHub Desktop.
Use mysql to expire sessions without loading the Rails environment, so we can call this task from cron without it being unnecessarily expensive.
usage = <<DESC
Use mysql to expire sessions without loading the Rails environment, so we
can call this task from cron without it being unnecessarily expensive.
Takes path to Rails app and desired RAILS_ENV as arguments.
DESC
namespace :db do
namespace :sessions do
desc usage
task :expire, :app_path, :rails_env do |task, args|
conf = YAML.load_file(File.join(args.app_path, 'config', 'database.yml'))[args.rails_env.to_s]
sh %!mysql -u#{conf['username']} -p#{conf['password']} -e 'DELETE FROM sessions WHERE DATEDIFF(NOW(),updated_at) > 14;' #{conf['database']}!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment