2017-02-26 97 views
0

我有一個工作者角色,所以當我部署它時,它工作一次,然後什麼都不做,這是我的代碼,我該如何解決這個問題? 我只改變了這個地方,函數OnStart和OnStop是默認的。Azure Worker角色剛開始工作

public class WorkerRole : RoleEntryPoint 
{ 
    private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); 
    private readonly ManualResetEvent runCompleteEvent = new ManualResetEvent(false); 

    public override void Run() 
    { 
     Thread.Sleep(10000); 
     Trace.TraceInformation("WorkerRole is running"); 

     while(true) 
     { 
      try 
      { 
       this.RunAsync(this.cancellationTokenSource.Token).Wait(); 
      } 
      finally 
      { 
       this.runCompleteEvent.Set(); 
      } 
     } 
    } 

    public override bool OnStart() 
    { 
     ServicePointManager.DefaultConnectionLimit = 12; 
     bool result = base.OnStart(); 
     Trace.TraceInformation("WorkerRole has been started"); 
     return result; 
    } 

    public override void OnStop() 
    { 
     Trace.TraceInformation("WorkerRole is stopping"); 

     this.cancellationTokenSource.Cancel(); 
     this.runCompleteEvent.WaitOne(); 

     base.OnStop(); 

     Trace.TraceInformation("WorkerRole2 has stopped"); 
    } 

    public byte[] imageToByteArray(System.Drawing.Image imageIn) 
    { 
     MemoryStream ms = new MemoryStream(); 
     imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
     return ms.ToArray(); 
    } 


    private CloudBlobContainer GetImageBlobContainer() 
    { 
     var storageAccount = CloudStorageAccount.Parse("myconnectionstring"); 
     var blobClient = storageAccount.CreateCloudBlobClient(); 
     var container = blobClient.GetContainerReference("cadblob"); 
     container.CreateIfNotExists(); 

     container.SetPermissions(
      new BlobContainerPermissions 
      { 
       PublicAccess = BlobContainerPublicAccessType.Blob 
      }); 
     return container; 
    } 

    public static Image ScaleImage(Image image, int maxWidth, int maxHeight) 
    { 
     var ratioX = (double)maxWidth/image.Width; 
     var ratioY = (double)maxHeight/image.Height; 
     var ratio = Math.Min(ratioX, ratioY); 

     var newWidth = (int)(image.Width * ratio); 
     var newHeight = (int)(image.Height * ratio); 

     var newImage = new Bitmap(newWidth, newHeight); 
     Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight); 
     return newImage; 
    } 

    private async Task RunAsync(CancellationToken cancellationToken) 
    { 
     CloudStorageAccount storageAccount = CloudStorageAccount.Parse("Myconnectionstring");    
     CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();   
     CloudQueue queue = queueClient.GetQueueReference("cadqueue");    
     CloudQueueMessage retrievedMessage = queue.GetMessage(); 

     if (retrievedMessage.AsString != null || retrievedMessage.AsString != "") 
     { 
      string[] msg = retrievedMessage.AsString.Split(' '); 

      CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); 
      CloudTable table = tableClient.GetTableReference("cadtable"); 

      TableOperation retrieveOperation = TableOperation.Retrieve<ImageEntity>("Images", msg[0]); 
      TableResult retrievedResult = table.Execute(retrieveOperation); 
      var container = GetImageBlobContainer(); 
      msg[2] = "mini_" + msg[2]; 
      CloudBlockBlob blockBlob = container.GetBlockBlobReference(msg[2]); 
      blockBlob.Properties.ContentType = msg[1]; 

      Image img; 
      var webClient = new WebClient(); 
      byte[] imgBytes = webClient.DownloadData(((ImageEntity)retrievedResult.Result).Full_img); 
      using (var ms = new MemoryStream(imgBytes)) 
      { 
       img = Image.FromStream(ms); 
      } 
      Image thumb = ScaleImage(img, 220, 160); 
      var fileBytes = imageToByteArray(thumb); 
      await blockBlob.UploadFromByteArrayAsync(fileBytes, 0, fileBytes.Length); 

      ImageEntity ent = (ImageEntity)retrievedResult.Result; 
      if (ent != null) 
      { 
       ent.Mini_img = "https://cadwebstorage.blob.core.windows.net/cadblob/" + msg[2]; 
       TableOperation insertOrReplaceOperation = TableOperation.InsertOrReplace(ent); 
       table.Execute(insertOrReplaceOperation); 
      } 
      else 
      { 
       Console.WriteLine("Entity could not be retrived."); 
      } 

      queue.DeleteMessage(retrievedMessage); 
     } 


     while (!cancellationToken.IsCancellationRequested) 
     { 
      Trace.TraceInformation("Working"); 
      await Task.Delay(10000); 
     } 
    } 

} 
+0

你可以在這裏發佈整個班級嗎? –

+0

是的,當然,我會編輯我的帖子來說清楚。 – oceankit

回答

2

圖像縮放代碼的工作,因爲這個循環

while (!cancellationToken.IsCancellationRequested) 
     { 
      Trace.TraceInformation("Working"); 
      await Task.Delay(10000); 
     } 

private async Task RunAsync(CancellationToken cancellationToken)的只有一次的原因。這永遠不會終止,除非你停止工人實例。 要解決它,你可以把圖像處理代碼內環路這樣

private async Task RunAsync(CancellationToken cancellationToken) 
     { 
      while (!cancellationToken.IsCancellationRequested) 
      { 
       ScaleImage(); 
       await Task.Delay(10000); 
      } 
     } 

     private void ScaleImage() 
     { 
      Trace.TraceInformation("Scaling Image"); 
      CloudStorageAccount storageAccount = CloudStorageAccount.Parse("Myconnectionstring"); 
      CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient(); 
      CloudQueue queue = queueClient.GetQueueReference("cadqueue"); 
      CloudQueueMessage retrievedMessage = queue.GetMessage(); 

      if (retrievedMessage.AsString != null || retrievedMessage.AsString != "") 
      { 
       string[] msg = retrievedMessage.AsString.Split(' '); 

       CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); 
       CloudTable table = tableClient.GetTableReference("cadtable"); 

       TableOperation retrieveOperation = TableOperation.Retrieve<ImageEntity>("Images", msg[0]); 
       TableResult retrievedResult = table.Execute(retrieveOperation); 
       var container = GetImageBlobContainer(); 
       msg[2] = "mini_" + msg[2]; 
       CloudBlockBlob blockBlob = container.GetBlockBlobReference(msg[2]); 
       blockBlob.Properties.ContentType = msg[1]; 

       Image img; 
       var webClient = new WebClient(); 
       byte[] imgBytes = webClient.DownloadData(((ImageEntity)retrievedResult.Result).Full_img); 
       using (var ms = new MemoryStream(imgBytes)) 
       { 
        img = Image.FromStream(ms); 
       } 
       Image thumb = ScaleImage(img, 220, 160); 
       var fileBytes = imageToByteArray(thumb); 
       await blockBlob.UploadFromByteArrayAsync(fileBytes, 0, fileBytes.Length); 

       ImageEntity ent = (ImageEntity)retrievedResult.Result; 
       if (ent != null) 
       { 
        ent.Mini_img = "https://cadwebstorage.blob.core.windows.net/cadblob/" + msg[2]; 
        TableOperation insertOrReplaceOperation = TableOperation.InsertOrReplace(ent); 
        table.Execute(insertOrReplaceOperation); 
       } 
       else 
       { 
        Console.WriteLine("Entity could not be retrived."); 
       } 

       queue.DeleteMessage(retrievedMessage); 
      } 

     } 

,你不需要循環while(true)Run()Run()可以這樣簡單

public override void Run() 
     { 
      try 
      { 
       RunAsync(cancellationTokenSource.Token).Wait(); 
      } 
      finally 
      { 
       runCompleteEvent.Set(); 
      } 
     } 
+0

非常感謝,它工作很好! – oceankit

+0

@oceankit你能接受答案嗎? –