Rails 7: Issues with Custom Middleware Affecting ActiveJob Execution Order
I'm trying to implement Quick question that's been bugging me - I'm attempting to set up This might be a silly question, but I'm working on a personal project and I'm encountering unexpected behavior with a custom middleware I've implemented in my Rails 7 application..... The middleware is designed to log job execution times for ActiveJob jobs, but it seems to interfere with the job execution order when using a Sidekiq backend. Specifically, when I enqueue multiple jobs, the logs indicate that jobs are finishing in an order that doesn't match their enqueue order, leading to confusion in tracking performance. Here's a snippet of my middleware: ```ruby class JobTimingMiddleware def call(worker, job, queue) start_time = Time.now yield duration = Time.now - start_time Rails.logger.info "Job #{job['class']} took #{duration.round(2)} seconds" end end ``` I have registered this middleware in my Sidekiq initializer like this: ```ruby Sidekiq.configure_server do |config| config.server_middleware do |chain| chain.add JobTimingMiddleware end end ``` When I run my tests with multiple jobs, I see log entries like this: ``` Job MyJob1 took 2.3 seconds Job MyJob2 took 1.8 seconds Job MyJob3 took 2.0 seconds ``` However, the expected order based on enqueueing is not reflected in the logs. I've tried adding more logging statements within the middleware to determine if other jobs are being enqueued concurrently, but they don't seem to provide clarity on the execution order. I also reviewed the Sidekiq documentation and confirmed that I'm not using any concurrency options that would affect job execution order. Can anyone point out what might be causing this issue, or share suggestions on how to ensure that my middleware doesn't interfere with job execution order? What's the best practice here? My development environment is Ubuntu. What am I doing wrong? This is happening in both development and production on Windows 10.