From a41e9979312e160ef42aaa3498ed576408230e91 Mon Sep 17 00:00:00 2001
From: Adam
Date: Thu, 31 Dec 2015 14:17:38 +0100
Subject: [PATCH] Execute task and getting output from rake
---
back-office/Gemfile | 1 +
.../app/controllers/items_controller.rb | 25 +++++++++++++++++--
back-office/app/helpers/items_helper.rb | 14 +++++++++++
back-office/app/models/item.rb | 3 ++-
.../app/views/items/export_import.html.erb | 4 +--
back-office/lib/tasks/ribica.rake | 3 +++
6 files changed, 45 insertions(+), 5 deletions(-)
diff --git a/back-office/Gemfile b/back-office/Gemfile
index 16ad7aa..cc75693 100644
--- a/back-office/Gemfile
+++ b/back-office/Gemfile
@@ -1,4 +1,5 @@
source 'https://rubygems.org'
+source 'http://gems.github.com'
gem 'rails_admin'
diff --git a/back-office/app/controllers/items_controller.rb b/back-office/app/controllers/items_controller.rb
index 8332681..c6f6994 100644
--- a/back-office/app/controllers/items_controller.rb
+++ b/back-office/app/controllers/items_controller.rb
@@ -48,13 +48,34 @@ class ItemsController < ApplicationController
@error_message = ""
@error_message = "Format of CSV is wrong (CSV content is empty)" if params[:csv_content] == ""
- csv_file = ItemsHelper::create_csv(CSV.parse(params[:csv_content]))
+ csv_parsed = CSV.parse(params[:csv_content])
+ csv_file = ItemsHelper::create_csv(csv_parsed)
+
+ @output = ""
+
begin
-
+ ENV["INPUT"] = csv_file.path
+
+ case params[:task]
+ when 'validate_items'
+ @output = ItemsHelper::execute_command("rake ribica:validate_items")
+ #ItemsHelper::execute_command("rake ribica:reindex")
+ when 'import_items'
+ @output = ItemsHelper::execute_command("rake ribica:import_items")
+ #ItemsHelper::execute_command("rake ribica:reindex")
+ when 'update_prices'
+ @output = ItemsHelper::execute_command("rake ribica:update_prices")
+ #ItemsHelper::execute_command("rake ribica:reindex")
+ else
+ @error_message = "There is no such task"
+ end
ensure
+ sleep 5
csv_file.unlink
end
+ @output = @output.join("
")
+
render :template => "items/export_import"
end
end
diff --git a/back-office/app/helpers/items_helper.rb b/back-office/app/helpers/items_helper.rb
index a330049..247dedb 100644
--- a/back-office/app/helpers/items_helper.rb
+++ b/back-office/app/helpers/items_helper.rb
@@ -10,4 +10,18 @@ module ItemsHelper
file.close
file
end
+
+ def self.execute_command(command)
+ buffer = []
+ Open3.popen3(command) do |stdin, stdout, stderr|
+ begin
+ while line = stdout.readline
+ buffer << line
+ end
+ rescue
+ end
+ end
+
+ buffer
+ end
end
diff --git a/back-office/app/models/item.rb b/back-office/app/models/item.rb
index c991c44..a8300c2 100644
--- a/back-office/app/models/item.rb
+++ b/back-office/app/models/item.rb
@@ -124,12 +124,13 @@ class Item < ActiveRecord::Base
end
end
puts "Nonexistent codes: "
- puts nonexistent_codes.join("\n")
+ puts "#{nonexistent_codes.join("\n")}"
end
def we_must_earn_money
if list_price.to_f <= current_input_price.to_f
errors[:list_price] << "#{code} Ulazna cijena veca od izlazne"
+ puts "#{code} Ulazna cijena veca od izlazne"
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 0b2ed4c..a3f1158 100644
--- a/back-office/app/views/items/export_import.html.erb
+++ b/back-office/app/views/items/export_import.html.erb
@@ -2,7 +2,7 @@
- <%= text_area_tag "csv_content", @csv_content, rows: 20, cols: 70 %>
+
<%= select_tag "task", options_for_select(@tasks) %>
@@ -15,5 +15,5 @@
-
+ <%= @output.html_safe %>
<% end %>
diff --git a/back-office/lib/tasks/ribica.rake b/back-office/lib/tasks/ribica.rake
index 1c7ed47..93b6891 100644
--- a/back-office/lib/tasks/ribica.rake
+++ b/back-office/lib/tasks/ribica.rake
@@ -223,6 +223,7 @@ def import_single_item(row, index, logger)
success = true
rescue Exception => e
logger.error "Could not import item on row number #{index} (#{row[lookup[:name_on_ribica]]}), reason: #{e}"
+ puts "Could not import item on row number #{index} (#{row[lookup[:name_on_ribica]]}), reason: #{e}"
success = false
end
@@ -281,8 +282,10 @@ def do_import(validate_only)
end
rescue Exception => e
puts "Import failed, please check the import log file for error details."
+ puts "Error while importing: #{e}"
logger.error "Error while importing: #{e}"
end
+ puts "Import done"
logger.info "Import done"
end