ruby-shoryuken / shoryuken

A super efficient Amazon SQS thread based message processor for Ruby. This project is in MAINTENANCE MODE. Reach me out on Slack link on the description if you want to become a new maintainer.
Other
2.06k stars 280 forks source link

Best Practices for configuration - Plain Ruby #724

Closed psulightning closed 1 year ago

psulightning commented 1 year ago

Looking to use shoryuken in a plain ruby environment and wanted to know the best practices on how to leverage configure_server or configure_client since I would not have a Rails initializer. I would rather have separation of concerns for the config not being inside the worker's file.

Would I be better off rolling my own script instead of using the built-in shoryuken executable?

Thanks

matt-taylor commented 1 year ago

Hi @psulightning

For non Rails execution of Shoryuken, If possible, I would rely on ENV variables for Server Config. If that is not possible and you still require a seperation of config and worker code, I would suggest using a require_relative for the config file. It could look something like this:

# frozen_string_literal: true

# ./shoryuken_config.rb

Shoryuken.configure_server do |config|
  # Server configuration here
end
# frozen_string_literal: true

# ./my_super_hard_worker.rb

require_relative "shoryuken_config.rb"

class MySuperHardWorker
  include Shoryuken::Worker
  shoryuken_options queue: 'default'

  def perform(sqs_msgs, events)
    # unbreakable code
  end
end

Then you should be able to run the executable as normal without needing a Rails initializer.

bundle exec shoryuken -q default -r ./my_super_hard_worker.rb
psulightning commented 1 year ago

I thought this pattern as well, but just wanted to confirm. Thank you @matt-taylor .