このWorkerRole本当に必要だろうか…


Windows Azureでバッチ処理などを行おうとすると必ずと言って良いほど、WorkerRoleを使いましょうなんて答えがかえってくる気がします。ちょっとだけ工夫?して節約してみましょう。

これは、WorkerRoleプロジェクトを新規に作成した際に生成されるテンプレートコードです。

   1: using System;

   2: using System.Collections.Generic;

   3: using System.Diagnostics;

   4: using System.Linq;

   5: using System.Net;

   6: using System.Threading;

   7: using Microsoft.WindowsAzure;

   8: using Microsoft.WindowsAzure.Diagnostics;

   9: using Microsoft.WindowsAzure.ServiceRuntime;

  10: using Microsoft.WindowsAzure.StorageClient;

  11:  

  12: namespace WorkerRole1

  13: {

  14:     public class WorkerRole : RoleEntryPoint

  15:     {

  16:         public override void Run()

  17:         {

  18:             // これはワーカーの実装例です。実際のロジックに置き換えてください。

  19:             Trace.WriteLine("WorkerRole1 entry point called", "Information");

  20:  

  21:             while (true)

  22:             {

  23:                 Thread.Sleep(10000);

  24:                 Trace.WriteLine("Working", "Information");

  25:             }

  26:         }

  27:  

  28:         public override bool OnStart()

  29:         {

  30:             // 同時接続の最大数を設定します

  31:             ServicePointManager.DefaultConnectionLimit = 12;

  32:  

  33:             // 構成の変更を処理する方法については、

  34:             // MSDN トピック (http://go.microsoft.com/fwlink/?LinkId=166357) を参照してください。

  35:  

  36:             return base.OnStart();

  37:         }

  38:     }

  39: }

 

対して、下記はWebRoleプロジェクトを新規に作成した際に生成されるテンプレートコードです。

   1: using System;

   2: using System.Collections.Generic;

   3: using System.Linq;

   4: using Microsoft.WindowsAzure;

   5: using Microsoft.WindowsAzure.Diagnostics;

   6: using Microsoft.WindowsAzure.ServiceRuntime;

   7:  

   8: namespace WebRole1

   9: {

  10:     public class WebRole : RoleEntryPoint

  11:     {

  12:         public override bool OnStart()

  13:         {

  14:             // 構成の変更を処理する方法については、

  15:             // MSDN トピック (http://go.microsoft.com/fwlink/?LinkId=166357) を参照してください。

  16:  

  17:             return base.OnStart();

  18:         }

  19:     }

  20: }

 

相違点は、Runメソッドが無いだけですね。と言うわけで、WorkerRoleと同様にRunメソッドを追加しましょう。
これでほとんどWorkerRoleのテンプレートコードと同じになりました。
WebRoleの負荷具合にもよると思いますが、SQL Azureに保存したセッションデータの削除など、ちょっとしたバッチ処理程度ならば、WebRoleにコードを追加してあげれば良いのかなと思う所です。(WorkerRoleの課金もバカにならないですからね。)

   1: using System;

   2: using System.Collections.Generic;

   3: using System.Diagnostics;

   4: using System.Linq;

   5: using System.Threading;

   6: using Microsoft.WindowsAzure;

   7: using Microsoft.WindowsAzure.Diagnostics;

   8: using Microsoft.WindowsAzure.ServiceRuntime;

   9: using Microsoft.WindowsAzure.StorageClient;

  10:  

  11: namespace WebRole1

  12: {

  13:     public class WebRole : RoleEntryPoint

  14:     {

  15:         public override void Run()

  16:         {

  17:             // これはワーカーの実装例です。実際のロジックに置き換えてください。

  18:             Trace.WriteLine("WorkerRole1 entry point called", "Information");

  19:  

  20:             while (true)

  21:             {

  22:                 //何かの処理を実行しましょう。

  23:                 

  24:                 Thread.Sleep(10000);

  25:                 Trace.WriteLine("Working", "Information");

  26:             }

  27:         }

  28:  

  29:         public override bool OnStart()

  30:         {

  31:             // 構成の変更を処理する方法については、

  32:             // MSDN トピック (http://go.microsoft.com/fwlink/?LinkId=166357) を参照してください。

  33:  

  34:             return base.OnStart();

  35:         }

  36:     }

  37: }

コメントを残す