tags:

views:

84

answers:

2

I coded small web app that runs ant (batch file). The processing time of the batch file could take up to minutes.

index.haml list all available ant files and run.haml runs them. The flow how it works now is that when I click a link from index.haml the ant script is run and after it finishes the whole run.haml page is sent to the browser. So after clicking link from index.haml I still can see index.haml and nothing from run.haml

After I click a link from index.haml I want to

  • show what script is going to be run and then
  • run the ant script and then
  • display the results of it.

I was recommended in my other question to use

I didn't understand how a separate worker thread could help me. Would the delayed job's results that are captured by ruby's call be sent to the browser once the job finishes?

I also didn't get how I can use Ajax in sinatra.

Could somebody point me out what a solution for that could be like? Please note that I know bit of ruby, learned bit of sinatra and haml yesterday. No nothing about Ajax :-) I learn by examples ... and happy to learn anything.

index.haml gives me html like

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html>
      <head>
        <title>Available test suits</title>
      </head>
      <body>
        <h1>Available test suits</h1>
        <br/><br/>
        <a href='run?run=login_build'>login_build</a>
        <br />
        <a href='run?run=login_cycle_build'>login_cycle_build</a>
        <br />
        <a href='run?run=login_cycle_academicyear_build'>login_cycle_academicyear_build</a>
      </body>
    </html>

run.haml looks like

    !!!  
    %html  
      %head  
        %title Running  
      %body
        = "Starting test suite: #{params['run']}"
        - output = %x[cd C:\\Program Files\\TestPro\\TestPro Automation Framework410 && ant -lib lib -f "C:\\Program Files\\TestPro\\TestPro Automation Framework410\\Output Files\\builds\\#{params['run']}.xml"]
        -#The result is
        %br
        = output.split("\n")[-2,2].join("<BR>")
        = "<br/>"*2
        %a(href="/")back to suits list
+2  A: 

If you want to use Ajax must, choose a javascript library Dojo, Prototype, Mootools, JQuey ... Each of them has specific tools for handling ajax requests.

In javascript you make an xhr(Ajax request), which may be asynchronous and still Sinatra serves you the wanted content, you can display in webpage what you want.

var xhrLoadClientMenu = function(param){
var result_node = dojo.byId('div_menu')
var xhrArgs = {
  // the Sinatra get "/run" ... end handler
  url: '/run',
  load: function(data){
    // When response is rendered come's here 
    result_node.innerHTML = data;
  },
  error: function(error){
    msg = "<p>Ooops some error ...<br><br>" + error + "</p>";
   }
 }
  // Here you can put what you want to display durring loading
  result_node.innerHtml = "Loading..." 
  var defered = dojo.xhrGet(xhrArgs)
}

a xhrGet example using dojo. See inline comments.

  • xhrLoadClientMenu(param) is triggered by an event;
  • a HTML element with id #div_menu from page is stored in result_node object;
  • xhrArgs object sets the Ajax request properties like:
    1. url: the Sinatra handler which renders the content;
    2. load: function gets the response from sinatra and replaces result_node content when load is completed
    3. error if something goes wrong this message will be shown in result_node;
  • meanwhile you may set the result_node content to anything while the real content is loading;
  • last line executes the xhr request.

All of this is happening without a page reload.

kfl62
@kfl62: does your Ajax request example renders(create) whole page? if yes it seems to me that I need to create one more page to call the Ajax.
Radek
the Ajax request does not render, Sinatra does. Ajax only gets the rendered info from Sinatra and replaces `result_node` content with it, without reloading the whole page.
kfl62
it's much clearer, thank you for the edit. Still need some clarification though. Is the Ajax code part of the 'sinatra run get'? if yes, I guess I need new sinatra 'get' to process the ajax request... or? :-)
Radek
I think you need to read [this](http://www.w3schools.com/ajax/default.asp)
kfl62
@kfl62: thank you.It works :-) I used just the examples from w3schools.com
Radek
nice hearing that :)
kfl62
@kfl62: bit struggling with http://stackoverflow.com/questions/3965834/can-i-use-parameter-to-call-loadxmldoc-from-html-ajax though
Radek
+1  A: 

Delayed_job results are indeed sent "back to the browser." Please take another look at this live example of using Sinatra and DJ to process a task asynchronously and push the result to the browser once the task is complete.

The call runs asynchronously, and the rest of the page loads, and when the calls are complete the data is posted back to the page. Now, since it's on the cloud, it may seem VERY delayed; you'll want to download the source and run it on your own machine to see what kind of speed DJ offers on a local network. The results are indeed processed and sent back; scroll down the page and you'll see the results of earlier processing -- you can even enter your own text block to add to the processing queue.

So perhaps you could utilize delayed_job to create a queue for asynchronous processing of your test suites -- 'out of sync' from the rest of your page, but also with the option of passing data back 'incrementally' as well as once the task completes.

Now, Ajax offers another way to provide this same functionality. I absolutely and unequivocally recommend getting one of the Javascript frameworks and learning how to do this sort of asynchronous processing that way as well. However, based on your earlier question, I would still suggest working through the source code of the example above using Sinatra and delayed_job, as it's already extremely close to what you are trying to do, not to mention that it keeps you entirely in Ruby.

Joe
I tried to used the demo page on Fri and it's Mon and nothing was translated. So it seemed to me that it doesn't work. So it looks like it is indeed VERY VERY VERY delayed.
Radek
@Joe: I want to know if user has to refresh the page or the job's result will appear after it finishes by itself
Radek
Well, look -- you're going to want to download the source and run it on your own machine to see what the performance is really like. Again, it's just an example :) You'll also want to try to play around with Ajax -- see the other answer for a good list of frameworks -- which can definitely auto-insert the result for you without a refresh.
Joe
so delayed_job will not `auto-insert` the results?
Radek