Skip to content
This repository has been archived by the owner on Apr 1, 2023. It is now read-only.

Update status in method within worker #140

Open
jameshaythornthwaite opened this issue Nov 18, 2018 · 8 comments
Open

Update status in method within worker #140

jameshaythornthwaite opened this issue Nov 18, 2018 · 8 comments

Comments

@jameshaythornthwaite
Copy link

I have a worker with a method that takes a long time to execute and would like to update the progress status within this method. Currently my plan is to pass the job ID to the method and then periodically update it but i cannot work out how to do this, I have only managed to retrieve the status but not set it.

class MyJob < ApplicationJob
  queue_as :default

  def perform(*args)
    total 100
    at 5
    slow_method(self.provider_job_id)
  end
end

The other class with the method called in it:

class OtherClass
    def slow_method(job_id)
    ??????
end
@ziaulrehman40
Copy link

Looking for same @jameshaythornthwaite Anything you have managed yet?

I particularly want to use the at helper method to set percentages in sub function calls. I was thinking to pass method as argument or something. Unless there is a better way

@jay2u
Copy link

jay2u commented Sep 15, 2019

I've managed to "work around" this by not passing the job_id to my class with the slow method in it but the entire job itself instead. It might not be best practice but it surely works.

class MyJob < ApplicationJob
  queue_as :default

  def perform(*args)
    Otherclass.slow_method(self)
  end
end

class OtherClass
    def self.slow_method(sidekiq_job)
      sidekiq_job.total 100

      ...
      sidekiq_job.at 5
      ...
      sidekiq_job.at 10
      ...
      sidekiq_job.at 15
      ...

end

@echan00
Copy link

echan00 commented Nov 6, 2019

Also looking for something like this!

@jay2u
Copy link

jay2u commented Dec 9, 2019

Also looking for something like this!

The solution I posted above works very well for me. I use it in production systems on a daily basis.

@echan00
Copy link

echan00 commented Dec 9, 2019

The solution I posted above works very well for me. I use it in production systems on a daily basis.

Thanks @jay2u, I considered your solution but it is a bit tough for me as it would require passing sidekiq_job through multiple functions in OtherClass

@jay2u
Copy link

jay2u commented Dec 30, 2019

Thanks @jay2u, I considered your solution but it is a bit tough for me as it would require passing sidekiq_job through multiple functions in OtherClass

@echan00
You could consider passing the sidekiq job to your OtherClass as an instance variable after which you should be able to reference the sidekiq job in all methods of your class without the need of passing it around all the time.

It is important however that all your methods in OtherClass are instance methods (i.e. not defined as self.) and that you create an instance of OtherClass before you start calling the slow methods.

It's still a bit contrived but it should hopefully do the trick for you.

Here's an example:

class MyJob < ApplicationJob
  queue_as :default

  def perform(*args)
    # Create instance of OtherClass so it's unique for each parallel worker
    # and pass sidekiq_job
    otherclass_instance = OtherClass.new(self)
    
    # This is where the heavy lifting starts
    otherclass_instance.slow_method
    otherclass_instance.another_slow_method
    ...
  end

end

class OtherClass

  def initialize(sidekiq_job)
    @sidekiq_job = sidekiq_job

    @sidekiq_job.total 100

  end

  def slow_method
    
    ...
    @sidekiq_job.at 5
    ...
    @sidekiq_job.at 10
    ...
    @sidekiq_job.at 15
    ...
  
  end

  def another_slow_method
    ...
    @sidekiq_job.at 20
    ...
    @sidekiq_job.at 25
    ...
    @sidekiq_job.at 30
    ...
  
  end

end

@echan00
Copy link

echan00 commented Dec 30, 2019

@jay2u thanks will try this and let you know!

@echan00
Copy link

echan00 commented Feb 23, 2020

@jay2u working well thanks!

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

No branches or pull requests

4 participants