Monday, July 07, 2008

Submit Multipart Form Programmatically.

I had a requirement where one application wanted to talk to another application for uploading a video.
I couldn't leverage Active Resource since it doesn't seem to have option to submit multipart data. I came up a with a solution to use mechanize along with Hpricot.


class BasicAuthMechanize < WWW::Mechanize
def set_headers(u, r, c)
super(u, r, c)
r.basic_auth(API_KEY, 'X')
r
end
end

agent = BasicAuthMechanize.new
url = ""

node = Hpricot::Elem.new(Hpricot::STag.new('form'))
node['action'] = url
node['method'] = 'POST'
node['enctype'] = 'multipart/form-data'

form = WWW::Mechanize::Form.new(node)
form.fields << WWW::Mechanize::Form::Field.new("video_image[video_id]",video_id)
form.file_uploads << WWW::Mechanize::Form::FileUpload.new("video_image[uploaded_data]", file_path)

page = agent.submit(form)
return page.response['status'].to_i == 200 # Success

Sunday, May 18, 2008

Half A Dream

Monday, April 21, 2008

Tropa Da Elite Trailer

Friday, April 11, 2008

ar_mailer: how to avoid memory related issues

In one of my projects. I am using ar_mailer gem to store emails in the the database and send them in batches every few minutes. its really a handy gem.

About its usage and how it works, you can refer this link.

ar_mailer has 2 components, one which helps you to store emails in the database and the 2nd which reads emails from the database and sends them out. The 2nd component is available as a utility called "ar_sendmail", which supports various options. Below, I will show you how I first used it wrongly(which brought our site down!) and then correctly using monit.

Wrong Usage(for my project situation)
I setup a cron job which runs every minute, invokes ar_sendmail with following options

ar_sendmail -b 100 --max-age 0 -c "/data/project/current" -e production" --once


Above command invokes ar_sendmail once(--once) and send out a batch of 100 emails(-b).

Whats wrong above?
The cron job executes ar_sendmail every minute, ar_sendmail invokes the whole Rails environment, sends out emails and then finishes. There are 2 problems,

1. ar_mailer invokes the whole production environment every minute: it takes a while to load the production environment(all the plugins, gems etc. gets loaded) and every minute!

2. Multiple instances of ar_sendmail: Multiple instances of ar_sendmail start running since ar_sendmail doesn't create a file lock to avoid this situation. For me, once 7 instances of ar_sendmail started running which brought down the system and had to be rebooted.

Correct Usage
So from above we need to make sure that ar_sendmail doesn't invoke the whole Rails environment every minute and also multiple instances of ar_mailer doesn't get spawned.
1. "-d" option of ar_sendmail: ar_sendmail has "-d" option which runs ar_sendmail as a daemon. it invokes webrick server once for all. So the new ar_sendmail command looks like following

ar_sendmail -d -b 100 --max-age 0 --delay 60 -c "/data/project/current" -e "production"


Note the -delay and -d option

2. To make sure that multiple instances of ar_sendmail doesn't get spawned, use monit as described here

Life is back to normal...

Tuesday, April 01, 2008

The Auteurs purchases The Weinstein Company for $2.1 billion

This is HUGE.

http://studio.theauteurs.com/?p=7

Thursday, February 28, 2008

Attachement_fu Gotchas

Recently, we have been getting EofError with S3 while trying to upload images to Amazon S3.

Turns out that this has been experienced by many Rails developers. Hampton Catlin gives possible solutions here.

Tuesday, November 20, 2007

The Auteurs

Yes, thats what has been keeping me busy for a while, The Auteurs.
From Studio@TheAuteurs,
"core idea of The Auteurs is to unite great, previously unavailable works of contemporary world cinema with an avid online film community"

Oh and did I tell you that TheAuteurs is now on Facebook?

Oh Hiatus Again.

I am finding it really difficult to cope up with blogging regularly and especially when I am working(read holidaying) in Goa!

I will keep the exciting stuff that I have been working on for future posts. for now enjoy "Gmail Live", what if gmail was developed by Microsoft,