question

Venkata R Alladi avatar image
Venkata R Alladi asked

Calling RESTFull Services

I am trying to call an external RESTFull service. The problem is the context of the event handler is getting closed (succeed or done) before the external service call return. I tried removing the "context.succeed()" method and the external call going well but with errors like "timed out.." OR "Task timed out after 3.00 seconds". Is there any way I can hold the context to completed the external call? and set the time out value. -R
alexa skills kitdebugging
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jjaquinta avatar image
jjaquinta answered
Being able to alter the timeout value has been a frequent feature request. The best I've seen anyone come up with is to tell the user it's going to take a bit, and ask if they want to hear a joke while they wait. :-) I posted a sample UPS app that had a similar problem. For that, I set the program up to track the things they want. The request happens on a background thread and I just give them the latest data in the foreground thread. If I know a request is pending, I tell them so and ask them to check back.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Venkata R Alladi avatar image
Venkata R Alladi answered
I see that but, is there any solution / way to async communication external sources (including databases). Also I keep getting the following message. "Process exited before completing request"
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jjaquinta avatar image
jjaquinta answered
If you're doing a web service, it shouldn't be a problem. In the UPS example I spin off threads to do the actual UPS call. If you're writing a Lambda function, I'm finding it all rather more problematic. It seems to shut everything down as soon as you end a call, and it creates JVMs as it wishes to service interleaved requests. I might just end up using the Lambda function to call my web service! If you want more details, you might indicate your implementation language and your deployment method.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Chris Griffin avatar image
Chris Griffin answered
First you have to tweak the http.get just slightly. Your callback should stream the data... var data = ''; response.on( 'data', function( x ) { data += x; } ); response.on( 'end', function() { var parsed = JSON.parse( data ); } If you are already doing this and it still gives you the exit error... Fret not, I just spent about 3 hrs figuring this out but what I realized is that it exits on uncaught errors. So make sure your code handling your service response is catching errors and/or debug and determine exactly where it exits. Good luck
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Ross@Amazon avatar image
Ross@Amazon answered
Thanks for the insight into this issue. The SDK team has added the ability to implement status messages into their backlog of possible features for future releases.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jondecker76 avatar image
jondecker76 answered
Any news on this? I've got a killer skill in the works that interfaces the Jawbone UP fitness band. Most functions are working great. However, some functions aren't possible yet because the process exits before the request completes and returns information from the Jawbone REST API. Is there any way around this???
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Nick Gardner avatar image
Nick Gardner answered
I've asked the Lambda team for additional information, as it seems this is a pretty common question people have been asking. I'll post here when I have more info. -Nick
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Kruse avatar image
Matt Kruse answered
Which timeout are you dealing with - Lambda timeout or Echo timeout? You can configure Lambda to timeout later. I set mine to 9 seconds, because the Echo timeout is 8 seconds. But if your web service call is taking longer than the Echo timeout, then you're going to be out of luck. There is no way (currently) to extend it.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jondecker76 avatar image
jondecker76 answered
It's not a time out at all. The lambda function finishes before data is returned from the external API call, so there is no way to act on the returned data. I've tried commenting out context.succeed and context.done calls but it doesn't seem to help (it doesn't seem like an elegant solution anyways)
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Kruse avatar image
Matt Kruse answered
Oh, I get it. You probably just need to code it differently. Since an external call is an async operation, you need to call context.succeed() in the callback, not outside of it. Something like this: [code] var http = require('http'); exports.handler = function (json, context) { try { var req = http.get(url,function(res) { context.succeed( {json} ); }); req.on('error', function(e) { context.fail('problem with request: ' + e.message); }); } catch (e) { context.fail("Exception: " + e); } }; [/code]
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.