[FIX]: Unnecessary Logs, Preventing System Inefficiencies and Inefficient Retries While Populating Dashboard Database #6078
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.
Issue Overview
When running the
rake populate:dev
task, the system was encountering aActiveRecord::RecordNotUnique
error due to duplicate entries in thecampaigns_courses
table. This error led to the creation of excessive logs, making it difficult to analyze system behavior and troubleshoot issues efficiently.Additionally, if a duplicate record is encountered for one entry during the execution of the populate_dashboard task, the process halts further attempts to populate subsequent entries. This causes the database to remain partially populated, failing to insert other valid records.
The presence of a single pre-existing record in the database could arise if the user terminates the
populate:dev
command prematurely. This interruption might happen because the process takes too long to fetch and insert data, leading to an incomplete population of demo records.Closes #6079
Proposed Solution
To address the issues with the
populate:dev
task and ensure a smoother data population process, the following solutions were implemented:Handle Duplicate Records Gracefully:
Adjusted the logic to check for existing entries before creating new records. This prevents duplicate records from causing the process to fail with
ActiveRecord::RecordNotUnique
errors.Continue Processing After Errors:
Introduced exception handling to ensure that encountering an error (e.g., a duplicate record) does not stop the task. Instead, the task skips the problematic entry and continues processing subsequent entries, ensuring the database is populated with all valid records.
Avoid Redundant Associations:
Added checks to avoid re-associating courses that already exist in a campaign, eliminating unnecessary database operations and log noise.
Improved Logging and Error Reporting:
Enhanced logging to provide clear, actionable error messages when issues arise, improving the debugging experience.
Changes Made
make_copy_of
function to verify the existence of records (e.g., courses, users, wikis) before attempting to create them.begin-rescue
blocks to catch and log errors likeActiveRecord::RecordInvalid
and other unexpected issues.Logging Improvements:
Added detailed error and progress messages using
Rails.logger
andputs
to provide better visibility into the task execution.Database Population Flow:
Screenshots -
Before -
After -