Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize JobWrapper queueing ActiveJobs #35

Merged
merged 6 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/delayed/active_job_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def _enqueue(job, opts = {})
opts.merge!({ queue: job.queue_name, priority: job.priority }.compact)
.merge!(job.provider_attributes || {})

Delayed::Job.enqueue(JobWrapper.new(job.serialize), opts).tap do |dj|
Delayed::Job.enqueue(JobWrapper.new(job), opts).tap do |dj|
job.provider_job_id = dj.id
end
end
Expand Down
16 changes: 13 additions & 3 deletions lib/delayed/job_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ class JobWrapper # rubocop:disable Betterment/ActiveJobPerformable

delegate_missing_to :job

def initialize(job_data)
@job_data = job_data
def initialize(job_or_data)
# During enqueue the job instance is passed in directly, saves us deserializing
# it to find out how to queue the job.
# During load from the db, we get a data hash passed in so deserialize lazily.
if job_or_data.is_a?(ActiveJob::Base)
@job = job_or_data
@job_data = job.serialize
caius marked this conversation as resolved.
Show resolved Hide resolved
else
@job_data = job_or_data
end
end

def display_name
Expand All @@ -25,7 +33,9 @@ def encode_with(coder)
private

def job
@job ||= ActiveJob::Base.deserialize(job_data) if job_data
return @job if defined?(@job)

@job = ActiveJob::Base.deserialize(job_data) if job_data
caius marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
10 changes: 10 additions & 0 deletions spec/delayed/active_job_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ def perform; end
ActiveJob::Base.queue_adapter = adapter_was
end

it "does not invoke #deserialize during enqueue" do # rubocop:disable RSpec/NoExpectationExample
JobClass.include(Module.new do
def deserialize(*)
raise "uh oh, deserialize called during enqueue!"
end
end)

JobClass.perform_later
end

it 'serializes a JobWrapper in the handler with expected fields' do
Timecop.freeze('2023-01-20T18:52:29Z') do
JobClass.perform_later
Expand Down
Loading