Skip to content

Commit

Permalink
check current penaltyjob before scheduling new job (#117)
Browse files Browse the repository at this point in the history
* bundle

* p_j

* fix

* fix
  • Loading branch information
adnjoo authored Oct 26, 2024
1 parent 6ae82e2 commit 4ecff65
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ gem "redcarpet", "~> 3.6"
gem "front_matter_parser", "~> 1.0"

gem "sidekiq", "~> 7.3"

gem "sidekiq-unique-jobs", "~> 8.0"
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ GEM
logger
rack (>= 2.2.4)
redis-client (>= 0.22.2)
sidekiq-unique-jobs (8.0.10)
concurrent-ruby (~> 1.0, >= 1.0.5)
sidekiq (>= 7.0.0, < 8.0.0)
thor (>= 1.0, < 3.0)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
Expand Down Expand Up @@ -352,6 +356,7 @@ DEPENDENCIES
rubocop-rails-omakase
selenium-webdriver
sidekiq (~> 7.3)
sidekiq-unique-jobs (~> 8.0)
sprockets-rails
stimulus-rails
stripe (~> 13.0)
Expand Down
6 changes: 4 additions & 2 deletions app/sidekiq/penalty_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
class PenaltyJob
include Sidekiq::Job

# Ensure job uniqueness until the job starts executing
sidekiq_options lock: :until_executed

def perform(note_id)
# Find the note
note = Note.find(note_id)
user = note.user

# Check if the note is incomplete and the deadline has passed
if !note.completed && note.deadline < Time.current
Rails.logger.info "PenaltyJob: Charging user #{user.id} for not completing the task #{note.id}"
# Charge the user for not completing the task

charge_user(user, note)
end
end
Expand Down
14 changes: 14 additions & 0 deletions config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
# config/initializers/sidekiq.rb
require "sidekiq_unique_jobs"

Sidekiq.configure_server do |config|
config.redis = { url: ENV["REDIS_URL"] }

config.server_middleware do |chain|
chain.add SidekiqUniqueJobs::Middleware::Server
end

config.client_middleware do |chain|
chain.add SidekiqUniqueJobs::Middleware::Client
end
end

Sidekiq.configure_client do |config|
config.redis = { url: ENV["REDIS_URL"] }

config.client_middleware do |chain|
chain.add SidekiqUniqueJobs::Middleware::Client
end
end

0 comments on commit 4ecff65

Please sign in to comment.