canvas-lms/gems/request_context
Cody Cutrer a468db2f6c bundle update redis-client
Change-Id: Ie8b0e0cb6730abd60e89836b7777261608c4ff1c
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/335848
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Isaac Moore <isaac.moore@instructure.com>
Build-Review: Isaac Moore <isaac.moore@instructure.com>
QA-Review: Cody Cutrer <cody@instructure.com>
Product-Review: Cody Cutrer <cody@instructure.com>
2024-01-05 21:24:42 +00:00
..
lib Rubocop for ruby 3.1 2023-06-06 16:44:26 +00:00
spec switch from byebug to debug 2023-09-20 23:48:39 +00:00
Gemfile rubocop: fix miscelleneuos violations in prep of bumping rubocop 2023-04-12 19:26:13 +00:00
Gemfile.lock bundle update redis-client 2024-01-05 21:24:42 +00:00
README.md request_context as a gem 2021-03-02 23:02:40 +00:00
request_context.gemspec switch from byebug to debug 2023-09-20 23:48:39 +00:00
test.sh request_context as a gem 2021-03-02 23:02:40 +00:00

README.md

RequestContext

Keeping the state for canvas requests.

Usage

RequestContext is a combination of middleware for rails and a kind of global "what is my current state" access from anywhere.

Generator

RequestContext::Generator, when added to the middleware stack for an application, makes sure that every request has a request_context_id on it:

# application.rb
initializer("extend_middleware_stack") do |app|
  app.config.middleware.swap(ActionDispatch::RequestId, RequestContext::Generator)\
  app.config.middleware.insert_after(config.session_store, RequestContext::Session)
end

Why replace the build in RequestId middleware? this one reads the incoming environment for potentially provided request IDs from other services in the 'HTTP_X_REQUEST_CONTEXT_ID' header. This class also provides an interface for accessing the current request id from pretty much anywhere in the app without having to know where it's stored (we use the thread context):

RequestContext::Generator.request_id

In order for this to work, you need the cookie jar to have the right data loaded into it, which you can find being written in the RequestContext::Session middleware. The reason is middleware ordering, the Generator runs after the cookie jar has already been written to on the way out.

You need to use both.

Running Tests

This gem is tested with rspec. You can use test.sh to run it, or do it yourself with bundle exec rspec spec.