0

スケーラビリティが最も重要な状況にあります。サードパーティのWebサービスを呼び出す必要があるAPIエンドポイントがあり、完了するまでに10秒以上かかる場合があります。私が懸念しているのは、サードパーティのリクエストが完了するのを待っている間に、Webリクエストがサーバーにスタックすることです。「StartJob」へのリクエストがすぐに返され、ジョブが実際にバックグラウンドで実行されることを確認する必要があります。これを行うための最良の方法は何ですか?

// Client polls this endpoint to find out if job is complete
public ActionResult GetResults(int jobId)
{
    return Content(Job.GetById(jobId));
}

//Client kicks off job with this endpoint
public ActionResult StartJob()
{
    //Create a new job record
    var job = new Job();
    job.Save();

    //start the job on a background thread and let IIS return it's current thread immediately
    StartJob(); //????

    return Content(job.Id);
}

//The job consists of calling a 3rd party web service which could take 10+ seconds.
private void StartJob(long jobId)
{
   var client = new WebClient();
   var response = client.downloadString("http://some3rdparty.com/dostuff");

   var job = Job.GetById(jobId);
   job.isComplete = true;
   job.Save();
}
4

1 に答える 1

3

呼び出し元が結果を気にしない場合は、次のようなことができます。

Task.Factory.StartNew(StartJob(job.Id));

このコメントでServyが提案したように、この適応を使用することもできます。

Task.Factory.StartNew(StartJob(job.Id), TaskCreationOptions.LongRunning);
于 2013-03-15T20:09:17.797 に答える