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

total and at methods don't work with ActiveJob support #40

Open
kelhusseiny opened this issue Jul 26, 2016 · 5 comments
Open

total and at methods don't work with ActiveJob support #40

kelhusseiny opened this issue Jul 26, 2016 · 5 comments

Comments

@kelhusseiny
Copy link

Using total and at methods with active job always returns 0. I think the problem come from trying to access @jid which is always return nil. so any idea how to fix that?

@cryo28
Copy link
Owner

cryo28 commented Oct 19, 2016

Sorry I don't use ActiveJob yet so I am not sure what's the problem there. Pull requests are welcome.

@ACPK
Copy link

ACPK commented Dec 11, 2016

@Azzurrio - Did you get it working with active job?

@kelhusseiny
Copy link
Author

@ACPK Not yet. Actually I did a workaround by doing something like this in application_job

class ApplicationJob < ActiveJob::Base
  attr_accessor :jid

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

PS: This works with Sidekiq only because I had to do a PR into rails to get it working.

@ACPK
Copy link

ACPK commented Dec 12, 2016

@Azzurrio Thank you!

@cpunion
Copy link

cpunion commented Feb 7, 2017

I wrote an ActiveJob adapter, not perfectly, just works.

The adapter below is compatible with Non-SidekiqStatus jobs. When the job class doesn't include SidekiqStatus::Worker, it uses the Sidekiq adapter (its simple than SidekiqStatus, so I think it's faster), otherwise use a dummy worker to call real SidekiqStatus worker.

Within worker, should include SidekiqStatus::Worker (don't inherit from ActiveJob::Base, it's denid by Sidekiq::Worker). See below.

The adapter:

module ActiveJob
  module QueueAdapters
    # == SidekiqStatus adapter for Active Job
    #
    # Simple, efficient background processing for Ruby. Sidekiq uses threads to
    # handle many jobs at the same time in the same process. It does not
    # require Rails but will integrate tightly with it to make background
    # processing dead simple.
    #
    # Read more about Sidekiq {here}[http://sidekiq.org].
    # And SidekiqStatus {here}[https://github.com/cryo28/sidekiq_status]
    #
    # To use SidekiqStatus set the queue_adapter config to +:sidekiq_status+.
    #
    #   Rails.application.config.active_job.queue_adapter = :sidekiq_status
    class SidekiqStatusAdapter
      def enqueue(job) #:nodoc:
        #Sidekiq::Client does not support symbols as keys
        job.provider_job_id = Sidekiq::Client.push \
          "class"   => job_class(job),
          "wrapped" => job.class.to_s,
          "queue"   => job.queue_name,
          "args"    => [ job.serialize ]
      end

      def enqueue_at(job, timestamp) #:nodoc:
        job.provider_job_id = Sidekiq::Client.push \
          "class"   => job_class(job),
          "wrapped" => job.class.to_s,
          "queue"   => job.queue_name,
          "args"    => [ job.serialize ],
          "at"      => timestamp
      end

      class JobWrapper #:nodoc:
        def perform(job_data)
          Base.execute job_data.merge("provider_job_id" => jid)
        end
      end

      private
        def job_class(job)
          if job.class < SidekiqStatus::Worker
            JobWrapper
          else
            ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper
          end
        end
    end
  end
end

Config your active_job in config/application.rb:

module MyApp
  class Application < Rails::Application
    config.active_job.queue_adapter = :sidekiq_status
  end
end

Worker code:

class MyWorker
  include SidekiqStatus::Worker

  def perform(*args)
    self.total = 5
    at(0, 'init')
    # do somethine
    at(1, 'xxxx'
    # ...
  end
end

Enqueues job:

MyWorker.perform_async(user)

Only a few cases tested, maybe has problems.

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

No branches or pull requests

4 participants