Feat/spreadsheet import - #4
Merged
Merged
Conversation
…anitization methods
…CSV and XLSX imports
…ssion handling; add remote_avatar_url to UserSerializer and corresponding tests
…gs and entry size validation
…skipping, and error handling
…d corresponding tests
… workers for improved job handling
…mport updates; add corresponding tests
…d real-time progress updates
…badges, and system tests
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR 5: Asynchronous spreadsheet import with live progress
Admins upload a .csv or .xlsx of users; Solid Queue processes it in the
background and the progress bar updates over Solid Cable.
Resumability via Active Job Continuations
ProcessImportJobuses the Rails 8.1ActiveJob::ContinuableAPI. The jobdeclares three steps (
count_rows,import_rows,finalize) and checkpointsa row cursor every 100 rows. On graceful shutdown or interruption, Solid Queue
re-enqueues and the job resumes at the cursor rather than restarting at row 1.
Checkpointing every 100 rows rather than every row keeps the checkpoint writes
off the hot path; an interruption replays at most 99 rows, and those replays
are no-ops (see idempotency below).
Partial success, not all-or-nothing
There is no wrapping transaction. One malformed email on row 4,000 must not
discard 3,999 valid rows. Each row is validated independently and the outcome
tallied as created / skipped / failed, with rejected rows collected into
error_report.The tradeoff: a failed import leaves partial data behind. That is why the
Importrecord carries a full accounting of what happened rather than just astatus flag. The opposite choice (atomic, reject the whole file) is defensible
for a finance-grade import; for user onboarding, partial success is friendlier.
error_reportis capped at 500 entries withfailed_counttracking the realtotal, so a 50k-row file of garbage cannot write a 50k-element JSONB column
that then has to be serialized on every page render.
Idempotency does not depend on the cursor
A checkpoint can be lost between
step.set!and a crash, so row application isidempotent on its own:
find_or_initialize_by(email_address:)skips users thatalready exist. Rerunning any import is a no-op.
ActiveRecord::RecordNotUniqueis rescued as a skip, since find-then-save is check-then-act and loses races
against concurrent signups; the unique index is the real guarantee.
Consequence: this import cannot bulk-edit existing users. Intentional.
Memory
CSV.foreachandRoo#each_row_streaming, neverreadorparse.import.file.openstreams the blob to a tempfile rather thandownloadinto a String. A 50k-row upload must not be a memory event on a512 MB Fargate task.
Broadcast suppression
Without a guard, a 10k-row import fires 10k dashboard broadcasts through the
PR 4
after_commithook.DashboardBroadcasts.suppresswraps the row loopusing
ActiveSupport::IsolatedExecutionState(fiber-aware, unlikeThread.current), andfinalizefires exactly one dashboard broadcast.Import progress broadcasts are batched at the same 100-row cadence.
Progress broadcasts carry a signal, not a payload, for the same authorization
reason as PR 4.
Security notes
= + - @stripped on ingest. A cell containing=HYPERLINK(...)that round-trips back out to a spreadsheet is a liveformula injection; stripping on the way in beats remembering to escape on
every export path.
Zip.setup { validate_entry_sizes = true }because xlsx is a zip archive anddecompression bombs are an upload vector.
SecureRandompassword and cannot sign in until theyrun a password reset. Production would send an invitation token instead.
Why row-by-row and not
insert_allRoughly 50x slower, and chosen anyway:
insert_allbypasses validations,has_secure_password,normalizes, and Active Record encryption onemail_address. Encrypted columns cannot be batch-inserted without hand-rollingciphertext. Measured at ~2,100 rows/sec locally, which is comfortably inside
the resumable-job envelope.
Infrastructure
Imports run on a dedicated single-threaded
importsqueue. Sharingdefaultmeans a large import starves password reset mails behind it.
Test plan
bundle exec rspec spec/imports spec/jobs spec/channels/import_channel_spec.rb spec/requests/admin/imports_spec.rbworker mid-run, restart it, confirm it resumes and the final count is correct.