beck03076
beck03076

Reputation: 3308

Emailing text files as attachments from rake tasks, files are incomplete

I have a rake task that does a lot, but WRITES whatever it does on to a text file as below,

handler = File.open("cheese.txt", "a+")

handler.write("====Starting write!====\n")

handler

Now, Im catching the CTRL + C event as below,

Kernel.trap('INT') { 

 email_files # A method that cd to a PATH and attaches "cheese.txt" and use RAILS MAILERS to email 

 abort("Files Emailed, kernel trapped!")

}

Problem is, the delivered text files doesn't have any content the first time I do a CTRL+C, but from the next time it delivers it right.

Any Suggestions?

Upvotes: 0

Views: 68

Answers (1)

DNNX
DNNX

Reputation: 6255

Close the file after you added a new line:

File.open("cheese.txt", "a+") do |handler|
  handler.write("====Starting write!====\n")
end

UPD: http://www.ruby-doc.org/core-2.0/File.html#method-c-open:

With no associated block, File.open is a synonym for ::new. If the optional code block is given, it will be passed the opened file as an argument and the File object will automatically be closed when the block terminates.

Upvotes: 1

Related Questions