From f9832013b0f81c7815c43a00f2b1f92670fcd69c Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Wed, 17 Apr 2019 12:06:46 +0200 Subject: [PATCH] Improve experiment preview image generation [SCI-3177] --- app/controllers/canvas_controller.rb | 2 +- app/controllers/experiments_controller.rb | 8 ++++++-- app/models/experiment.rb | 10 ++++++---- app/models/my_module.rb | 2 +- .../experiments/copy_experiment_as_template_service.rb | 2 +- .../experiments/generate_workflow_image_service.rb | 2 -- app/services/team_importer.rb | 2 +- app/utilities/first_time_data_generator.rb | 2 +- 8 files changed, 17 insertions(+), 13 deletions(-) diff --git a/app/controllers/canvas_controller.rb b/app/controllers/canvas_controller.rb index 05ebfe730..ede012a2e 100644 --- a/app/controllers/canvas_controller.rb +++ b/app/controllers/canvas_controller.rb @@ -186,7 +186,7 @@ class CanvasController < ApplicationController end # Create workflow image - @experiment.delay.generate_workflow_img + @experiment.generate_workflow_img flash[:success] = t('experiments.canvas.update.success_flash') redirect_to canvas_experiment_path(@experiment) diff --git a/app/controllers/experiments_controller.rb b/app/controllers/experiments_controller.rb index cffd51cf4..ad77bf398 100644 --- a/app/controllers/experiments_controller.rb +++ b/app/controllers/experiments_controller.rb @@ -237,10 +237,14 @@ class ExperimentsController < ApplicationController end def updated_img + if @experiment.workflowimg.present? && !@experiment.workflowimg.exists? + @experiment.workflowimg = nil + @experiment.save + @experiment.generate_workflow_img + end respond_to do |format| format.json do - if @experiment.workflowimg_updated_at.to_i >= - params[:timestamp].to_time.to_i + if @experiment.workflowimg.present? render json: {}, status: 200 else render json: {}, status: 404 diff --git a/app/models/experiment.rb b/app/models/experiment.rb index 2ee527e66..d730b06d7 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -49,8 +49,6 @@ class Experiment < ApplicationRecord experiment.validates :archived_on, presence: true end - after_touch { update_column(:workflowimg_updated_at, updated_at) } - scope :is_archived, ->(is_archived) { where("archived = ?", is_archived) } def self.search( @@ -222,7 +220,11 @@ class Experiment < ApplicationRecord end def generate_workflow_img - Experiments::GenerateWorkflowImageService.call(experiment_id: id) + if workflowimg.present? + self.workflowimg = nil + save + end + Experiments::GenerateWorkflowImageService.delay.call(experiment_id: id) end # Get projects where user is either owner or user in the same team @@ -423,7 +425,7 @@ class Experiment < ApplicationRecord # to other experiment def generate_workflow_img_for_moved_modules(to_move) Experiment.where(id: to_move.values.uniq).each do |exp| - exp.delay.generate_workflow_img + exp.generate_workflow_img end end diff --git a/app/models/my_module.rb b/app/models/my_module.rb index f8d726537..914c018fd 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -184,7 +184,7 @@ class MyModule < ApplicationRecord raise ActiveRecord::Rollback end end - experiment.delay.generate_workflow_img + experiment.generate_workflow_img restored end diff --git a/app/services/experiments/copy_experiment_as_template_service.rb b/app/services/experiments/copy_experiment_as_template_service.rb index c49e28e12..5a708ce7b 100644 --- a/app/services/experiments/copy_experiment_as_template_service.rb +++ b/app/services/experiments/copy_experiment_as_template_service.rb @@ -43,7 +43,7 @@ module Experiments @errors.merge!(@c_exp.errors.to_hash) unless @c_exp.valid? @c_exp = nil unless succeed? - @c_exp.delay.generate_workflow_img if succeed? + @c_exp.generate_workflow_img if succeed? track_activity if succeed? self diff --git a/app/services/experiments/generate_workflow_image_service.rb b/app/services/experiments/generate_workflow_image_service.rb index 71ca146dc..725f67be8 100644 --- a/app/services/experiments/generate_workflow_image_service.rb +++ b/app/services/experiments/generate_workflow_image_service.rb @@ -80,8 +80,6 @@ module Experiments file.close @exp.save @exp.touch(:workflowimg_updated_at) - rescue StandardError => ex - logger.error ex.message end end end diff --git a/app/services/team_importer.rb b/app/services/team_importer.rb index 0535f98eb..4238d7089 100644 --- a/app/services/team_importer.rb +++ b/app/services/team_importer.rb @@ -565,7 +565,7 @@ class TeamImporter my_module_group.save! @my_module_group_mappings[orig_module_group_id] = my_module_group.id end - experiment.delay.generate_workflow_img + experiment.generate_workflow_img create_my_modules(experiment_json['my_modules'], experiment, user_id) experiment end diff --git a/app/utilities/first_time_data_generator.rb b/app/utilities/first_time_data_generator.rb index e2a08b316..40db93395 100644 --- a/app/utilities/first_time_data_generator.rb +++ b/app/utilities/first_time_data_generator.rb @@ -1398,7 +1398,7 @@ module FirstTimeDataGenerator ) # create thumbnail - experiment.delay.generate_workflow_img + experiment.generate_workflow_img end # Used for delayed jobs