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

How to Inject code into worker initialization? #477

Open
raymzag opened this issue Apr 18, 2023 · 0 comments
Open

How to Inject code into worker initialization? #477

raymzag opened this issue Apr 18, 2023 · 0 comments

Comments

@raymzag
Copy link

raymzag commented Apr 18, 2023

Is there a better way than this if I want to inject a third party api client into the worker process? I have tried before_fork and after_fork hooks, but not working in my use case, because the api client will spawn child process to update its own cache from remote api. If I put Client.new in the main sneaker thread (or before/after fork), the cache updates do not get propagated to worker MyWorker processes. So, I resorted to code below, which seems to be working. But I am wondering if there's better ways?

class MyWorker
  include Sneakers::Worker
  attr_reader :my_client

  def initialize(...)
    super(...)
    @my_client = Client.new(...)
  end

  from_queue 'myworker'

  def work(data)
    puts "executing at #{Time.now}"
    puts my_client.method()
    ack!
  end
end
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

1 participant