class ReindexNpmRepositoryManager extends StateGuardLifecycleSupport {

  @Override
  protected void doStart() {
    if (!enabled) {
      return;
    }
    try {
      for (Repository repository : repositoryManager.browse()) {
        if (unprocessedRepositoryChecker.isUnprocessedNpmRepository(repository)) {
          boolean existingTask = taskScheduler.findAndSubmit(ReindexNpmRepositoryTaskDescriptor.TYPE_ID,
              ImmutableMap.of(REPOSITORY_NAME_FIELD_ID, repository.getName()));
          if (!existingTask) {
            runReindexTaskForRepository(repository);
          }
        }
      }
    }
    catch (Exception e) {
      log.error("Failed to determine if any npm repositories needed to be reindexed", e);
    }
  }

  @Inject public  ReindexNpmRepositoryManager(final TaskScheduler taskScheduler,
                                     final RepositoryManager repositoryManager,
                                     final UnprocessedRepositoryChecker unprocessedRepositoryChecker,
                                     @Named("${nexus.npm.reindexOnStartup.enabled:-true}") final boolean enabled);

}