Execute task and getting output from rake
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
source 'https://rubygems.org'
|
source 'https://rubygems.org'
|
||||||
|
source 'http://gems.github.com'
|
||||||
|
|
||||||
|
|
||||||
gem 'rails_admin'
|
gem 'rails_admin'
|
||||||
|
|||||||
@@ -48,13 +48,34 @@ class ItemsController < ApplicationController
|
|||||||
@error_message = ""
|
@error_message = ""
|
||||||
@error_message = "Format of CSV is wrong (CSV content is empty)" if params[:csv_content] == ""
|
@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
|
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
|
ensure
|
||||||
|
sleep 5
|
||||||
csv_file.unlink
|
csv_file.unlink
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@output = @output.join("<br/>")
|
||||||
|
|
||||||
render :template => "items/export_import"
|
render :template => "items/export_import"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -10,4 +10,18 @@ module ItemsHelper
|
|||||||
file.close
|
file.close
|
||||||
file
|
file
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -124,12 +124,13 @@ class Item < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
puts "Nonexistent codes: "
|
puts "Nonexistent codes: "
|
||||||
puts nonexistent_codes.join("\n")
|
puts "#{nonexistent_codes.join("\n")}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def we_must_earn_money
|
def we_must_earn_money
|
||||||
if list_price.to_f <= current_input_price.to_f
|
if list_price.to_f <= current_input_price.to_f
|
||||||
errors[:list_price] << "#{code} Ulazna cijena veca od izlazne"
|
errors[:list_price] << "#{code} Ulazna cijena veca od izlazne"
|
||||||
|
puts "#{code} Ulazna cijena veca od izlazne"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<p><label for="codes">
|
<p><label for="codes">
|
||||||
CSV content goes here:
|
CSV content goes here:
|
||||||
</label><br />
|
</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>
|
<p><label for="task">Task: </label>
|
||||||
<%= select_tag "task", options_for_select(@tasks) %></p>
|
<%= select_tag "task", options_for_select(@tasks) %></p>
|
||||||
@@ -15,5 +15,5 @@
|
|||||||
|
|
||||||
<br />
|
<br />
|
||||||
<p><label for="output_area">Output area: </label>
|
<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 %>
|
<% end %>
|
||||||
|
|||||||
@@ -223,6 +223,7 @@ def import_single_item(row, index, logger)
|
|||||||
success = true
|
success = true
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
logger.error "Could not import item on row number #{index} (#{row[lookup[:name_on_ribica]]}), reason: #{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
|
success = false
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -281,8 +282,10 @@ def do_import(validate_only)
|
|||||||
end
|
end
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
puts "Import failed, please check the import log file for error details."
|
puts "Import failed, please check the import log file for error details."
|
||||||
|
puts "Error while importing: #{e}"
|
||||||
logger.error "Error while importing: #{e}"
|
logger.error "Error while importing: #{e}"
|
||||||
end
|
end
|
||||||
|
puts "Import done"
|
||||||
logger.info "Import done"
|
logger.info "Import done"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user