Skip to content

Commit

Permalink
DBP-830 Introduce Rufus scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncolincap committed Aug 26, 2024
1 parent c983991 commit 321b02b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ gem 'rails', '~> 7.1.1'
gem 'redis', '~> 4.0'
gem 'sprockets-rails'
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
gem 'rufus-scheduler'

group :development, :test do
gem 'debug', platforms: %i[mri mingw x64_mingw]
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ GEM
email_validator (2.2.4)
activemodel
erubi (1.12.0)
et-orbi (1.2.11)
tzinfo
factory_bot (6.4.6)
activesupport (>= 5.0.0)
factory_bot_rails (6.4.3)
Expand All @@ -176,6 +178,9 @@ GEM
net-http
ffi (1.17.0-x64-mingw-ucrt)
ffi (1.17.0-x86_64-linux-musl)
fugit (1.11.1)
et-orbi (~> 1, >= 1.2.11)
raabro (~> 1.4)
globalid (1.2.1)
activesupport (>= 6.1)
google-apis-core (0.15.0)
Expand Down Expand Up @@ -319,6 +324,7 @@ GEM
public_suffix (5.0.5)
puma (6.4.2)
nio4r (~> 2.0)
raabro (1.4.0)
racc (1.8.0)
rack (3.0.11)
rack-oauth2 (2.2.1)
Expand Down Expand Up @@ -430,6 +436,8 @@ GEM
ruby-vips (2.2.1)
ffi (~> 1.12)
rubyzip (2.3.2)
rufus-scheduler (3.9.1)
fugit (~> 1.1, >= 1.1.6)
selenium-webdriver (4.10.0)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
Expand Down Expand Up @@ -540,6 +548,7 @@ DEPENDENCIES
rubocop-performance (~> 1.13)
rubocop-rails (~> 2.18, >= 2.18.0)
rubocop-rspec (~> 2.9.0)
rufus-scheduler
selenium-webdriver
shoulda-matchers (~> 5.0)
sprockets-rails
Expand Down
21 changes: 21 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,25 @@ def check_user_role_provider

errors.add(:user_provider, 'has to be the same as the Role provider') if provider != role.provider
end

def self.disable_inactive_users
setting_id = Setting.find_by(name: 'AutomatedUserBanTime')&.id
days = SiteSetting.find_by(setting_id: setting_id)&.value.to_i
if days.nil? || days.zero?
return
end
inactive_users = User.where('users.status !=2 AND users.last_login < ?',
days.days.ago).or(User.where('users.status !=2 AND users.last_login IS NULL AND users.created_at < ?',
days.days.ago))
inactive_users = inactive_users.includes(:role).where.not(roles: { name: 'Administrator' })
if inactive_users.any?
size = inactive_users.size
inactive_users.each do |user|
user.update(status: 2)
user.notify_admins_blocked_users_inactivity
rescue StandardError => e
puts "Failed to block #{user.email}: #{e.message}"
end
end
return
end
7 changes: 7 additions & 0 deletions config/initializers/scheduler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rufus-scheduler'

scheduler = Rufus::Scheduler.new

scheduler.cron '0 * * * *' do
User.disable_inactive_users
end

0 comments on commit 321b02b

Please sign in to comment.