Ajax http 500 error Azure not on local

1.1k views Asked by At

When running an ajax request I get an error message:

Failed to load resource: the server responded with a status of 500 (OK)

The problem is that the server error does not seem to occur. When I run the application on my local machine but with the azure database I don't get the error message. Only the published azure application generates this error. I have done remote debugging and even though the browser display this error the server keeps handling the request and some minutes later it finishes the request. As if there actually was not server error.

The server needs about 10 minutes to finish the request. I believe it has something to do with the fact that the request it very long (because it works on smaller databases). I know azure has restrictions on CPU time on app service level free but I switched up to basic (no cpu time restrictions) so that should not be a problem. The request is very sql intense (about 20k sql queries).

Ajax call:

  $.ajax({
  async: true,
  cache: false,
  type: "POST",
  url: FooURL,
  contentType: 'application/json',
  dataType: "json",
  data: JSON.stringify(null),
  success: function (error_message) {
    $("#FooBar").removeClass("loading");
  },
  error: function(jqXHR, textStatus, errorThrown) {
  console.log(textStatus, errorThrown);
  }
});

Controller:

  [Authorize]
  [RequireHttpsAttribute]
  public class FooController : Controller
  {
    private FooBarModel foobarModel = new FooBarModel();

    public ActionResult UpdateFooBarModel()
    {
      foobarModel.UpdateModel();
      return Json("Success");
    }
1

There are 1 answers

0
Gustav Wiklander On BEST ANSWER

There is apparently an idle timeout in azure which is described here. The default value is set to 4 min and can be configured up to 30 min if you run your application on a VM. I solved it by creating a table in the database with stores the current status of request.

Table:

CREATE TABLE [dbo].[MyTable] (
[UpdateNo] INT IDENTITY (1, 1) NOT NULL,
[AllDone]  BIT DEFAULT ((0)) NOT NULL,
CONSTRAINT [MyTable$PrimaryKey] PRIMARY KEY CLUSTERED ([UpdateNo] ASC)

);

Instead of calling the method directly I create a task and returns the id of the update status row.

public ActionResult UpdateFooBarModel()
{
  int id = foobarModel.StartUpUpdate(); //Creates the status row 
  Task.Run(() => foobarModel.UpdateModel(id));
  return Json(id);
}

public ActionResult GetUpdateStatus(int UpdateNo)
{
  bool status = foobarModel.GetUpdateStatus(UpdateNo);
  return Json(status);
}

Ajax call:

function check_status(StatusId) {
$.ajax({
  async: true,
  cache: false,
  type: "POST",
  url: GetUpdateStatusURL + "/?UpdateNo=" + StatusId,
  contentType: 'application/json',
  dataType: "json",
  success: function (StatusDone) {
    if (StatusDone == true) {
      console.log("Update done!");
      $("#FooBar").removeClass("loading");
    } else {
      console.log("Not done!")
      setTimeout(function () {
        check_status(StatusId);
      }, 5000); //Check every 5 seconds
    }
  },
  error: function (jqXHR, textStatus, errorThrown) {
    console.log(textStatus, errorThrown);
  }
});

}