DEV: Support a perform_when_readonly option for Jobs::Scheduled (#30478)

This is useful for scheduled jobs that should be performed even when
`Discourse.readonly_mode?` is `true`.
This commit is contained in:
Alan Guo Xiang Tan 2024-12-27 09:42:40 +08:00 committed by GitHub
parent 56ac103547
commit cad56e2a21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 72 additions and 1 deletions

View File

@ -367,8 +367,16 @@ module Jobs
class Scheduled < Base
extend MiniScheduler::Schedule
def self.perform_when_readonly
@perform_when_readonly = true
end
def self.perform_when_readonly?
@perform_when_readonly || false
end
def perform(*args)
super if !Discourse.readonly_mode?
super if self.class.perform_when_readonly? || !Discourse.readonly_mode?
end
end

View File

@ -0,0 +1,63 @@
# frozen_string_literal: true
RSpec.describe Jobs::Scheduled do
describe "#perform" do
context "when `Discourse.readonly_mode?` is enabled" do
before { Discourse.enable_readonly_mode }
after { Discourse.disable_readonly_mode }
it "does not perform scheduled jobs in readonly mode" do
Sidekiq::Testing.inline! do
klass =
Class.new(described_class) do
every 1.minute
@called = 0
def self.count
@called
end
def self.increment
@called += 1
end
def execute(args)
self.class.increment
end
end
klass.new.perform(nil)
expect(klass.count).to eq(0)
end
end
it "still enqueues scheduled jobs that has `perform_when_readonly` option set to true in readonly mode" do
Sidekiq::Testing.inline! do
klass =
Class.new(described_class) do
every 1.minute
perform_when_readonly
@called = 0
def self.count
@called
end
def self.increment
@called += 1
end
def execute(args)
self.class.increment
end
end
klass.new.perform(nil)
expect(klass.count).to eq(1)
end
end
end
end
end