示例#1
0
        public static async Task Run(
            [TimerTrigger("* */1 * * * *", RunOnStartup = true)] TimerInfo timer,
            [Queue(Constants.IndexingQueue, Connection = Constants.IndexingQueueConnection)] ICollector <PackageOperation> queueCollector,
            ILogger logger)
        {
            var cursor = new InMemoryCursor(timer.ScheduleStatus?.Last ?? DateTimeOffset.UtcNow);

            var processor = new CatalogProcessor(
                cursor,
                new CatalogClient(HttpClient, new NullLogger <CatalogClient>()),
                new DelegatingCatalogLeafProcessor(
                    added =>
            {
                var packageVersion = added.ParsePackageVersion();

                queueCollector.Add(PackageOperation.ForAdd(
                                       added.PackageId,
                                       added.PackageVersion,
                                       added.VerbatimVersion,
                                       packageVersion.ToNormalizedString(),
                                       added.Published,
                                       string.Format(Constants.NuGetPackageUrlTemplate, added.PackageId, packageVersion.ToNormalizedString()).ToLowerInvariant(),
                                       added.IsListed()));

                return(Task.FromResult(true));
            },
                    deleted =>
            {
                queueCollector.Add(PackageOperation.ForDelete(
                                       deleted.PackageId,
                                       deleted.PackageVersion,
                                       deleted.ParsePackageVersion().ToNormalizedString()));

                return(Task.FromResult(true));
            }),
                new CatalogProcessorSettings
            {
                MinCommitTimestamp = timer.ScheduleStatus?.Last ?? DateTimeOffset.UtcNow,
                MaxCommitTimestamp = timer.ScheduleStatus?.Next ?? DateTimeOffset.UtcNow,
                ServiceIndexUrl    = "https://api.nuget.org/v3/index.json"
            },
                new NullLogger <CatalogProcessor>());

            await processor.ProcessAsync(CancellationToken.None);
        }
        public NuGetCatalogListener(string serviceIndexUrl, CloudBlockBlob cursorBlob, bool useBatchProcessor, int previousHours, ITriggeredFunctionExecutor executor, ILoggerFactory loggerFactory)
        {
            if (string.IsNullOrEmpty(serviceIndexUrl))
            {
                throw new ArgumentNullException(nameof(serviceIndexUrl));
            }
            if (executor == null)
            {
                throw new ArgumentNullException(nameof(executor));
            }

            async Task <bool> PackageDeleted(PackageDeleteCatalogLeaf deleted)
            {
                await executor.TryExecuteAsync(new TriggeredFunctionData
                {
                    TriggerValue = PackageOperation.ForDelete(
                        deleted.PackageId,
                        deleted.PackageVersion,
                        deleted.ParsePackageVersion().ToNormalizedString()),
                    TriggerDetails = new Dictionary <string, string>()
                }, CancellationToken.None);

                return(true);
            }

            async Task <bool> PackageAdded(PackageDetailsCatalogLeaf added)
            {
                var packageVersion = added.ParsePackageVersion();

                await executor.TryExecuteAsync(new TriggeredFunctionData
                {
                    TriggerValue = PackageOperation.ForAdd(
                        added.PackageId,
                        added.PackageVersion,
                        added.VerbatimVersion,
                        packageVersion.ToNormalizedString(),
                        added.Published,
                        GeneratePackageUrl(added.PackageId, packageVersion),
                        added.IsListed()),
                    TriggerDetails = new Dictionary <string, string>()
                }, CancellationToken.None);

                return(true);
            }

            var minCommitTimeStamp = DateTimeOffset.MinValue;

            if (previousHours > 0)
            {
                minCommitTimeStamp = DateTimeOffset.UtcNow
                                     .AddHours(Math.Abs(previousHours) * -1);
            }

            if (!useBatchProcessor)
            {
                _processor = new CatalogProcessor(
                    new CloudBlobCursor(cursorBlob),
                    new CatalogClient(HttpClient, loggerFactory.CreateLogger <CatalogClient>()),
                    new DelegatingCatalogLeafProcessor(PackageAdded, PackageDeleted),
                    new CatalogProcessorSettings {
                    ServiceIndexUrl = serviceIndexUrl, MinCommitTimestamp = minCommitTimeStamp
                },
                    loggerFactory.CreateLogger <CatalogProcessor>());
            }
            else
            {
                _processor = new BatchCatalogProcessor(
                    new CloudBlobCursor(cursorBlob),
                    new CatalogClient(HttpClient, loggerFactory.CreateLogger <CatalogClient>()),
                    new DelegatingCatalogLeafProcessor(PackageAdded, PackageDeleted),
                    new CatalogProcessorSettings {
                    ServiceIndexUrl = serviceIndexUrl, MinCommitTimestamp = minCommitTimeStamp
                },
                    loggerFactory.CreateLogger <BatchCatalogProcessor>());
            }
        }