ruby-on-railsrubydelayed-jobrails-activejob

How to properly pass "request" params in ActiveJob rails?


I would like to pass on the request params so that I can execute it on the delayed_job as follows:

ObserveJob.perform_later(request)

but the error is always ActiveJob::SerializationError: Unsupported argument type: ActionDispatch::Request

Is there a way to pass on this built-in request params in rails so it will execute properly using delayed_job?


Solution

  • Instead of passing the whole request object – which is quite big and complex – only pass the attributes you actually need to the job. This has several advantages:

    I would change the perform method to

    def perform(path:, referer:, url: remote_ip:, user_agent:, location:)
      # ...
    end
    

    and call it like this:

    ObserveJob.perform_later(
      path:       request.path,
      referer:    request.referer,
      url:        request.url,
      remote_ip:  request.remote_ip,
      user_agent: request.user_agent,
      location:   request.location
    )