3

I am calling two different actions for the same controller with ajax in development environment. In one action I am updating a database in a very slow process. On the other action I want to get the percentual concluded to update a progressbar. I can see that both are executing, The problem is that the second action is waiting for the first to finish to execute itself and it should be executing every second to get the actual percentual. Can rails execute two actions in parallel? or is it a Webrick problem?


I Tried with puma as suggested but it's still waiting for the first action, heres the ajax code call

$(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 print out:

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 for the console it is just making the first call before the upload, then it's making the upload, then, after finishing it making the second call to status

capivarao
  • 70
  • 6

2 Answers2

0

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

Note: Be advised that using SQLite can have similar concurrency issues (especially if writes are involved!) Switch to PostgeSQL or MySQL.


To use Thin:

Gemfile

gem 'thin'

Of course...

bundle install

Then to start server

thin start

To use Puma:

Gemfile

gem 'puma'

Of course...

bundle install

To start server

rails s puma

To use Unicorn:

Gemfile

gem 'unicorn'

Of course...

bundle install

To start server

unicorn_rails
Darren Hicks
  • 4,610
  • 1
  • 29
  • 32
0

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

config.middleware.delete Rack::Lock

You also has to change Webrick. It worked ok with Puma.

capivarao
  • 70
  • 6