Skip to content

Commit

Permalink
Add jobs to verify commitments expired in 7 days
Browse files Browse the repository at this point in the history
  • Loading branch information
eltonsantos committed Oct 18, 2024
1 parent fcdf81a commit d57f125
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 7 deletions.
15 changes: 8 additions & 7 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ gem "bootsnap", require: false
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

gem 'devise'
gem 'kaminari'
gem 'ransack'
gem 'lucide-rails'
gem 'paper_trail'
gem "devise"
gem "kaminari"
gem "ransack"
gem "lucide-rails"
gem "paper_trail"
gem "sidekiq"

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
Expand All @@ -56,8 +57,8 @@ end
group :development do
# Use console on exceptions pages [https://github.com/rails/web-console]
gem "web-console"
gem 'bullet'
gem 'rails-erd'
gem "bullet"
gem "rails-erd"
end

group :test do
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ GEM
i18n
rdoc (6.7.0)
psych (>= 4.0.0)
redis-client (0.22.2)
connection_pool
regexp_parser (2.9.2)
reline (0.5.10)
io-console (~> 0.5)
Expand Down Expand Up @@ -291,6 +293,12 @@ GEM
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
sidekiq (7.3.2)
concurrent-ruby (< 2)
connection_pool (>= 2.3.0)
logger
rack (>= 2.2.4)
redis-client (>= 0.22.2)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
Expand Down Expand Up @@ -366,6 +374,7 @@ DEPENDENCIES
ransack
rubocop-rails-omakase
selenium-webdriver
sidekiq
sprockets-rails
stimulus-rails
tailwindcss-rails
Expand Down
13 changes: 13 additions & 0 deletions app/jobs/commitment_expiration_check_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CommitmentExpirationCheckJob < ApplicationJob
queue_as :default

def perform(commitment_id)
commitment = Commitment.find_by(id: commitment_id)

return unless commitment

if commitment.active && commitment.created_at < 7.days.ago
commitment.update(active: false)
end
end
end
6 changes: 6 additions & 0 deletions app/models/commitment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ class Commitment < ApplicationRecord
validates :description, presence: true
validates :progress, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 100 }

after_create :schedule_expiration_check

def schedule_expiration_check
CommitmentExpirationCheckJob.set(wait_until: created_at + 7.days).perform_later(id)
end

def check_active_status
if active && created_at < 7.days.ago
update(active: false)
Expand Down
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ class Application < Rails::Application
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
# config.i18n.default_locale = :'pt-BR'

config.active_job.queue_adapter = :sidekiq
end
end
10 changes: 10 additions & 0 deletions lib/tasks/commitment_expiration.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace :commitments do
desc "Check for expired commitments and deactivate them"
task check_expired: :environment do
Commitment.where(active: true).each do |commitment|
if commitment.created_at < 7.days.ago
commitment.update(active: false)
end
end
end
end
7 changes: 7 additions & 0 deletions test/jobs/commitment_expiration_check_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class CommitmentExpirationCheckJobTest < ActiveJob::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit d57f125

Please sign in to comment.