Skip to content

Commit

Permalink
Add cleanup job
Browse files Browse the repository at this point in the history
  • Loading branch information
hschne committed Dec 20, 2023
1 parent 95fc41a commit 2c3fe70
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ group :development do
gem 'annotate', '~> 3.2'
gem 'erb-formatter', '~> 0.6.0'
gem 'rubocop', '~> 1.59'
gem 'rubocop-rails', '~> 2.23'
gem 'web-console'

# Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ GEM
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
rubocop-rails (2.23.0)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)
rubocop-ast (>= 1.30.0, < 2.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
rufus-scheduler (3.9.1)
Expand Down Expand Up @@ -344,6 +349,7 @@ DEPENDENCIES
puma (>= 5.0)
rails (~> 7.1.2)
rubocop (~> 1.59)
rubocop-rails (~> 2.23)
rufus-scheduler (~> 3.9)
sentry-rails (~> 5.14)
sentry-ruby (~> 5.14)
Expand Down
9 changes: 9 additions & 0 deletions app/jobs/cleanup_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CleanupJob < ApplicationJob
queue_as :default

def perform
Upload.where(expiry: (Time.now..))
.or(Upload.where('remaining_uses < ?', 1))
.destroy_all
end
end
13 changes: 13 additions & 0 deletions config/initializers/scheduler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'rufus-scheduler'

stop = defined?(Rails::Console) ||
Rails.env.test? ||
File.split($PROGRAM_NAME).last == 'rake'
return if stop

scheduler = Rufus::Scheduler.singleton

# Run cleanup job every minute
scheduler.cron '* * * * *' do
CleanupJob.perform_later
end

0 comments on commit 2c3fe70

Please sign in to comment.