|
| 1 | +require "rails_helper" |
| 2 | + |
| 3 | +RSpec.describe SolidQueueWeb::SlowJobAlert do |
| 4 | + let(:webhook_url) { "http://example.com/webhook" } |
| 5 | + |
| 6 | + before do |
| 7 | + SolidQueueWeb.alert_webhook_url = webhook_url |
| 8 | + SolidQueueWeb.slow_job_threshold = 5.minutes |
| 9 | + SolidQueueWeb.alert_slow_job_count_threshold = 3 |
| 10 | + SolidQueueWeb.alert_webhook_cooldown = 3600 |
| 11 | + allow(Thread).to receive(:new).and_yield |
| 12 | + allow_any_instance_of(Net::HTTP).to receive(:request).and_return(Net::HTTPSuccess.new("1.1", "200", "OK")) |
| 13 | + end |
| 14 | + |
| 15 | + after do |
| 16 | + SolidQueueWeb.alert_webhook_url = nil |
| 17 | + SolidQueueWeb.slow_job_threshold = nil |
| 18 | + SolidQueueWeb.alert_slow_job_count_threshold = nil |
| 19 | + SolidQueueWeb.alert_webhook_cooldown = nil |
| 20 | + described_class.reset! |
| 21 | + end |
| 22 | + |
| 23 | + let(:worker_process) do |
| 24 | + SolidQueue::Process.create!( |
| 25 | + kind: "Worker", pid: 99_999, hostname: "test-host", |
| 26 | + name: "worker-slow-alert-test", last_heartbeat_at: Time.current |
| 27 | + ) |
| 28 | + end |
| 29 | + |
| 30 | + def create_slow_claimed_job(count: 1) |
| 31 | + count.times do |i| |
| 32 | + job = SolidQueue::Job.create!( |
| 33 | + queue_name: "default", |
| 34 | + class_name: "SlowTestJob", |
| 35 | + arguments: {}, |
| 36 | + active_job_id: SecureRandom.uuid |
| 37 | + ) |
| 38 | + execution = SolidQueue::ClaimedExecution.create!(job: job, process: worker_process) |
| 39 | + execution.update_columns(created_at: 10.minutes.ago) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + describe ".call" do |
| 44 | + it "fires when slow job count meets the threshold" do |
| 45 | + create_slow_claimed_job(count: 3) |
| 46 | + expect_any_instance_of(Net::HTTP).to receive(:request) |
| 47 | + described_class.call |
| 48 | + end |
| 49 | + |
| 50 | + it "fires when slow job count exceeds the threshold" do |
| 51 | + create_slow_claimed_job(count: 5) |
| 52 | + expect_any_instance_of(Net::HTTP).to receive(:request) |
| 53 | + described_class.call |
| 54 | + end |
| 55 | + |
| 56 | + it "does not fire when slow job count is below the threshold" do |
| 57 | + create_slow_claimed_job(count: 2) |
| 58 | + expect_any_instance_of(Net::HTTP).not_to receive(:request) |
| 59 | + described_class.call |
| 60 | + end |
| 61 | + |
| 62 | + it "does not count jobs that are not yet slow" do |
| 63 | + job = SolidQueue::Job.create!( |
| 64 | + queue_name: "default", |
| 65 | + class_name: "FastTestJob", |
| 66 | + arguments: {}, |
| 67 | + active_job_id: SecureRandom.uuid |
| 68 | + ) |
| 69 | + execution = SolidQueue::ClaimedExecution.create!(job: job, process: worker_process) |
| 70 | + execution.update_columns(created_at: 1.minute.ago) |
| 71 | + |
| 72 | + expect_any_instance_of(Net::HTTP).not_to receive(:request) |
| 73 | + described_class.call |
| 74 | + end |
| 75 | + |
| 76 | + it "does not fire when slow_job_threshold is not configured" do |
| 77 | + SolidQueueWeb.slow_job_threshold = nil |
| 78 | + create_slow_claimed_job(count: 5) |
| 79 | + expect_any_instance_of(Net::HTTP).not_to receive(:request) |
| 80 | + described_class.call |
| 81 | + end |
| 82 | + |
| 83 | + it "does not fire when alert_slow_job_count_threshold is not configured" do |
| 84 | + SolidQueueWeb.alert_slow_job_count_threshold = nil |
| 85 | + create_slow_claimed_job(count: 5) |
| 86 | + expect_any_instance_of(Net::HTTP).not_to receive(:request) |
| 87 | + described_class.call |
| 88 | + end |
| 89 | + |
| 90 | + it "does not fire when webhook url is not configured" do |
| 91 | + SolidQueueWeb.alert_webhook_url = nil |
| 92 | + create_slow_claimed_job(count: 5) |
| 93 | + expect_any_instance_of(Net::HTTP).not_to receive(:request) |
| 94 | + described_class.call |
| 95 | + end |
| 96 | + |
| 97 | + it "does not fire again within the cooldown window" do |
| 98 | + create_slow_claimed_job(count: 3) |
| 99 | + described_class.call |
| 100 | + expect_any_instance_of(Net::HTTP).not_to receive(:request) |
| 101 | + described_class.call |
| 102 | + end |
| 103 | + |
| 104 | + it "fires again after the cooldown window expires" do |
| 105 | + create_slow_claimed_job(count: 3) |
| 106 | + described_class.call |
| 107 | + described_class.instance_variable_set(:@last_fired_at, 2.hours.ago) |
| 108 | + expect_any_instance_of(Net::HTTP).to receive(:request) |
| 109 | + described_class.call |
| 110 | + end |
| 111 | + |
| 112 | + it "sends a JSON payload with the correct fields" do |
| 113 | + create_slow_claimed_job(count: 4) |
| 114 | + posted_body = nil |
| 115 | + allow_any_instance_of(Net::HTTP).to receive(:request) do |_, req| |
| 116 | + posted_body = JSON.parse(req.body) |
| 117 | + Net::HTTPSuccess.new("1.1", "200", "OK") |
| 118 | + end |
| 119 | + |
| 120 | + described_class.call |
| 121 | + |
| 122 | + expect(posted_body["event"]).to eq("slow_job_threshold_exceeded") |
| 123 | + expect(posted_body["slow_job_count"]).to eq(4) |
| 124 | + expect(posted_body["threshold"]).to eq(3) |
| 125 | + expect(posted_body["fired_at"]).to be_present |
| 126 | + end |
| 127 | + |
| 128 | + it "sets Content-Type to application/json" do |
| 129 | + create_slow_claimed_job(count: 3) |
| 130 | + sent_request = nil |
| 131 | + allow_any_instance_of(Net::HTTP).to receive(:request) do |_, req| |
| 132 | + sent_request = req |
| 133 | + Net::HTTPSuccess.new("1.1", "200", "OK") |
| 134 | + end |
| 135 | + |
| 136 | + described_class.call |
| 137 | + |
| 138 | + expect(sent_request["Content-Type"]).to eq("application/json") |
| 139 | + end |
| 140 | + |
| 141 | + it "logs an error and does not raise when the HTTP request fails" do |
| 142 | + create_slow_claimed_job(count: 3) |
| 143 | + allow_any_instance_of(Net::HTTP).to receive(:request).and_raise(RuntimeError, "connection refused") |
| 144 | + expect(Rails.logger).to receive(:error).with(/Slow job alert webhook failed/) |
| 145 | + expect { described_class.call }.not_to raise_error |
| 146 | + end |
| 147 | + |
| 148 | + context "when alert_webhook_url is an array" do |
| 149 | + let(:second_url) { "http://example.com/second-webhook" } |
| 150 | + |
| 151 | + before { SolidQueueWeb.alert_webhook_url = [webhook_url, second_url] } |
| 152 | + |
| 153 | + it "posts to all configured URLs" do |
| 154 | + create_slow_claimed_job(count: 3) |
| 155 | + expect(Net::HTTP).to receive(:new).twice.and_call_original |
| 156 | + described_class.call |
| 157 | + end |
| 158 | + end |
| 159 | + end |
| 160 | +end |
0 commit comments