When should NuGet Gallery search index update?

1.5k views Asked by At

I have recently installed a local NuGet Gallery from the instructions on GitHub.

It appears to be working correctly when I upload packages through the UI, but packages pushed using the command line don't show up in search results. In the packages windows it says "Search Index last updated 55 minutes ago". This corresponds to when I last published the website. What determines when the search index runs? A quick look at the code makes it look like it should happen whenever you add/remove a package, but it doesn't appear to be doing this.

How can I increase the indexing frequency?

1

There are 1 answers

0
Nico Vega On

In the NuGetGallery project, go to the CreatePackageInternal method in /Controllers/ApiController and call this line before the return statement.

IndexingService.UpdateIndex(true);

Your code must be something like this

    private async Task<ActionResult> CreatePackageInternal()
    {
        // Get the user
        var user = GetCurrentUser();

        using (var packageToPush = ReadPackageFromRequest())
        {
            if (packageToPush.Metadata.MinClientVersion > typeof(Manifest).Assembly.GetName().Version)
            {
                return new HttpStatusCodeWithBodyResult(HttpStatusCode.BadRequest, String.Format(
                    CultureInfo.CurrentCulture,
                    Strings.UploadPackage_MinClientVersionOutOfRange,
                    packageToPush.Metadata.MinClientVersion));
            }

            // Ensure that the user can push packages for this partialId.
            var packageRegistration = PackageService.FindPackageRegistrationById(packageToPush.Metadata.Id);
            if (packageRegistration != null)
            {
                if (!packageRegistration.IsOwner(user))
                {
                    return new HttpStatusCodeWithBodyResult(HttpStatusCode.Forbidden, Strings.ApiKeyNotAuthorized);
                }

                // Check if a particular Id-Version combination already exists. We eventually need to remove this check.
                string normalizedVersion = packageToPush.Metadata.Version.ToNormalizedString();
                bool packageExists =
                    packageRegistration.Packages.Any(
                        p => String.Equals(
                            p.NormalizedVersion,
                            normalizedVersion,
                            StringComparison.OrdinalIgnoreCase));

                if (packageExists)
                {
                    return new HttpStatusCodeWithBodyResult(
                        HttpStatusCode.Conflict,
                        String.Format(CultureInfo.CurrentCulture, Strings.PackageExistsAndCannotBeModified,
                                      packageToPush.Metadata.Id, packageToPush.Metadata.Version.ToNormalizedStringSafe()));
                }
            }

            var package = PackageService.CreatePackage(packageToPush, user, commitChanges: false);
            AutoCuratePackage.Execute(package, packageToPush, commitChanges: false);
            EntitiesContext.SaveChanges();

            using (Stream uploadStream = packageToPush.GetStream())
            {
                await PackageFileService.SavePackageFileAsync(package, uploadStream);
            }
        }

        IndexingService.UpdateIndex(true);

        return new HttpStatusCodeResult(HttpStatusCode.Created);
    }