简体   繁体   中英

How can I set default_url_options for Sidekiq workers?

I have a Sidekiq worker that is intended to perform social actions (eg: like pages on Facebook). Part of this requires knowing the URL for the object being liked.

Fortunately, Rails 3 makes it easy to access app-specific routes by including Rails.application.routes.url_helpers in whatever class or module needs access to the path/url helper method.

The problem I'm running into is that my default url/port are not accessible from within my Sidekiq worker despite various attempts to define them in my development.rb or production.rb .

class Facebook::LikeRecipeWorker
  include Sidekiq::Worker
  include Rails.application.routes.url_helpers

  sidekiq_options queue: :facebook

  def perform(recipe_id, user_id)
    recipe = Recipe.find(recipe_id)
    user = User.find(user_id)

    if user.facebook_token
      api = Koala::Facebook::API.new(user.facebook_token)
      api.put_connections 'me', 'my_namespace:like', object: recipe_url(recipe)
    end
  end
end

When the recipe_url method is access, an ArgumentError is raised with the message:

ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

I know that I can specify default_url_options for ActionController or ActionMailer in the environment-specific config files, eg:

config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_controller.default_url_options = { host: 'localhost', port: 3000 }

However, these (rightfully) appear to have no influence on my Sidekiq worker classes. How should I go about defining default_url_options for these classes?

What we did was this:

In your config file(s) (eg config/application.rb, config/production.db, etc.), have a line that sets the default:

 routes.default_url_options = { host: 'www.example.org' }

I found a potential solution to this, though it feels like a little bit of a hack. I'm definitely open to better answers.

First, in my environment files (eg: config/environments/development.rb ), I specify the default_url_options for my controllers:

config.action_controller.default_url_options = { host: 'localhost', port: 3000 }

Then in my worker class, I define a default_url_options instance method there:

class Facebook::LikeRecipeWorker
  # ...

  private

  def default_url_options
    ActionController::Base.default_url_options
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM