示例#1
0
        public static async Task DeliveryPostage(
            [QueueTrigger(QueueNames.CommSendPostage)] MessageDispatch sendMessage,
            [Table(TableNames.CommSchedule, CommSchedulePartitionKeys.InProgress, "{RowKey}")] TableEntityAdapter <MessageSchedule> schedule,
            [Table(TableNames.CommMessage, CommMessagePartitionKeys.Created, "{MessageReference}")] TableEntityAdapter <Message> message,
            [Table(TableNames.CommSchedule)] CloudTable scheduleTable,
            [Inject] App app,
            [Inject] IScheduleIdGenerator idGenerator,
            ILogger log)
        {
            if (schedule == null)
            {
                return;
            }

            var postage = message.GetMessageOf <Postage>();

            // handle unexpected non existing message
            if (postage == null)
            {
                await schedule.MoveTo(scheduleTable, s => CommSchedulePartitionKeys.Skipped).ConfigureAwait(false);

                return;
            }

            await schedule.MarkScheduleSent(scheduleTable, app, idGenerator);
        }
示例#2
0
        public async Task ProcessWebhookEvents(
            [QueueTrigger(WebhookQueueNames.ProcessWebhookEvents)] Tuple <string, string> keys,
            [Table(nameof(WebhookContainerName.WebhookPayloads), nameof(PartitionKeyValue.New), "{Item2}")] TableEntityAdapter <PayloadContent> context,
            [CosmosDB(nameof(DatabaseName.Sufong2001), nameof(WebhookContainerName.WebhookPayloads), ConnectionStringSetting = DatabaseConfig.CosmosDbConnectionString)] IAsyncCollector <InvoiceEntity> invoicesOut,
            [Table(nameof(WebhookContainerName.WebhookPayloads))] CloudTable payloadTable,
            ILogger log)
        {
            if (context == null)
            {
                return;
            }

            var getInvoices = context.OriginalEntity.Payload.To <Payload>().Events
                              .Where(e => e.EventCategory == "INVOICE")
                              .GroupBy(e => e.TenantId)
                              .Select(GetInvoicesAsync);

            // Get Invoice details from Xero
            var result = await Task.WhenAll(getInvoices);

            var invoices = result.SelectMany(i => i);

            var operations = invoices.Select(i => invoicesOut.AddAsync(i));

            await Task.WhenAll(operations);

            await context.MoveTo(payloadTable, p => nameof(PartitionKeyValue.Processed));
        }
示例#3
0
        public static async Task <IActionResult> End(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = ServiceNames.UploadEnd + "/{session}/{filename?}")]
            HttpRequest req,
            string session,
            string filename,
            [Blob(BlobNames.UploadDirectory + "/{session}")] CloudBlobDirectory uploadDir,
            [Table(TableNames.CommUpload, CommUploadPartitionKeys.Temp, "{session}")] TableEntityAdapter <UploadSession> upload,
            [Table(TableNames.CommUpload)] CloudTable uploadTable,
            [Queue(QueueNames.CommProcess)] CloudQueue processQueue,
            [Inject] App app,
            ILogger log)
        {
            try
            {
                if (session.IsNullOrEmpty())
                {
                    throw new ArgumentException("The Session Id is required.", nameof(session));
                }

                // save the file
                var uploadTo = await req.Body.UploadTo(uploadDir, filename);

                #region unexpected behaviour notes

                /*
                 * update the OriginalEntity.Property value has cause some funny error without cloning the object
                 * System.Private.CoreLib: Exception while executing function: TransferEnd.
                 * Microsoft.Azure.WebJobs.Host: Error while handling parameter upload after function returned:.
                 * Microsoft.WindowsAzure.Storage: Not Found.
                 *
                 * upload.OriginalEntity.UploadEnd = app.DateTimeNow;
                 */

                #endregion unexpected behaviour notes

                UploadSession UpdateOriginalEntity(UploadSession uploadSession)
                {
                    uploadSession.UploadEnd        = app.DateTimeNow;
                    uploadSession.ManifestFile     = new[] { upload.OriginalEntity.ManifestFile, filename }.IsIfManifest();
                    uploadSession.LastUploadedFile = new[] { filename, uploadSession.LastUploadedFile }.FirstIsNotEmpty();
                    return(uploadSession);
                }

                // close the upload session
                upload = await upload.MoveTo(uploadTable
                                             , uploadSession => CommUploadPartitionKeys.Completed
                                             , updateOriginalEntity : UpdateOriginalEntity
                                             );

                // raise the UploadCompleted event
                await new UploadCompleted {
                    SessionId = session
                }.AddMessageToAsync(processQueue);

                return(new OkObjectResult(upload.OriginalEntity));
            }
            catch (Exception ex)
            {
                log.LogError(ex.StackTrace);
                return(new BadRequestObjectResult(new UploadSession {
                    Errors = ex.Message
                }));
            }
        }