Created
January 14, 2010 21:55
-
-
Save ryenski/277544 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Export < ActiveRecord::Base | |
include ActionView::Helpers::DateHelper | |
include ActionView::Helpers::NumberHelper | |
has_attached_file :attachment, :storage => :s3 | |
belongs_to :organization | |
belongs_to :user | |
after_create :process | |
def process | |
send_later(:process!) | |
end | |
def process! | |
return if finished? | |
begin | |
update_attribute(:state, "running") | |
csv = tempfile | |
@joins = [:names, :addresses, :email_addresses, :web_addresses] | |
smart_tag = organization.smart_tags.find(settings[:smart_tag]) | |
@conditions = smart_tag.conditions | |
headers, n_names, n_addresses, n_email_addresses, n_phone_numbers, n_web_addresses = organization.personas.csv_headers | |
csv.puts headers.to_csv | |
@row_counter = 0 | |
@lasttime = Time.now | |
@starttime = Time.now | |
@count = organization.personas.count(:conditions => @conditions) | |
organization.personas.find_each(:conditions => @conditions, :include => @joins) do |persona| | |
csv.puts persona.to_csv(n_names, n_addresses, n_email_addresses, n_phone_numbers, n_web_addresses) | |
# debugging...{} | |
@row_counter += 1 | |
if @row_counter%100 == 0 | |
currtime = Time.now | |
str = "" | |
str << "#{(@row_counter/@count.to_f * 100).to_i}% complete " | |
str << "(#{@row_counter} of #{@count} records). " | |
str << "Elapsed: #{distance_of_time_in_words_to_now(@starttime)} (#{"%0.2f" % (currtime - @lasttime)}sec/h). " | |
memory_usage = `ps -o rss= -p #{Process.pid}`.to_i # in kilobytes | |
str << "Memory used: #{number_to_human_size(memory_usage*1000)}. " | |
self.update_attribute(:detail, str) | |
@lasttime = Time.now | |
# csv.flush | |
end | |
end | |
# Send to S3 | |
self.attachment = csv | |
self.state = "finished" | |
self.detail = "Processed in #{distance_of_time_in_words_to_now(@starttime)}" | |
self.save! | |
# send email | |
UserNotifier.deliver_finished_export(self) | |
rescue Exception => e | |
notify_hoptoad(e) | |
self.update_attribute(:state, "failed") | |
raise e | |
end | |
end | |
def tempfile | |
@tempfile ||= Tempfile.new(filename) | |
end | |
def filename | |
"#{id}.#{format}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment