core icon indicating copy to clipboard operation
core copied to clipboard

Controller and timeouts

Open kyuz0 opened this issue 9 years ago • 8 comments

Hi,

I have a controller for an AJAX request that takes a long time to process and return to the browser. What I am observing is that, before the controller has a chance to end, the ajax request fails with 'net::ERR_EMPTY_RESPONSE' on the client (jquery client-side).

Now, I originally thought this was an issue with timeouts client-side, but I have increased that and I still get this empty response.

Might it be that Adonis is imposing a server-side maximum limit on requests after which it simply fails and returns an empty answer (which would explain the error message as Jquery cannot parse that as JSON).

Cheers, Donato

kyuz0 avatar Jan 27 '17 14:01 kyuz0

  1. What's the url you are calling from Ajax?
  2. Howz your routes.js file?
  3. Are you making the response via the controller method for the given route?

thetutlage avatar Jan 27 '17 15:01 thetutlage

Url:

  • http://localhost:3333/test

Route: Route.get('/test', 'TasksController.test')

Controller:

  • test (request, response) { setTimeout(function() { response.json({ok:'ok'}) }, 120000) }

The setTimeout() simulates a longer call. if I go with 1 minute, everything works and I get the ok JSON response. I got with 2 minutes, and things break.

On a similar note, I'm confused on the usage of async functions with callbacks within a generator, not sure if that's the right way of doing it.

kyuz0 avatar Jan 27 '17 15:01 kyuz0

Why do you want to make the request wait for 2minutes?

thetutlage avatar Jan 27 '17 16:01 thetutlage

Im using setTimeout() here just as an example. I'm performing a task that takes several minutes to return, the time it takes is not really under my control.

kyuz0 avatar Jan 27 '17 16:01 kyuz0

Http server in Node.js has a timeout, you can disable it or set it to higher value. https://nodejs.org/api/http.html#http_server_timeout

How to do it in AdonisJs

You can do it by getting the access to the raw HTTP server inside Listeners/Http.js file.https://github.com/adonisjs/adonis-app/blob/develop/app/Listeners/Http.js#L40

const Server = use('Adonis/Src/Server')

Server.getInstance().timeout = 0

thetutlage avatar Jan 27 '17 16:01 thetutlage

That did the trick. Thanks a lot!

I'm really enjoying using AdonisJS and I'm new to Javascript really, so enjoying learning a new language as well. Still getting my head around the callback and asyc-based nature of the language, can certainly appreciate generators a lot!

A question: I understand in order to use generators you need a sort of generator runner, which is going to call next and so on... what do you use in AdonisJS? And how does that work when, from within a generator, you use a normal function with a callback?

kyuz0 avatar Jan 27 '17 20:01 kyuz0

@kyuz0 How async function works is a broad concept. I suggest you to join the gitter room and discuss about same.

Closing the issue since it works

thetutlage avatar Jan 28 '17 13:01 thetutlage

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Mar 11 '20 19:03 lock[bot]