Skip to content

Instantly share code, notes, and snippets.

@fmontes86
Last active August 29, 2015 13:58
Show Gist options
  • Save fmontes86/9956924 to your computer and use it in GitHub Desktop.
Save fmontes86/9956924 to your computer and use it in GitHub Desktop.
Delayed_job implementaion with smarter_csv and fork creation by chunks
def load_imported_campaign
total_chunks = SmarterCSV.process(file.path, {:chunk_size => 2, :strings_as_keys => true, :remove_unmapped_keys => true, :col_sep => "\t"}) do |chunk|
chunk.each_with_index do |h, i|
forkPID = fork do
p "Start new FORK with PID: #{forkPID} on ROW: #{i}"
# ESTO ES PARA EL USUARIO
# user = User.where(email: row["General-Email"]).first_or_create
# user.update(email: row["General-Email"])
# user.save!
h.each do |column|
col_value = column[1]
group = column[0].split("-")[0]
link = column[0].split("-")[1]
save_association(group, link, col_value)
end
end
p "Finished FORK with PID = #{forkPID}"
Process.wait
end
end
end
handle_asynchronously :load_imported_campaign
def save_association(group, link, col_value)
case group
when "General" then GeneralImport.create(name: link, value: col_value, campaign_import_id: self.id)
when "Footer" then FooterImport.create(name: link, value: col_value, campaign_import_id: self.id)
when "Section" then SectionImport.create(name: link, value: col_value, campaign_import_id: self.id)
end
end
...
def create
@campaign_import = CampaignImport.new(campaing_import_params)
if @campaign_import.save
flash[:success] = "Se cargó el archivo perfectamente" if @campaign_import.load_imported_campaign
redirect_to campaign_imports_path
else
flash[:error] = []
@campaign_import.errors.full_messages.each {|msg| flash[:error] << msg }
render :new
end
end
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment