Ruby on rails - How to make controller do 2 actions at the same time with ajax?

I am calling two different actions for one controller with ajax in a development environment. In one step, I am updating the database in a very slow process. In another activity, I want to get the completed percentage in order to update the progress bar. I can see that both are running, the problem is that the second activity is waiting for the first to finish executing and it has to run every second to get the actual percentage. Can rails do two things in parallel? or is it a Webrick problem?


I tried with puma as suggested but it is still waiting for the first action, heres calling the ajax code

$(document).ready(function() {
$(':button').click(function(){
    var formData = new FormData($('form')[0]);
    $.ajax({
        url: '/quotes/upload',  //Server script to process data
        type: 'POST',
        //Ajax events
        beforeSend: progress,
        success: completeHandler,
        error: function(ts) { alert(ts.responseText) },
        // Form data
        data: formData,
        //Options to tell jQuery not to process data or worry about content-type.
        cache: false,
        contentType: false,
        processData: false
    });
});
});

function progress() {
$.ajax({
    url: '/quotes/status.json',
    dataType: "JSON"
}).success(function (percentual) {
    if (percentual >= '95') {
        location.reload();
    } else {
        var $bar = $('.bar');
        $bar.width(percentual+"%");
        setInterval(progress,800);
    }
});
}

      

and here is the console printout:

Started GET "/quotes/status.json" for 127.0.0.1 at 2014-09-21 10:23:38 -0300
Processing by QuotesController#status as JSON

 percentual => 0 Completed 200 OK in 4ms (Views: 0.6ms | ActiveRecord: 0.0ms)


Started POST "/quotes/upload" for 127.0.0.1 at 2014-09-21 10:23:38 -0300
Processing by QuotesController#upload as */*
Parameters: {"quotes"=>#<ActionDispatch::Http::UploadedFile:0x00000001b980b0    @tempfile=#<Tempfile:/tmp/RackMultipart20140921-5849-otvxky>, @original_filename="HIST_XXX.TXT", @content_type="text/plain", @headers="Content-Disposition: form-data; name=\"quotes\"; filename=\"HIST_XXX.TXT\"\r\nContent-Type: text/plain\r\n">}
  Rendered quotes/index.html.erb within layouts/application (0.7ms)
 Completed 200 OK in 10456ms (Views: 170.3ms | ActiveRecord: 0.0ms)


Started GET "/quotes/status.json" for 127.0.0.1 at 2014-09-21 10:23:49 -0300
Processing by QuotesController#status as JSON

 percentual => 100 Completed 200 OK in 3ms (Views: 0.4ms | ActiveRecord: 0.0ms)

      

As I can see in the console, it just makes the first call before loading and then does the download, after which it completes the second call to the status

+3


source to share


2 answers


Thanks for the tips, I found a solution: Place this in your development.rb file:

config.middleware.delete Rack::Lock

      



You should also change Webrick. It worked fine with Puma.

0


source


You're right - Webrick will only process one request at a time. Maybe give Thin , Puma or Unicorn a go - they all handle concurrent requests.

Note . Note that using SQLite can have similar concurrency issues (especially when it comes to writing!) Switch to PostgeSQL or MySQL.


Use Thin :

Gemfile



gem 'thin'

      

Sure...

bundle install

      

Then start the server

thin start

      


Use Puma :

Gemfile



gem 'puma'

      

Sure...

bundle install

      

To start the server

rails s puma

      


Use Unicorn :

Gemfile



gem 'unicorn'

      

Sure...

bundle install

      

To start the server

unicorn_rails

      

0


source







All Articles