简体   繁体   中英

Azure Mobile Services An unhandled exception occurred. Error: One of your scripts caused the service to become unresponsive

Apologize for my English.

I have a node js script that has to send AMQP messages to device using IoT hub. I've took thiss script from github of azure iot. Here is this sample.

Here is this sample

Here is my script, based on this one:

    console.log("creating the client");

var Client = require('azure-iothub').Client;
console.log("client has been created");

var Message = require('azure-iot-common').Message;
console.log("message has been created");


var connectionString = "HostName=id**.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=***;
console.log(connectionString);
var targetDevice = 'devicesergey';

var client = Client.fromConnectionString(connectionString);

client.open(function (err) {
    if (err) {
        console.error('Could not connect: ' + err.message);
    } 
    else {
        console.log('Client connected');

        var data = JSON.stringify({ text : 'foo' });
        var message = new Message(data);
        console.log("json message is created")
        console.log('Sending message: ' + message.getData());
        client.send(targetDevice, message, printResultFor('send'));
        console.log("message has been sent");             
    }
});

function printResultFor(op) {
  return function printResult(err, res) {
    if (err) {
      console.log(op + ' error: ' + err.toString());
    } else {
      console.log(op + ' status: ' + res.constructor.name);
    }
  };
} 

That works fine locally and I see messages on my device emulator. But when I try to put it to Azure Mobile Services API and try to run it, I see this message on logs:

An unhandled exception occurred. Error: One of your scripts caused the service to become unresponsive and the service was restarted. This is commonly caused by a script executing an infinite loop or a long, blocking operation. The service was restarted after the script continuously executed for longer than 5000 milliseconds. at process.Server._registerUncaughtExceptionListenerAndCreateHttpServer._onUncaughtException (D:\\home\\site\\wwwroot\\node_modules\\azure-mobile-services\\runtime\\server.js:218:17) at process.EventEmitter.emit (events.js:126:20)

And sometimes I see this IIS error I know exactly that this line occurs this function: client.open(function.... I've evem tried to leave only client.open() and send a messages out of this function. But in this case I see "client is not connected".

I asked about this stuff on github. They advised me to asked here. Maybe someone know how to solve this issue (with script or Azure). I would be very very greatfull!

Thank you!

The Mobile Service Custom API is a script that expose the functionality of the express.js library, please see the section Overview of custom APIs of the offical document "Work with a JavaScript backend mobile service"

I reproduced the issue successfully. I guess your script was not wrapped in the code below as the body block, and not sent the response to the client like browser.

exports.get = function(request, response) {
    // The body block
    ....
    response.send(200, "<response-body>");
}

For more details of Mobile Service Custom API, please see https://msdn.microsoft.com/library/azure/dn280974.aspx .


Update :

I changed your code as below. 在此处输入图片说明

And In order to facilitate the test, I changed the permission for the api as below, then I can access the api link https://<mobile-service-name>.azure-mobile.net/api/test with browser. 在此处输入图片说明

I've just tried to execute my script on new Azure MS and it was unsuccesfully. I will write my step-by-step actions, maybe you can see anything wrong, because I'm not so good in NodeJS.

  1. Add a new Azure MS with new SQL Database
  2. Add a new API "dev". Access - everyone for all points. Here is source code:

     exports.get = function(request, response) { console.log("creating the client"); var Client = require('azure-iothub').Client; console.log("client has been created"); var Message = require('azure-iot-common').Message; console.log("message has been created"); var connectionString = "HostName=i***.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey***"; console.log(connectionString); var targetDevice = 'devicesergey'; var client = Client.fromConnectionString(connectionString); client.open(function (err) { if (err) { console.error('Could not connect: ' + err.message); } else { console.log('Client connected'); var data = JSON.stringify({ text : 'foo' }); var message = new Message(data); console.log("json message is created") console.log('Sending message: ' + message.getData()); client.send(targetDevice, message, printResultFor('send')); console.log("message has been sent"); } }); response(200, "Hello, world!"); }; function printResultFor(op) { return function printResult(err, res) { if (err) { console.log(op + ' error: ' + err.toString()); } else { console.log(op + ' status: ' + res.constructor.name); } }; } 

If I try to execute this stuff it occurs "no azure-iothub" and "no azure-iot-common", so I need to use git to add these npm.

  1. I clone this repository to my local dir using git access to Azure MS https://id .scm.azure-mobile.net/id .git
  2. Enter the "API" folder and add the NPMs: npm安装

  3. Then I perfom "Rescan", "Save changes", "Commit", "Push" on

  4. After these actions I execute my script by path " http://id **.mobile-services.net/api/dev" and don't see anything o see the error "500.1013" and these messages on logs (id depends):

An unhandled exception occurred. Error: One of your scripts caused the service to become unresponsive and the service was restarted. This is commonly caused by a script executing an infinite loop or a long, blocking operation. The service was restarted after the script continuously executed for longer than 5000 milliseconds. at process.Server._registerUncaughtExceptionListenerAndCreateHttpServer._onUncaughtException (D:\\home\\site\\wwwroot\\node_modules\\azure-mobile-services\\runtime\\server.js:218:17) at process.EventEmitter.emit (events.js:126:20)

I can't realize what I'm doing wrong

UPDATE: I've tried to use Kudu console for installing the npms and it returns many errors. If i figured out correctly, I need to update my node js and npm. But I don't know how to do this and I didn't manage to find a solution. Here are logs: 在此处输入图片说明 I have lack of reputation, so I am not allowed to past log scripts.

I've tried to do these actions, but it doesn't help:

at the root of the repo, you'll find a .deployment file that has:

command = ..\\ZumoDeploy.cmd Change it to

command = deploy.cmd And create a deploy.cmd next to it containing:

set NPM_JS_PATH=%ProgramFiles(x86)%\\npm\\1.4.9\\node_modules\\npm\\bin\\npm-cli.js ..\\ZumoDeploy.cmd Commit both files and push.

I'm confused. How is it possible? Azure Mobile services don't permit to install azure-iot-hub npm). What can I do with this issue?

UPDATE2: Peter Pan - MSFT, you advised me to use Kudu DebucConsole to install necessary npm. But when I try to do it - I see errors.

I've messaged about this issue to "npm" command on github, they say that the version of npm which Azure is used to - is completely unsupported. htt ps://github.com/npm/npm/issues/12210#event-615573997

UPDATE3 (04/12/2016) I've solved this issue by different way. Created my own node JS script that is listening a port, read GET params(deviceId and message) and send D2C messages. Unfortunately, I still can't get trow the Azure issue.

UPDATE4 Peter Pan gave me an advise how to use another version of nodejs and npm. Now I've succesfully installed necessary NPM modules. But now Azure Mobile Script APIs don't work, it shows me {"code":404,"error":"Error: Not Found"} on any script that I try to get in my browser.

Maybe I've deleted something when I tried to do these stuffs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM