From a87c43e999a05f9a6e7d358e2e204a71b959d6db Mon Sep 17 00:00:00 2001 From: zmagod Date: Thu, 4 Jan 2018 14:49:52 +0100 Subject: [PATCH 1/3] nil image guard [fixes SCI-1907] --- lib/tasks/tiny_mce_asset.rake | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/tasks/tiny_mce_asset.rake b/lib/tasks/tiny_mce_asset.rake index a668cee5b..20c01033a 100644 --- a/lib/tasks/tiny_mce_asset.rake +++ b/lib/tasks/tiny_mce_asset.rake @@ -21,6 +21,9 @@ namespace :tiny_mce_asset do step.description.gsub!(regex) do |el| match = el.match(regex) old_img = TinyMceAsset.find_by_id(match[1]) + # skip other processing and deletes tiny_mce tag + # if image is not in database + next unless old_img new_img = TinyMceAsset.create(image: old_img.image, team: team, reference: step) @@ -47,6 +50,9 @@ namespace :tiny_mce_asset do result_text.text.gsub!(regex) do |el| match = el.match(regex) old_img = TinyMceAsset.find_by_id(match[1]) + # skip other processing and deletes tiny_mce tag + # if image is not in database + next unless old_img new_img = TinyMceAsset.create(image: old_img.image, team: team, reference: result_text) From 498f5eec13b59290a1f1ba890d05e9c19d388530 Mon Sep 17 00:00:00 2001 From: zmagod Date: Fri, 5 Jan 2018 13:23:58 +0100 Subject: [PATCH 2/3] add params to task so we can restart the script from the failed attempt in case it happens --- lib/tasks/tiny_mce_asset.rake | 46 +++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/lib/tasks/tiny_mce_asset.rake b/lib/tasks/tiny_mce_asset.rake index 20c01033a..780dc4bf9 100644 --- a/lib/tasks/tiny_mce_asset.rake +++ b/lib/tasks/tiny_mce_asset.rake @@ -7,17 +7,27 @@ namespace :tiny_mce_asset do nil, nil, 7.days.ago).destroy_all end - desc 'Generate new tiny_mce_assets and replace old assets in RTE' - task regenerate_images: :environment do + desc 'Generate new tiny_mce_assets and replace old assets in RTE for ' \ + 'steps. Assign the last printed id if the script crashes or ' \ + 'id + 1 if there is a problematic asset' + task :regenerate_step_images, [:last_id] => :environment do |_, args| regex = /\[~tiny_mce_id:([0-9a-zA-Z]+)\]/ replaced_images = 0 failed_attempts = 0 - all_images = TinyMceAsset.count + all_images = TinyMceAsset.where('step_id IS NOT NULL').count failed_attempts_ids = [] puts 'Start processing steps...' - Step.find_each do |step| + params = { batch_size: 100 } + if args.present? && args[:last_id].present? + # fetch all steps and sort them asc + params[:start] = args[:last_id].to_i + end + Step.find_each(batch_size: 100) do |step| next unless step.description && step.description.match(regex) team = step.protocol.team + puts "******************************* \n\n\n\n" + puts "Processing step id => [#{step.id}] \n\n\n\n" + puts '*******************************' step.description.gsub!(regex) do |el| match = el.match(regex) old_img = TinyMceAsset.find_by_id(match[1]) @@ -43,10 +53,36 @@ namespace :tiny_mce_asset do step.save end puts 'Completed processing steps...' + + puts '----------- TASK REPORT -----------------' + puts "All images: #{all_images}" + puts "Recreated images: #{replaced_images}" + puts "Failed attempts: #{failed_attempts}" + puts "TinyMceAsset ids of failed attempts: #{failed_attempts_ids}" + puts '-----------------------------------------' + end + + desc 'Generate new tiny_mce_assets and replace old assets in RTE ' \ + 'for results. Assign the last printed id if the script crashes or ' \ + 'id + 1 if there is a problematic asset' + task :regenerate_results_images, [:last_id] => :environment do |_, args| + regex = /\[~tiny_mce_id:([0-9a-zA-Z]+)\]/ + replaced_images = 0 + failed_attempts = 0 + all_images = TinyMceAsset.where('result_text_id IS NOT NULL').count + failed_attempts_ids = [] + params = { batch_size: 100 } + if args.present? && args[:last_id].present? + params[:start] = args[:last_id].to_i + end + puts 'Start processing result_texts...' - ResultText.find_each do |result_text| + ResultText.find_each(params) do |result_text| next unless result_text.text && result_text.text.match(regex) team = result_text.result.my_module.protocol.team + puts "******************************************* \n\n\n\n" + puts "Processing result_text id => [#{result_text.id}] \n\n\n\n" + puts '*******************************************' result_text.text.gsub!(regex) do |el| match = el.match(regex) old_img = TinyMceAsset.find_by_id(match[1]) From 11e47bbfc3c6a42ee3d41df92c59a6012e79e969 Mon Sep 17 00:00:00 2001 From: zmagod Date: Fri, 5 Jan 2018 15:37:58 +0100 Subject: [PATCH 3/3] follow @duco 's suggestions --- lib/tasks/tiny_mce_asset.rake | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/tasks/tiny_mce_asset.rake b/lib/tasks/tiny_mce_asset.rake index 780dc4bf9..ecd64d88b 100644 --- a/lib/tasks/tiny_mce_asset.rake +++ b/lib/tasks/tiny_mce_asset.rake @@ -1,4 +1,5 @@ namespace :tiny_mce_asset do + REGEX = /\[~tiny_mce_id:([0-9a-zA-Z]+)\]/ desc 'Remove obsolete images that were created on new steps or '\ 'results and the step/result didn\'t get saved.' task remove_obsolete_images: :environment do @@ -11,10 +12,9 @@ namespace :tiny_mce_asset do 'steps. Assign the last printed id if the script crashes or ' \ 'id + 1 if there is a problematic asset' task :regenerate_step_images, [:last_id] => :environment do |_, args| - regex = /\[~tiny_mce_id:([0-9a-zA-Z]+)\]/ replaced_images = 0 failed_attempts = 0 - all_images = TinyMceAsset.where('step_id IS NOT NULL').count + all_images = TinyMceAsset.where.not(step: nil).count failed_attempts_ids = [] puts 'Start processing steps...' params = { batch_size: 100 } @@ -22,14 +22,14 @@ namespace :tiny_mce_asset do # fetch all steps and sort them asc params[:start] = args[:last_id].to_i end - Step.find_each(batch_size: 100) do |step| - next unless step.description && step.description.match(regex) + Step.find_each(params) do |step| + next unless step.description && step.description.match(REGEX) team = step.protocol.team puts "******************************* \n\n\n\n" puts "Processing step id => [#{step.id}] \n\n\n\n" puts '*******************************' - step.description.gsub!(regex) do |el| - match = el.match(regex) + step.description.gsub!(REGEX) do |el| + match = el.match(REGEX) old_img = TinyMceAsset.find_by_id(match[1]) # skip other processing and deletes tiny_mce tag # if image is not in database @@ -66,10 +66,9 @@ namespace :tiny_mce_asset do 'for results. Assign the last printed id if the script crashes or ' \ 'id + 1 if there is a problematic asset' task :regenerate_results_images, [:last_id] => :environment do |_, args| - regex = /\[~tiny_mce_id:([0-9a-zA-Z]+)\]/ replaced_images = 0 failed_attempts = 0 - all_images = TinyMceAsset.where('result_text_id IS NOT NULL').count + all_images = TinyMceAsset.where.not(result_text: nil).count failed_attempts_ids = [] params = { batch_size: 100 } if args.present? && args[:last_id].present? @@ -78,13 +77,13 @@ namespace :tiny_mce_asset do puts 'Start processing result_texts...' ResultText.find_each(params) do |result_text| - next unless result_text.text && result_text.text.match(regex) + next unless result_text.text && result_text.text.match(REGEX) team = result_text.result.my_module.protocol.team puts "******************************************* \n\n\n\n" puts "Processing result_text id => [#{result_text.id}] \n\n\n\n" puts '*******************************************' - result_text.text.gsub!(regex) do |el| - match = el.match(regex) + result_text.text.gsub!(REGEX) do |el| + match = el.match(REGEX) old_img = TinyMceAsset.find_by_id(match[1]) # skip other processing and deletes tiny_mce tag # if image is not in database