Execute task and getting output from rake

This commit is contained in:
Adam
2015-12-31 14:17:38 +01:00
parent ca91c3d16f
commit a41e997931
6 changed files with 45 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
source 'https://rubygems.org'
source 'http://gems.github.com'
gem 'rails_admin'

View File

@@ -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("<br/>")
render :template => "items/export_import"
end
end

View File

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

View File

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

View File

@@ -2,7 +2,7 @@
<p><label for="codes">
CSV content goes here:
</label><br />
<%= text_area_tag "csv_content", @csv_content, rows: 20, cols: 70 %></p>
<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>
@@ -15,5 +15,5 @@
<br />
<p><label for="output_area">Output area: </label>
<div id="output_area" style="width: 36.4em;height: 14em;outline: 1px solid #A9A9A9;"></div>
<div id="output_area" style="width: 100%;height: 14em;outline: 1px solid #A9A9A9; overflow: scroll"><%= @output.html_safe %></div>
<% end %>

View File

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