From 6b61afebc0a99a494d46f8ad3731c230080a5b07 Mon Sep 17 00:00:00 2001 From: "adam.harbas@a-net.ba" Date: Mon, 4 Jan 2016 09:56:40 +0100 Subject: [PATCH] Check if task succeeded and execute reindex task --- back-office/app/controllers/items_controller.rb | 13 +++++++++---- back-office/app/helpers/rake_tasks_helper.rb | 9 +++++++++ back-office/app/views/items/export_import.html.erb | 2 +- back-office/lib/tasks/ribica.rake | 3 +++ 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 back-office/app/helpers/rake_tasks_helper.rb diff --git a/back-office/app/controllers/items_controller.rb b/back-office/app/controllers/items_controller.rb index abf4ff2..096f0f2 100644 --- a/back-office/app/controllers/items_controller.rb +++ b/back-office/app/controllers/items_controller.rb @@ -36,6 +36,8 @@ class ItemsController < ApplicationController @tasks = [ ["Validate items", "validate_items"], ["Import items", "import_items"], ["Update prices", "update_prices"] ] + + @task = "validate_items" end def export_import_post @@ -43,6 +45,8 @@ class ItemsController < ApplicationController ["Import items", "import_items"], ["Update prices", "update_prices"] ] + @task = params[:task] + @csv_content = params[:csv_content] @error_message = "" @@ -63,16 +67,17 @@ class ItemsController < ApplicationController begin ENV["INPUT"] = csv_file.path - case params[:task] + case @task when 'validate_items' @output = ItemsHelper::execute_command("rake ribica:validate_items") - #ItemsHelper::execute_command("rake ribica:reindex") + @output.collect{|x| x.sub! "\n", "" } when 'import_items' @output = ItemsHelper::execute_command("rake ribica:import_items") - #ItemsHelper::execute_command("rake ribica:reindex") + @output.collect{|x| x.sub! "\n", "" } + ItemsHelper::execute_command("rake ribica:reindex") unless RakeTasksHelper::is_error_occurred when 'update_prices' @output = ItemsHelper::execute_command("rake ribica:update_prices") - #ItemsHelper::execute_command("rake ribica:reindex") + ItemsHelper::execute_command("rake ribica:reindex") else @error_message = "There is no such task" end diff --git a/back-office/app/helpers/rake_tasks_helper.rb b/back-office/app/helpers/rake_tasks_helper.rb new file mode 100644 index 0000000..e8b9815 --- /dev/null +++ b/back-office/app/helpers/rake_tasks_helper.rb @@ -0,0 +1,9 @@ +module RakeTasksHelper + def self.task_error_message + "Rake task execution error" + end + + def self.is_error_occurred(buffer) + buffer.include? self.task_error_message + end +end diff --git a/back-office/app/views/items/export_import.html.erb b/back-office/app/views/items/export_import.html.erb index 7a2ba0b..28e8a0d 100644 --- a/back-office/app/views/items/export_import.html.erb +++ b/back-office/app/views/items/export_import.html.erb @@ -5,7 +5,7 @@

- <%= select_tag "task", options_for_select(@tasks) %>

+ <%= select_tag "task", options_for_select(@tasks, @task) %>

<%= submit_tag "Run task" %>

diff --git a/back-office/lib/tasks/ribica.rake b/back-office/lib/tasks/ribica.rake index 93b6891..67b6b02 100644 --- a/back-office/lib/tasks/ribica.rake +++ b/back-office/lib/tasks/ribica.rake @@ -243,6 +243,7 @@ def do_import(validate_only) input_file = ENV['INPUT'] if input_file.to_s == "" puts "Input file is missing! Please provide input file in form INPUT=somefile.csv" + puts RakeTasksHelper.task_error_message return end @@ -275,6 +276,7 @@ def do_import(validate_only) if should_rollback puts "Import failed, please check the import log file for error details." logger.info "Rolling back because of errors" + puts RakeTasksHelper.task_error_message end raise ActiveRecord::Rollback @@ -284,6 +286,7 @@ def do_import(validate_only) puts "Import failed, please check the import log file for error details." puts "Error while importing: #{e}" logger.error "Error while importing: #{e}" + puts RakeTasksHelper.task_error_message end puts "Import done" logger.info "Import done"