Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Open Graph from Rails Heroku

I've been fighting this problem all day, and could really need some input.

I have a Rails application (3.1.3) running on Heroku Cedar, trying to publish some Facebook Open Graph actions with gem fb_graph (2.4.0).

This is my code.

begin
    app = FbGraph::Application.new(ENV['facebook_app_id'])
    me = FbGraph::User.me(user.facebook_access_token)

    logger.info "Facebook: User #{user.name} reviewing game #{game_url(review.game)}"

    action = me.og_action!(
        app.og_action(:review),
        :game => game_url(review.game),
        :content => review.review,
        :rating => review.rating
    )
rescue Exception => exc
    logger.error "Failed to publish review #{review.id} to facebook #{user.facebook_auth}"
    logger.error "Facebook error msg: #{exc.message}"
end

If I go to the page in my application that will try to run this code, it will fail with the following message.

FbGraph::InvalidRequest (Exception :: Could not retrieve data from URL.)

But, if I where to open a Heroku console, heroku run console and type in the code by hand, it works perfectly.

My first thought was that game_url(review.game) where to blame, but after adding the log message, I'm sure that it returns the correct URL. And, since the whole thing works when I run it manually through heroku console it confirms that URL to be accessible and delivering the data requested.

Any feedback or experiences with this is greatly appreciated.

like image 933
Mikael Lundin Avatar asked Mar 27 '12 15:03

Mikael Lundin


2 Answers

Solved it!

Really stupid actually. I had only one web dyno in my Heroku application. Since my only web dyno was doing the Facebook API call, there was no dyno to respond to the Facebook callback. You need at least 2 to get this to work.

The reason the code worked in heroku console was simply because the web dyno was not busy handling my request.

I might push that code to a work queue in the future as it seems more appropriate and let a worker dyno handle the Facebook publishing.

like image 125
Mikael Lundin Avatar answered Nov 17 '22 15:11

Mikael Lundin


An alternative to a second dyno might be to try Unicorn - that would allow your app to service the second call while the first is blocked.

These articles give some useful detail on getting started with Unicorn:

  • More concurrency on a single Heroku dyno with the new Celadon Cedar stack
  • Unicorn!
like image 1
metadaddy Avatar answered Nov 17 '22 15:11

metadaddy