예제 #1
0
        // This function will get triggered/executed when a new message is written
        // on an Azure Queue called queue.
        public static void ProcessQueueMessage([QueueTrigger(PnPPartnerPackSettings.StorageQueueName)] ContinousJobItem content, TextWriter log)
        {
            // Attach TextWriter log to Trace
            // https://blog.josequinto.com/2017/02/16/enable-azure-invocation-log-at-a-web-job-function-level-for-pnp-provisioning/
            TextWriterTraceListener twtl = new TextWriterTraceListener(log);

            twtl.Name = "ContinousJobLogger";
            string[] notShownWords = new string[] { "TokenCache", "AcquireTokenHandlerBase" };
            twtl.Filter = new RemoveWordsFilter(notShownWords);
            Trace.Listeners.Add(twtl);
            Trace.AutoFlush = true;

            log.WriteLine(String.Format("Found Job: {0}", content.JobId));

            // Get the info about the Provisioning Job
            ProvisioningJobInformation jobInfo =
                ProvisioningRepositoryFactory.Current.GetProvisioningJob(content.JobId, true);

            // Get a reference to the Provisioning Job
            ProvisioningJob job = jobInfo.JobFile.FromJsonStream(jobInfo.Type);

            if (PnPPartnerPackSettings.ContinousJobHandlers.ContainsKey(job.GetType()))
            {
                PnPPartnerPackSettings.ContinousJobHandlers[job.GetType()].RunJob(job);
            }

            log.WriteLine("Completed Job execution");

            // Remove Trace Listener
            Trace.Listeners.Remove(twtl);
        }
예제 #2
0
        // This function will get triggered/executed when a new message is written
        // on an Azure Queue called queue.
        public static void ProcessQueueMessage([QueueTrigger(PnPPartnerPackSettings.StorageQueueName)] ContinousJobItem content, TextWriter log)
        {
            log.WriteLine(String.Format("Found Job: {0}", content.JobId));

            // Get the info about the Provisioning Job
            ProvisioningJobInformation jobInfo =
                ProvisioningRepositoryFactory.Current.GetProvisioningJob(content.JobId, true);

            // Get a reference to the Provisioning Job
            ProvisioningJob job = jobInfo.JobFile.FromJsonStream(jobInfo.Type);

            if (PnPPartnerPackSettings.ContinousJobHandlers.ContainsKey(job.GetType()))
            {
                PnPPartnerPackSettings.ContinousJobHandlers[job.GetType()].RunJob(job);
            }

            log.WriteLine("Completed Job execution");
        }
        public Guid EnqueueProvisioningJob(ProvisioningJob job)
        {
            // Prepare the Job ID
            Guid jobId = Guid.NewGuid();

            // Connect to the Infrastructural Site Collection
            using (var context = PnPPartnerPackContextProvider.GetAppOnlyClientContext(PnPPartnerPackSettings.InfrastructureSiteUrl))
            {
                // Set the initial status of the Job
                job.JobId  = jobId;
                job.Status = ProvisioningJobStatus.Pending;

                // Convert the current Provisioning Job into a Stream
                Stream stream = job.ToJsonStream();

                // Get a reference to the target library
                Web  web  = context.Web;
                List list = web.Lists.GetByTitle(PnPPartnerPackConstants.PnPProvisioningJobs);
                Microsoft.SharePoint.Client.File file = list.RootFolder.UploadFile(String.Format("{0}.job", jobId), stream, false);

                Microsoft.SharePoint.Client.User ownerUser = web.EnsureUser(job.Owner);
                context.Load(ownerUser);
                context.ExecuteQueryRetry();

                ListItem item = file.ListItemAllFields;

                item[PnPPartnerPackConstants.ContentTypeIdField]       = PnPPartnerPackConstants.PnPProvisioningJobContentTypeId;
                item[PnPPartnerPackConstants.TitleField]               = job.Title;
                item[PnPPartnerPackConstants.PnPProvisioningJobStatus] = ProvisioningJobStatus.Pending.ToString();
                item[PnPPartnerPackConstants.PnPProvisioningJobError]  = String.Empty;
                item[PnPPartnerPackConstants.PnPProvisioningJobType]   = job.GetType().FullName;

                FieldUserValue ownerUserValue = new FieldUserValue();
                ownerUserValue.LookupId = ownerUser.Id;
                item[PnPPartnerPackConstants.PnPProvisioningJobOwner] = ownerUserValue;

                item.Update();

                context.ExecuteQueryRetry();

                // Check if we need to enqueue a message in the Azure Storage Queue, as well
                // This happens when the Provisioning Job has to be executed in Continous mode
                if (PnPPartnerPackSettings.ContinousJobHandlers.ContainsKey(job.GetType()))
                {
                    // Get the storage account for Azure Storage Queue
                    CloudStorageAccount storageAccount =
                        CloudStorageAccount.Parse(PnPPartnerPackSettings.StorageQueueConnectionString);

                    // Get queue ... and create if it does not exist
                    CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
                    CloudQueue       queue       = queueClient.GetQueueReference(PnPPartnerPackSettings.StorageQueueName);
                    queue.CreateIfNotExists();

                    // Add entry to queue
                    ContinousJobItem content = new ContinousJobItem {
                        JobId = job.JobId
                    };
                    queue.AddMessage(new CloudQueueMessage(JsonConvert.SerializeObject(content)));
                }
            }

            return(jobId);
        }