Check if task succeeded and execute reindex task

This commit is contained in:
adam.harbas@a-net.ba
2016-01-04 09:56:40 +01:00
parent 3fc4a9edea
commit 6b61afebc0
4 changed files with 22 additions and 5 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -5,7 +5,7 @@
<textarea id="csv_content" name="csv_content" rows=20 style="width: 100%"><%= @csv_content %></textarea></p>
<p><label for="task">Task: </label>
<%= select_tag "task", options_for_select(@tasks) %></p>
<%= select_tag "task", options_for_select(@tasks, @task) %></p>
<p><%= submit_tag "Run task" %></p>

View File

@@ -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"