ElMassimo / ElMassimo/request_store_rails

When a new sidekiq job is created inside a sidekiq job

Open
#12 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Ruby
Stars
85
Forks
6
PR merge metrics
No merged PRs in 30d

Description

Hi All

When pushing a job to Sidekiq, configured the client middleware to pass metadata in 'key_value_hash' that is needed while processing the job

```
class SidekiqRequestStoreClientMiddleware
def call(worker_class, job, queue, redis_pool)
binding.pry
job['key_value_hash'] = RequestLocals.fetch(:key_value_hash) rescue nil
job['current_store_id'] = RequestLocals.current_store_id rescue nil
yield
end
end
```
```
class SidekiqRequestStoreServerMiddleware
def call(_worker, job, _queue)
##TODO Handle retries
create_instance(job)
yield
ensure
RequestLocals.clear!
RequestLocals.set_current_store_id(nil)
end

def create_instance(job)
RequestLocals.set_current_store_id(job['jid'])
key_value_hash = job['key_value_hash']

key_value_hash.each do |key, value|
RequestLocals.store[key.to_sym] = value
end

end
end
```

This seems to fail when a sidekiq job is created inside an another sidekiq job, because this time sidekiq server acts as a client. To handle this below code was added in config.

```
Sidekiq.configure_server do |config|
config.client_middleware do |chain|
chain.add SidekiqRequestStoreClientMiddleware
end
config.server_middleware do |chain|
chain.add SidekiqRequestStoreServerMiddleware
end
end
```
Still facing issue to access RequestLocals in nested sidekiq job

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing a Sidekiq job that creates another job, then trace SidekiqRequestStoreClientMiddleware and SidekiqRequestStoreServerMiddleware through the nested-job path. Done means RequestLocals metadata remains available when the nested job is processed.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.