0

I have a problem with Ruby on Rails.

I have a "Host Model" with a method, which takes longer to run.

class Host < ActiveRecord::Base
  def take-a-while 
    #do some stuff 
  end 
end

Now I try to access a page in which this method is executed on all hosts and is automatically displayed each time you perform in a new table row with the result. My problem is that the HTML document is first completely rendered and the table is displayed only after the complete loading (several minutes).

Is there a way to solve this problem?

Thanks in advance Greetings!

Markus
  • 33
  • 4

2 Answers2

0

try this out:

You could try caching the resultant of the method take-a-while Please look into Caching with Rails

Sachin Singh
  • 7,107
  • 6
  • 40
  • 80
  • My problem is that the method performs a snmp-walk on a live system and the data must be up to date. – Markus Jul 28 '14 at 07:39
  • then you could try to fetch only relevant data, not whole data. – Sachin Singh Jul 28 '14 at 07:40
  • Ok I think I need to explain it better. In the show of every Host there is an option to get the result of the SNMP-Walk but now I want the Option to SNMP-Walk over all Hosts and display the State on a Page. This page should show the result with every Iteration in a table – Markus Jul 28 '14 at 07:43
0

With this kind of problem. One way to solve is using background job (queueing system + state machine)

Practically, in Ruby and Ruby on Rails world, there are many options like

The simplest setup is to use delayed_job

Good article to checkout https://devcenter.heroku.com/articles/delayed-job

And these articles http://www.rubyspy.com/282_10039570/, http://blog.salsify.com/engineering/delayed-jobs-callbacks-and-hooks-in-rails might be helpful for handling delayed_job finished callback.

zdk
  • 1,528
  • 11
  • 17
  • Ok, thanks a lot. I read through the article and now I delayed the snmp-walk. but how can i now add cols to the table exactly when a job is finished? – Markus Jul 28 '14 at 07:57
  • You can use ajax polling, comet and web socket etc. but the easiest to start probably just using ajax like http://stackoverflow.com/questions/5042300/indicate-to-an-ajax-process-that-the-delayed-job-has-completed – zdk Jul 28 '14 at 08:06
  • Ok thanks again, unfortunately I have no idea what I need to do. How do I get my results in the HTML table? I have no experience with AJAX and do not know how I let run simultaneously an AJAX process that captures and transmits the results in the table – Markus Jul 28 '14 at 08:23
  • Well, I can explain it the other way. Once you push the job in the background. It likes that you detach the job from the normal process. And the idea to get thing back is your frontend, the html content, keep asking your backend (at the available url endpoint) if there is something new etc. if so, your frontend updates its content. Hope this clarifies a bit more to you. – zdk Jul 28 '14 at 08:36
  • Yes :) It is clearer to me now. But how is it done that my forntend asks all the time is there is something new? As far as I understood your Link about AJAX i can check if create a state field and ask for the state. But how is it done that my frontend continuously asks for changes? – Markus Jul 28 '14 at 08:42
  • Check out this one: http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery that explains that stuff. – zdk Jul 28 '14 at 08:50