Skip to content

Commit

Permalink
Use find_each in delete_old_guest_users task (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
hackartisan authored Jan 21, 2022
1 parent 0a9c003 commit 85b2bc4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Prevent crashing processes due to loading all guest users in memory during delete_old_guest_users task ([#42](https://github.com/cbeer/devise-guests/pull/42))

## [0.8.1] - 2021-10-26
### Changed
- Simplify guest transfer ([#38](https://github.com/cbeer/devise-guests/pull/38))
Expand Down
8 changes: 5 additions & 3 deletions lib/railties/devise_guests.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ namespace :devise_guests do
# example cron entry to delete users older than 7 days at 2:00 AM every day:
# 0 2 * * * cd /path/to/your/app && /path/to/rake devise_guests:delete_old_guest_users[7] RAILS_ENV=your_env
desc "Removes entries in the users table for guest users that are older than the number of days given."
task :delete_old_guest_users, [:days_old] => [:environment] do |t, args|
args.with_defaults(days_old: 7)
User.where("guest = ? and updated_at < ?", true, Time.now - args[:days_old].to_i.days).each { |x| x.destroy }
task :delete_old_guest_users, [:days_old, :batch_size] => [:environment] do |t, args|
args.with_defaults(days_old: 7, batch_size: 1000)
User
.where("guest = ? and updated_at < ?", true, Time.now - args[:days_old].to_i.days)
.find_each(batch_size: batch_size, &:destroy)
end
end

0 comments on commit 85b2bc4

Please sign in to comment.