Last active
April 16, 2019 09:32
-
-
Save noriyukitakei/96b43d2b394da3150099834eb9081ffb to your computer and use it in GitHub Desktop.
多分わかりやすいDurable Functios【Durable Functionsを使わない場合(Azure Functions)】
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Runtime.Serialization; | |
using System.Runtime.Serialization.Json; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Xml; | |
using System.Xml.Linq; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.Azure.WebJobs.Host; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Blob; | |
using Microsoft.WindowsAzure.Storage.Table; | |
namespace BackupSiteContentByAzureFunctions | |
{ | |
public static class Functions | |
{ | |
private const string AZURE_STORAGE_CONNECTION_STRING = "UseDevelopmentStorage=true"; | |
[FunctionName("CollectBlogTaggedByAzure")] | |
public static async void Run([QueueTrigger("task-queue")]string myQueueItem, ILogger log) | |
{ | |
// Azure Queue Storageに登録されたメッセージを読み込む。メッセージの形式は以下の通りとなる。 | |
// { | |
// "jobId":"[集計対象のURLをグルーピングした一意のIDd]", | |
// "url":"[集計対象のWebサイトのURL]" | |
// } | |
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(RssMessage)); | |
RssMessage rssMessage = new RssMessage(); | |
byte[] bytes = Encoding.UTF8.GetBytes(myQueueItem); | |
using (MemoryStream ms = new MemoryStream(bytes)) | |
{ | |
rssMessage = (RssMessage)serializer.ReadObject(ms); | |
} | |
// 集計対象のWebサイトのRSSのURLを読み込み、azureというカテゴリの記事の数をカウントする。 | |
XDocument xml = XDocument.Load(rssMessage.url); | |
XElement rss = xml.Element("rss"); | |
XElement channel = rss.Element("channel"); | |
var items = channel.Elements("item"); | |
int count = 0; | |
foreach (XElement item in items) | |
{ | |
var categories = item.Elements("category"); | |
foreach (XElement category in categories) | |
{ | |
Console.WriteLine(category.Value); | |
if (category.Value.ToLower().Contains("azure")) | |
{ | |
count++; | |
break; | |
} | |
} | |
} | |
// Azure Queue Storageへの接続設定を取得する。 | |
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(AZURE_STORAGE_CONNECTION_STRING); | |
// Azure Table Storageのテーブルを作成する。 | |
CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); | |
CloudTable table = tableClient.GetTableReference("TaskHistory"); | |
await table.CreateIfNotExistsAsync(); | |
// 集計対象のURLをbase64エンコードして登録する。(Azure Table StorageのRowKeyにはASCII文字しか入らない) | |
string urlConvertedToBase64 = Convert.ToBase64String(Encoding.GetEncoding("UTF-8").GetBytes(rssMessage.url)); | |
TableOperation retrieveOperation = TableOperation.Retrieve<RssCountTaskEntity>(rssMessage.jobId, urlConvertedToBase64); | |
TableResult retrievedResult = await table.ExecuteAsync(retrieveOperation); | |
RssCountTaskEntity updateEntity = (RssCountTaskEntity)retrievedResult.Result; | |
if (updateEntity != null) { | |
updateEntity.Count = count.ToString(); | |
updateEntity.Status = "success"; | |
// Create the Replace TableOperation. | |
TableOperation updateOperation = TableOperation.Replace(updateEntity); | |
// Execute the operation. | |
await table.ExecuteAsync(updateOperation); | |
} | |
} | |
// Azure Queue Storageに登録したメッセージ(JSON)をオブジェクトに変換するためのDTO | |
[DataContract] | |
class RssMessage | |
{ | |
[DataMember] | |
public string jobId { get; set; } | |
[DataMember] | |
public string url { get; set; } | |
} | |
// Azure Table Storageに登録するレコードのEntity | |
public class RssCountTaskEntity : TableEntity | |
{ | |
public RssCountTaskEntity(string jobId, string path) | |
{ | |
this.PartitionKey = jobId; | |
this.RowKey = path; | |
} | |
public RssCountTaskEntity() { } | |
public string Count { get; set; } | |
public string Status { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment