前回の記事で id:waritohutsu:20130715:1373856716 複数のストレージアカウントをアタッチする方法を紹介したが、今回は主要更新の一つである通知APIに Queue ストレージを利用する方法を紹介する。詳細な手順やサンプルコード全体は Listening to Windows Azure Media Services Notifications に記載されているため、本記事を通読後に一読することをお勧めする。
現時点でのQueue ストレージを利用した通知APIの考慮点は以下になる。
- 通知結果をQueueストレージに反映するのに最大15分かかる
- FIFO(First-In-First-Out)は保証されない
- Push通知ではないので、Push 通知が必要な場合は独自の実装が必要になる
- Queueストレージの制限に従うので Windows Azure Queues and Windows Azure Service Bus Queues - Compared and Contrasted を参照のこと
通知APIでQueue ストレージを利用する
まず、Media Service の通知結果を格納するためのストレージサービスを作成する。同ストレージサービスは Media Service にアタッチされているものでも構わない。以下に実行結果も含めたスクリーンキャプチャなので、構成の参考にしてほしい。
次に、Media Service SDK .NET版を利用した以下の様なC#コード(抜粋で恐縮だが)を作成する。
private static IJob EncodeToMp4(CloudMediaContext context, string inputAssetId) { var inputAsset = context.Assets.Where(a => a.Id == inputAssetId).FirstOrDefault(); if (inputAsset == null) throw new ArgumentException("Could not find assetId: " + inputAssetId); var encodingPreset = "H264 Adaptive Bitrate MP4 Set SD 16x9"; IJob job = context.Jobs.Create("Encoding " + inputAsset.Name + " to " + encodingPreset); //通知に利用する方法(Queueストレージ)とエンドポイント(myqueue)を指定 INotificationEndPoint notificationEndPoint = context.NotificationEndPoints.Create( Guid.NewGuid().ToString(), NotificationEndPointType.AzureQueue, "myqueue"); //全てのイベント(Schduled -> processing -> finished)で通知を行うことを設定 //job.JobNotificationSubscriptions.AddNew(NotificationJobState.FinalStatesOnly, notificationEndPoint); job.JobNotificationSubscriptions.AddNew(NotificationJobState.All, notificationEndPoint); IMediaProcessor latestWameMediaProcessor = (from p in context.MediaProcessors where p.Name == "Windows Azure Media Encoder" select p).ToList() .OrderBy(wame => new Version(wame.Version)).LastOrDefault(); ITask encodeTask = job.Tasks.AddNew("Encoding", latestWameMediaProcessor, encodingPreset, TaskOptions.None); encodeTask.InputAssets.Add(inputAsset); encodeTask.OutputAssets.AddNew(inputAsset.Name + " as " + encodingPreset, AssetCreationOptions.None); job.StateChanged += new EventHandler<JobStateChangedEventArgs>(JobStateChanged); job.Submit(); job.GetExecutionProgressTask(CancellationToken.None).Wait(); return job; }
現時点では NotificationEndPointType.AzureQueue しか通知の方法が用意されていないが、今後は通知方法の拡張が期待される。また、通知情報は"完了時のみ(NotificationJobState.FinalStatesOnly)" or "イベント毎の通知(NotificationJobState.All)" or "通知しない(NotificationJobState.None)" を選択が可能だ。
上記のコードを含めた処理を実行した結果、以下のJSON形式の文字列がQueueストレージに格納される。「1.登録 → 2.スケジュール → 3.処理中 → 4.完了」の流れが格納されていることが確認できる。
{"MessageVersion":"1.0","EventType":"NotificationEndPointRegistration","ETag":"a756c1f67e96d95273e38e5e5e918a77cbef37bdfaded6069eb0a8bd800bae81","TimeStamp":"2013-07-21T02:08:04","Properties":{"NotificationEndPointId":"nb:nepid:UUID:4e4199b8-3db1-43ea-851b-95576d17e60b","State":"Registered","Name":"8822be8d-2791-4ad0-a991-54ef1b74388a","Created":"2013-07-21T02:08:01"}} {"MessageVersion":"1.0","EventType":"JobStateChange","ETag":"f9cb50428bc9927d4f637d8efe9a82aa3de0c65ded214cde17ef378d5388e218","TimeStamp":"2013-07-21T02:08:17","Properties":{"JobId":"nb:jid:UUID:10a68b86-fe65-b943-bd2d-6ad2ba1cd3aa","JobName":"Encoding azure to H264 Adaptive Bitrate MP4 Set SD 16x9","NewState":"Scheduled","OldState":"Queued","AccountName":"xxxxxxxxxxxxxxxxxxx"}} {"MessageVersion":"1.0","EventType":"JobStateChange","ETag":"b70613d1c78821f96a05bda3c7ec16623424441ecd37b0b9417cbaa70761faab","TimeStamp":"2013-07-21T02:08:22","Properties":{"JobId":"nb:jid:UUID:10a68b86-fe65-b943-bd2d-6ad2ba1cd3aa","JobName":"Encoding azure to H264 Adaptive Bitrate MP4 Set SD 16x9","NewState":"Processing","OldState":"Scheduled","AccountName":"xxxxxxxxxxxxxxxxxxx"}} {"MessageVersion":"1.0","EventType":"JobStateChange","ETag":"74830e61be956a9c7566585d09dec148365806b42b9813405d4f97a1e1b3861a","TimeStamp":"2013-07-21T02:09:36","Properties":{"JobId":"nb:jid:UUID:10a68b86-fe65-b943-bd2d-6ad2ba1cd3aa","JobName":"Encoding azure to H264 Adaptive Bitrate MP4 Set SD 16x9","NewState":"Finished","OldState":"Processing","AccountName":"xxxxxxxxxxxxxxxxxxx"}}
下記のJSONオブジェクトを利用する場合は個別にクラスを作成する必要があるので、以下どちらかの手法を利用してほしい。
また、キューストレージのエンドポイントが不正な形式の場合、以下のレスポンスが帰ってくるので参考にしてほしい。
"<?xml version=\"1.0\" encoding=\"utf-8\"?><m:error xmlns:m=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\"><m:code /><m:message xml:lang=\"en-US\">NotificationEndPoint.EndPointAddress must match rules described in http://msdn.microsoft.com/en-us/library/windowsazure/dd179349.aspx</m:message></m:error>"