示例#1
0
        public static async Task SaveClaimToDb(
            [ActivityTrigger] WhatIfDemoDbDataContext.Claim claim,
            ILogger log)
        {
            var ctx = await WhatIfDemoDbDataContext.CreateAsync();

            ctx.Claims.Add(claim);

            await ctx.SaveChangesIdempotentlyAsync(ex => {
                // Explicitly handling the case of duplicated execution.
                // Which might happen, if the process crashes or restarts.
                log.LogError($"Failed to add policy {claim.id} from userId {claim.userId}: {ex.Message}");
            });

            log.LogWarning($"Saved claim {claim.id} from userId {claim.userId}");
        }
示例#2
0
        public static async Task SendMobileNotification(
            [ActivityTrigger] WhatIfDemoDbDataContext.Claim claim,
            ILogger log)
        {
            // Notifications binding is not yet available for Functions 2.0, unfortunately.
            // So just using NotificationHubClient manually.
            var hub = NotificationHubClient.CreateClientFromConnectionString(Environment.GetEnvironmentVariable(NotificationHubConnectionVariableName), Environment.GetEnvironmentVariable(NotificationHubPathVariableName));

            var payload = new
            {
                message = $"Click to approve claim {claim.id} from user {claim.userId} with license {claim.licenseId}",
                uri     = $"{Helpers.GetHostName()}/api/{nameof(ApproveClaim)}?claimId={claim.id}"
            };

            await hub.SendNotificationAsync(new FcmNotification($"{{\"data\":{payload.ToJson()}}}"));

            log.LogWarning($"Notification about claim {claim.id} from user {claim.userId} with license {claim.licenseId} sent!");
        }
示例#3
0
        public static async Task ProcessClaim(
            [OrchestrationClient] DurableOrchestrationClient orchestrationClient,
            [BlobTrigger("claims/{fileFullName}", Connection = "AzureWebJobsStorage")] Stream fileStream,
            string fileFullName,
            ILogger log)
        {
            // Expecting the file name be in form '<userId>/<fileName>'
            var fileNameParts = fileFullName.Split('/');

            if (fileNameParts.Length != 2)
            {
                return;
            }

            string userId   = fileNameParts[0];
            string fileName = fileNameParts[1];

            if (fileName != DriversLicenseFileName)
            {
                return;
            }

            // Analyzing fileStream before starting a Saga, since fileStream isn't serializable
            // and since there's a built-in retry logic for BlobTriggers anyway.
            string licenseId = await ExtractLicenseId(fileStream);

            log.LogWarning($"Extracted driving license id {licenseId}, starting claim processing for user {userId}");

            var claim = new WhatIfDemoDbDataContext.Claim
            {
                id          = Guid.NewGuid(),
                userId      = userId,
                licenseId   = licenseId,
                dateCreated = DateTime.UtcNow,
                amount      = new Random().Next(1, 1000)
            };

            // Starting the claim processing Saga
            await orchestrationClient.StartNewAsync(nameof(ProcessClaimOrchestrator), claim.id.ToString(), claim);
        }