Search code examples
ruby-on-railsrubyruby-on-rails-3delayed-jobsidekiq

Pull/push status in rails 3


I have a longer running task in the background, and how exactly would I let pull status from my background task or would it better somehow to communicate the task completion to my front end?

Background :

Basically my app uses third party service for processing data, so I want this external web service workload not to block all the incoming requests to my website, so I put this call inside a background job (I use sidekiq). And so when this task is done, I was thinking of sending a webhook to a certain controller which will notify the front end that the task is complete.

How can I do this? Is there a better solution for this?

Update:

My app is hosted on heroku

Update II:

I've done some research on the topic and I found out that I can create a seperate app on heroku which will handle this, found this example :

https://github.com/heroku-examples/ruby-websockets-chat-demo

This long running task will be run per user, on a website with a lot of traffic, is this a good idea?


Solution

  • I would implement this using a pub/sub system such as Faye or Pusher. The idea behind this is that you would publish the status of your long running job to a channel, which would then cause all subscribers of that channel to be notified of the status change.

    For example, within your job runner you could notify Faye of a status change with something like:

    client = Faye::Client.new('http://localhost:9292/')
    client.publish('/jobstatus', {id: jobid, status: 'in_progress'})
    

    And then in your front end you can subscribe to that channel using javascript:

    var client = new Faye.Client('http://localhost:9292/');
    
    client.subscribe('/jobstatus', function(message) {
      alert('the status of job #' + message.jobid + ' changed to ' + message.status);
    });
    

    Using a pub/sub system in this way allows you to scale your realtime page events separately from your main app - you could run Faye on another server. You could also go for a hosted (and paid) solution like Pusher, and let them take care of scaling your infrastructure.

    It's also worth mentioning that Faye uses the bayeaux protocol, which means it will utilise websockets where it is available, and long-polling where it is not.