Move Twitch webhook registration from ActiveJob to Sidekiq (#5414)
This commit is contained in:
parent
92b5f113be
commit
dba7e6ca60
7 changed files with 58 additions and 53 deletions
|
|
@ -44,7 +44,7 @@ class UsersController < ApplicationController
|
|||
if @user.twitch_username != new_twitch_username
|
||||
if @user.update(twitch_username: new_twitch_username)
|
||||
@user.touch(:profile_updated_at)
|
||||
Streams::TwitchWebhookRegistrationJob.perform_later(@user.id) if @user.twitch_username?
|
||||
Streams::TwitchWebhookRegistrationWorker.perform_async(@user.id) if @user.twitch_username?
|
||||
end
|
||||
flash[:settings_notice] = "Your Twitch username was successfully updated."
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,12 +0,0 @@
|
|||
module Streams
|
||||
class TwitchWebhookRegistrationJob < ApplicationJob
|
||||
queue_as :twitch_webhook_registration
|
||||
|
||||
def perform(user_id, service = TwitchWebhook::Register)
|
||||
user = User.find_by(id: user_id)
|
||||
return if user.blank? || user.twitch_username.blank?
|
||||
|
||||
service.call(user)
|
||||
end
|
||||
end
|
||||
end
|
||||
13
app/workers/streams/twitch_webhook_registration_worker.rb
Normal file
13
app/workers/streams/twitch_webhook_registration_worker.rb
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
module Streams
|
||||
class TwitchWebhookRegistrationWorker
|
||||
include Sidekiq::Worker
|
||||
sidekiq_options queue: :low_priority, retry: 10
|
||||
|
||||
def perform(user_id)
|
||||
user = User.find_by(id: user_id)
|
||||
return if user.blank? || user.twitch_username.blank?
|
||||
|
||||
TwitchWebhook::Register.call(user)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -2,7 +2,7 @@ namespace :twitch do
|
|||
desc "Register for Webhooks for all User's Registered with Twitch"
|
||||
task wehbook_register_all: :environment do
|
||||
User.where.not(twitch_username: nil).find_each do |user|
|
||||
Streams::TwitchWebhookRegistrationJob.perform_later(user.id)
|
||||
Streams::TwitchWebhookRegistrationWorker.perform_async(user.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,33 +0,0 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe Streams::TwitchWebhookRegistrationJob, type: :job do
|
||||
let(:user) { create(:user, twitch_username: "test-username") }
|
||||
|
||||
let(:service) { double }
|
||||
|
||||
before do
|
||||
allow(service).to receive(:call)
|
||||
end
|
||||
|
||||
context "when the user does NOT have a twitch username present" do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
it "noops" do
|
||||
described_class.perform_now(user.id, service)
|
||||
|
||||
expect(service).not_to have_received(:call)
|
||||
end
|
||||
end
|
||||
|
||||
it "noops when the id passed does not belong to a user" do
|
||||
described_class.perform_now(987_654_321, service)
|
||||
|
||||
expect(service).not_to have_received(:call)
|
||||
end
|
||||
|
||||
it "registers for webhooks" do
|
||||
described_class.perform_now(user.id, service)
|
||||
|
||||
expect(service).to have_received(:call).with(user)
|
||||
end
|
||||
end
|
||||
|
|
@ -137,9 +137,9 @@ RSpec.describe "UserSettings", type: :request do
|
|||
end
|
||||
|
||||
it "schedules the job while updating" do
|
||||
expect do
|
||||
sidekiq_assert_enqueued_with(job: Streams::TwitchWebhookRegistrationWorker, args: [user.id]) do
|
||||
post "/users/update_twitch_username", params: { user: { twitch_username: "anna_lightalloy" } }
|
||||
end.to have_enqueued_job(Streams::TwitchWebhookRegistrationJob).exactly(:once).with(user.id)
|
||||
end
|
||||
end
|
||||
|
||||
it "removes twitch_username" do
|
||||
|
|
@ -150,16 +150,16 @@ RSpec.describe "UserSettings", type: :request do
|
|||
end
|
||||
|
||||
it "doesn't schedule the job when removing" do
|
||||
expect do
|
||||
sidekiq_assert_no_enqueued_jobs(only: Streams::TwitchWebhookRegistrationWorker) do
|
||||
post "/users/update_twitch_username", params: { user: { twitch_username: "" } }
|
||||
end.not_to have_enqueued_job(Streams::TwitchWebhookRegistrationJob)
|
||||
end
|
||||
end
|
||||
|
||||
it "doesn't schedule the job when saving the same twitch username" do
|
||||
user.update_column(:twitch_username, "robot")
|
||||
expect do
|
||||
sidekiq_assert_no_enqueued_jobs(only: Streams::TwitchWebhookRegistrationWorker) do
|
||||
post "/users/update_twitch_username", params: { user: { twitch_username: "robot" } }
|
||||
end.not_to have_enqueued_job(Streams::TwitchWebhookRegistrationJob)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe Streams::TwitchWebhookRegistrationWorker, type: :worker do
|
||||
include_examples "#enqueues_on_correct_queue", "low_priority", 1
|
||||
|
||||
describe "#perform" do
|
||||
let(:user) { create(:user, twitch_username: "test-username") }
|
||||
let(:worker) { subject }
|
||||
let(:service) { Streams::TwitchWebhook::Register }
|
||||
|
||||
before do
|
||||
allow(service).to receive(:call)
|
||||
end
|
||||
|
||||
context "when the user does NOT have a twitch username present" do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
it "noops" do
|
||||
worker.perform(user.id)
|
||||
|
||||
expect(service).not_to have_received(:call)
|
||||
end
|
||||
end
|
||||
|
||||
it "noops when the id passed does not belong to a user" do
|
||||
worker.perform(987_654_321)
|
||||
|
||||
expect(service).not_to have_received(:call)
|
||||
end
|
||||
|
||||
it "registers for webhooks" do
|
||||
worker.perform(user.id)
|
||||
|
||||
expect(service).to have_received(:call).with(user)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue