Skip to content

Conversation

@syounglee012
Copy link
Contributor

Link to Jira ticket

Purpose

Fix NoMethodError: undefined method 'each' for nil:NilClass that occurs when redis.smembers() returns nil instead of an empty array in the touch_courses method.

This error has been occurring every 10 minutes since 2026-01-29T17:42 on cdcr.strongmind.com only, with 36+ occurrences. The periodic job "Ten minute course touch" fails when Redis connectivity issues cause smembers to return nil.

Related Sentry issue: CANVAS-LMS-2WJ

Approach

Add defensive coding to handle Redis edge cases:

  1. Nil coalescing - redis.smembers(key) || [] to handle nil responses
  2. Course existence check - Course.find_by_id(member) with nil guard to handle deleted courses
  3. Error handling - rescue Redis::BaseError to catch connection errors gracefully
  4. Logging - Warn for stale course references, error for Redis failures
  5. Sentry integration - Report Redis errors to Sentry for visibility

Testing

Added comprehensive unit tests covering:

  • ✅ Normal operation with valid course IDs
  • redis.smembers returns nil (the bug scenario)
  • redis.smembers returns empty array
  • ✅ Course not found (stale reference cleanup)
  • ✅ Redis connection errors

Run tests:

bundle exec rspec spec/decorators/models/course_decorator_spec.rb

Infrastructure Note

⚠️ Ops team investigation needed:

The error only affects cdcr.strongmind.com and no cdcr-specific Redis cluster was found in AWS ElastiCache monitoring. Please verify:

  1. What is the REDIS_SERVER env var value for cdcr?
  2. Is the Redis endpoint reachable from cdcr workers?

Screenshots/Video

N/A - Backend fix, no UI changes.

Add defensive coding to handle cases where redis.smembers() returns
nil instead of an empty array, which was causing NoMethodError in
production (cdcr.strongmind.com only).

Changes:
- Add nil coalescing (|| []) to handle nil from smembers
- Add nil check for Course.find_by_id to handle deleted courses
- Wrap Redis operations in begin/rescue for Redis::BaseError
- Log warnings for stale course references
- Log errors and report to Sentry for Redis connection issues

Fixes CANVAS-LMS-2WJ

Co-authored-by: Cursor <cursoragent@cursor.com>
@syounglee012 syounglee012 requested a review from a team as a code owner January 29, 2026 23:37
syounglee012 and others added 5 commits January 29, 2026 16:50
Redis gem is not available in canvas_shim test environment since it's
provided by the host Canvas application. Add stub classes to allow
tests to run in CI.

Co-authored-by: Cursor <cursoragent@cursor.com>
- Extract process_course_touch method for pure logic testing
- Separate Redis dependency from business logic
- Add unit tests without Rails dependency
@syounglee012 syounglee012 merged commit cc5dfc1 into main Jan 30, 2026
3 checks passed
@syounglee012 syounglee012 deleted the QTY-18984 branch January 30, 2026 22:55
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

Successfully merging this pull request may close these issues.

2 participants