Implemented ActiveJob for RateLimitChecker Job (#2878)
* Implemented ActiveJob for RateLimitChecker Job * Changes as per the review * Further changes as per the review
This commit is contained in:
parent
ff929fcf14
commit
03a122f0ce
4 changed files with 71 additions and 9 deletions
8
app/jobs/rate_limit_checker_job.rb
Normal file
8
app/jobs/rate_limit_checker_job.rb
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
class RateLimitCheckerJob < ApplicationJob
|
||||
queue_as :rate_limit_checker
|
||||
|
||||
def perform(user_id)
|
||||
user = User.find_by(id: user_id)
|
||||
PingAdmins.call(user) if user
|
||||
end
|
||||
end
|
||||
|
|
@ -34,14 +34,10 @@ class RateLimitChecker
|
|||
end
|
||||
|
||||
def ping_admins
|
||||
return unless user && Rails.env.production?
|
||||
|
||||
SlackBot.ping(
|
||||
"Rate limit exceeded. https://dev.to#{user.path}",
|
||||
channel: "abuse-reports",
|
||||
username: "rate_limit",
|
||||
icon_emoji: ":hand:",
|
||||
)
|
||||
RateLimitCheckerJob.perform_later(user.id)
|
||||
end
|
||||
|
||||
def ping_admins_without_delay
|
||||
RateLimitCheckerJob.perform_now(user.id)
|
||||
end
|
||||
handle_asynchronously :ping_admins
|
||||
end
|
||||
|
|
|
|||
24
app/services/ping_admins.rb
Normal file
24
app/services/ping_admins.rb
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
class PingAdmins
|
||||
def initialize(user)
|
||||
@user = user
|
||||
end
|
||||
|
||||
def self.call(*args)
|
||||
new(*args).call
|
||||
end
|
||||
|
||||
def call
|
||||
return unless user && Rails.env.production?
|
||||
|
||||
SlackBot.ping(
|
||||
"Rate limit exceeded. https://dev.to#{user.path}",
|
||||
channel: "abuse-reports",
|
||||
username: "rate_limit",
|
||||
icon_emoji: ":hand:",
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :user
|
||||
end
|
||||
34
spec/jobs/rate_limit_checker_job_spec.rb
Normal file
34
spec/jobs/rate_limit_checker_job_spec.rb
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe RateLimitCheckerJob, type: :job do
|
||||
include_examples "#enqueues_job", "rate_limit_checker", 2
|
||||
|
||||
describe "#perform_later" do
|
||||
let(:user) { create(:user) }
|
||||
let(:service) { PingAdmins }
|
||||
|
||||
before { allow(service).to receive(:call) }
|
||||
|
||||
it "enqueues the job" do
|
||||
expect do
|
||||
described_class.perform_later(user.id)
|
||||
end.to have_enqueued_job.with(user.id).on_queue("rate_limit_checker")
|
||||
end
|
||||
|
||||
it "calls a service" do
|
||||
perform_enqueued_jobs do
|
||||
described_class.perform_now(user.id)
|
||||
|
||||
expect(service).to have_received(:call).with(user).once
|
||||
end
|
||||
end
|
||||
|
||||
it "does nothing for non-existent user" do
|
||||
perform_enqueued_jobs do
|
||||
described_class.perform_now(nil)
|
||||
|
||||
expect(service).not_to have_received(:call)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue