[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

logging with rake

Simon Strandgaard

1/26/2006 9:12:00 PM

Hi list,

Howto capture stdout + stderr from jobs invoked with rake?


I run rake from cron. I have a

def log(msg)
str = Time.now.to_s + ": " + msg + "\n"
file = $settings['logfile']
File.open(file, 'a+') {|f| f.write str }
end

it only tells where it went wrong, but not what went wrong.





any ideas how to do better logging with the following code?

--
Simon Strandgaard



desc "creates a hotcopy backup of the repository."
task :backup => [:clean] do
ok = true

log('create hotcopy')
tmpdir = $settings['backup_tmpdir_name']
repo_path = $settings['backup_repository_path']
create_hotcopy(repo_path, tmpdir)

log('compressing')
zipfile = $settings['backup_zipfile']
compress_dir(tmpdir, zipfile)


log('encrypting')
passphrase = $settings['passphrase']
cryptfile = $settings['backup_cryptfile']
encrypt_file(zipfile, cryptfile, passphrase)


log('splitting into chunks')
# split file into small chunks (that can go with the mail)
prefix = $settings['backup_chunk_prefix']
size = $settings['backup_chunk_size']
split_file(cryptfile, size, prefix)


# send a mail with each chunk attached
rev = youngest_revision(tmpdir)
time = Time.now.strftime('%Y%m%d')
subject = "hotcopy#{time}_rev#{rev}"
reciever = $settings['backup_recievers']
mime = 'application/octet-stream'
chunks = Dir.glob(prefix + '*').sort
log("revision #{rev}, consists of #{chunks.size} chunks.")
chunks.each_with_index do |filename, index|
log("sending chunk##{index+1}.")
bodytext = "this is chunk##{index+1} out of #{chunks.size} in total."

attachments = [[filename, mime]]
begin
send_mail(reciever, subject, bodytext, attachments)
rescue => e
log("ERROR: failed sending, #{e.inspect}")
ok = false
end
end
msg = ok ? "OK" : "with error!"
log("backup completed #{msg}\n\n")
end


6 Answers

rubikitch

1/27/2006 10:45:00 AM

0

Simon Strandgaard

1/29/2006 11:20:00 AM

0

On 1/27/06, rubikitch <rubikitch@ruby-lang.org> wrote:
> From: Simon Strandgaard <neoneye@gmail.com>
> > Howto capture stdout + stderr from jobs invoked with rake?
> >
> > I run rake from cron. I have a
>
> cd /path/to/backup; rake 2>&1 >> /log/handybackup.log

Aha.. I did'nt knew redirection was possible.. very nice.


I had to put it in paranthesis for it to work.. maybe parenthesis is a
bash thing.

(rake 2>&1) >> log


anyways.. it works ;-)


> And `tail -f /log/handybackup.log' will help you.
> I use GNU Screen and have many `tail -f' windows in Screen.

GNU Screen is nice as well.. its been a while since I tried it out.
I wonder how to automaticly launch screen with a bunch of
'tail -f somelog'.. I have looked through some tutorials,
but they are mostly about what the keystrokes does.



> > desc "creates a hotcopy backup of the repository."
> > task :backup => [:clean] do
>
> I think the `backup' task shold be splitted into some small tasks.

Indeed its long.. I will split it. Thanks.




Sorry for the delay.

--
Simon Strandgaard


Simon Strandgaard

1/29/2006 2:39:00 PM

0

When invoking `cmd 2> log2 >> log1` within ruby..
is there any issues concerning the shell I should be aware of?
placement of parenthesis and stuff.

or will 2> and >> always work as long we are on unix?




prompt> ./a.rb
STDOUT:
stdout
STDERR:
stderr
DONE


prompt> cat a.rb
#!/usr/local/bin/ruby
`rm lout lerr`
`touch lout lerr`
`ruby test.rb 2> lerr >> lout`
puts "STDOUT:\n" + IO.read('lout')
puts "STDERR:\n" + IO.read('lerr')
puts "DONE"


prompt> cat test.rb
$stdout.puts 'stdout'
$stderr.puts 'stderr'



--
Simon Strandgaard


Austin Ziegler

1/29/2006 3:25:00 PM

0

On 29/01/06, Simon Strandgaard <neoneye@gmail.com> wrote:
> When invoking `cmd 2> log2 >> log1` within ruby.. is there any issues
> concerning the shell I should be aware of? placement of parenthesis
> and stuff. or will 2> and >> always work as long we are on unix?

2>&1 and a few other Unix redirectionisms will work on Windows with
cmd.exe as well.

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Guillaume Marcais

1/31/2006 11:44:00 AM

0

rubikitch wrote:
> From: Simon Strandgaard <neoneye@gmail.com>
> Subject: logging with rake
> Date: Fri, 27 Jan 2006 06:11:43 +0900
>
>> Howto capture stdout + stderr from jobs invoked with rake?
>>
>>
>> I run rake from cron. I have a
>
> cd /path/to/backup; rake 2>&1 >> /log/handybackup.log

The order is significant. To append both stdout and stderr to the log
file, it should be:

rake >>/log/handybackup.log 2>&1

Guillaume.

>
> And `tail -f /log/handybackup.log' will help you.
> I use GNU Screen and have many `tail -f' windows in Screen.
>
>> def log(msg)
>> str = Time.now.to_s + ": " + msg + "\n"
>> file = $settings['logfile']
>> File.open(file, 'a+') {|f| f.write str }
>> end
>
> Using shell redirection makes the log method simple.
>
> def log(msg)
> str = Time.now.to_s + ": " + msg + "\n"
> print str
> end
>
>
>> desc "creates a hotcopy backup of the repository."
>> task :backup => [:clean] do
>
> I think the `backup' task shold be splitted into some small tasks.
> --
> rubikitch
> http://www.rubyist.net/~...
>



RichAsianKid

4/11/2011 12:46:00 AM

0

On 08/04/2011 6:49 AM, asdf asdf wrote:
> On Apr 8, 2:38 pm, RichAsianKid<RichAsian...@hotmail.com> wrote:
>> Gaussian distribution.
>>
>> * * * *http://online.wsj.com/article/SB200014240527487035155045761......
>>
>> ("With India's population size, it should be so much easier to find
>> employees," says S. Nagarajan, founder of the company. "Instead, we're
>> scouring every nook and cranny."
>> :
>> :
>> "My family has invested so much money in my education, and they don't
>> understand why I am still not finding a job," says Mr. Shivanand. "They
>> are hoping very, very much that I get a job soon, so after all of their
>> investment, I will finally support them.")
>
> You are wasting time to even discuss about them.


India went from adolescence to retirement without adulthood?