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

Fix accessing provider_job_id inside active jobs for sidekiq adapter #25961

Merged
merged 1 commit into from
Jul 28, 2016

Conversation

kelhusseiny
Copy link
Contributor

@kelhusseiny kelhusseiny commented Jul 26, 2016

Accessing provider_job_id attribute inside a job using sidekiq adapter always returns nil. I looked at SidekiqAdapter class inside ActiveJob, and it looks like that enqueue method here already fire the job before assigning its id to job.provider_job_id.

 def enqueue(job) #:nodoc:
        #Sidekiq::Client does not support symbols as keys
        job.provider_job_id = Sidekiq::Client.push(
          'class'   => JobWrapper,
          'wrapped' => job.class.to_s,
          'queue'   => job.queue_name,
          'args'    => [ job.serialize ])
      end

I need to access it so I can use Sidekiq::Status tracking-progress methods. The basic idea behind these methods is to store the progress using the jid attribute within Sidekiq::Worker. So inside ActiveJob while we don't have access to that jidattribute, we can easily work around that by using a before_perform callback like that

class ActiveJob::Base
  attr_accessor :jid

  before_perform do |job|
    job.jid = job.provider_job_id
  end
end

but the problem now, is that provider_job_id always returns nil.

so I fixed this by merging the jid to the job_data inside JobWrapper#perform method, and then assigning it inside deserialize method.

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @schneems (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@mperham
Copy link
Contributor

mperham commented Jul 26, 2016

Looks fine to me. Not sure if you need the same support for the other adapters + tests.

@maclover7
Copy link
Contributor

Build failures are unrelated.

@rafaelfranca
Copy link
Member

We need tests for sure.

@@ -37,7 +37,7 @@ class JobWrapper #:nodoc:
include Sidekiq::Worker

def perform(job_data)
Base.execute job_data
Base.execute job_data.merge('provider_job_id' => jid)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is being done here is it still needed to set the provider_job_id in the enqueue method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we still need to set it in the enqueue method so we can access it outside the job (not inside). ie:

job = SomeJob.perform_later
jid = job.provider_job_id

@kelhusseiny
Copy link
Contributor Author

@rafaelfranca ok, so could you give me any idea how can I test this? I believe that all we need to do here is to make sure that job_provider_id is accessible inside the job and doesn't return nil. suggestions?

@rafaelfranca
Copy link
Member

Something like this test but exposing the value that was inside the job? https://github.com/rails/rails/blob/master/activejob/test/integration/queuing_test.rb#L62

@kelhusseiny kelhusseiny force-pushed the master branch 3 times, most recently from ccc8b61 to ff59074 Compare July 28, 2016 02:04
@kelhusseiny
Copy link
Contributor Author

kelhusseiny commented Jul 28, 2016

@rafaelfranca ok man, done! I've added a test and CI is green now

@rafaelfranca rafaelfranca merged commit 410a214 into rails:master Jul 28, 2016
rafaelfranca added a commit that referenced this pull request Jul 28, 2016
Fix accessing provider_job_id inside active jobs for sidekiq adapter
@rafaelfranca
Copy link
Member

Backported in cbe752f

@volkanunsal
Copy link

I know this is an old issue, but I noticed that provider_job_id value is flaky in my tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants