Пример #1
0
func (bc *baggageCollector) getResourceHashVersions() (resourceHashVersion, error) {
	bc.logger.Session("ranking-resource-versions")
	resourceHash := resourceHashVersion{}

	pipelines, err := bc.db.GetAllActivePipelines()
	if err != nil {
		bc.logger.Error("could-not-get-active-pipelines", err)
		return nil, err
	}

	for _, pipeline := range pipelines {
		pipelineDB := bc.pipelineDBFactory.Build(pipeline)
		pipelineResources := pipeline.Config.Resources

		for _, pipelineResource := range pipelineResources {
			pipelineResourceVersions, _, err := pipelineDB.GetResourceVersions(pipelineResource.Name, db.Page{Limit: 5})
			if err != nil {
				bc.logger.Error("could-not-get-resource-history", err)
				return nil, err
			}

			versionRank := 0
			for _, pipelineResourceVersion := range pipelineResourceVersions {
				if pipelineResourceVersion.Enabled {

					version, _ := json.Marshal(pipelineResourceVersion.VersionedResource.Version)
					hashKey := string(version) + resource.GenerateResourceHash(pipelineResource.Source, pipelineResource.Type)

					if rank, ok := resourceHash[hashKey]; ok {
						resourceHash[hashKey] = min(rank, versionRank)
					} else {
						resourceHash[hashKey] = versionRank
					}

					versionRank++
				}
			}
		}
	}

	return resourceHash, nil
}
Пример #2
0
func (bc *baggageCollector) getLatestVersionSet() (hashedVersionSet, error) {
	latestVersions := hashedVersionSet{}

	pipelines, err := bc.db.GetAllPipelines()
	if err != nil {
		bc.logger.Error("could-not-get-active-pipelines", err)
		return nil, err
	}

	for _, pipeline := range pipelines {
		pipelineDB := bc.pipelineDBFactory.Build(pipeline)
		pipelineResources := pipeline.Config.Resources

		for _, pipelineResource := range pipelineResources {
			logger := bc.logger.WithData(lager.Data{
				"pipeline": pipeline.Name,
				"resource": pipelineResource.Name,
			})

			latestEnabledVersion, found, err := pipelineDB.GetLatestEnabledVersionedResource(pipelineResource.Name)
			if err != nil {
				logger.Error("could-not-get-latest-enabled-resource", err)
				return nil, err
			}

			if !found {
				continue
			}

			version, _ := json.Marshal(latestEnabledVersion.VersionedResource.Version)
			hashKey := string(version) + resource.GenerateResourceHash(
				pipelineResource.Source, pipelineResource.Type,
			)
			insertOrIncreaseVersionTTL(latestVersions, hashKey, 0) // live forever
		}

		for _, pipelineJob := range pipeline.Config.Jobs {
			logger := bc.logger.WithData(lager.Data{
				"pipeline": pipeline.Name,
				"job":      pipelineJob.Name,
			})

			finished, _, err := pipelineDB.GetJobFinishedAndNextBuild(pipelineJob.Name)
			if err != nil {
				logger.Error("could-not-acquire-finished-and-next-builds-for-job", err)
				return nil, err
			}

			if finished != nil {
				volumeIdentifiers, err := bc.db.GetImageVolumeIdentifiersByBuildID(finished.ID)
				if err != nil {
					logger.Error("could-not-acquire-volume-identifiers-for-build", err)
					return nil, err
				}

				for _, identifier := range volumeIdentifiers {
					version, _ := json.Marshal(identifier.ResourceVersion)
					hashKey := string(version) + identifier.ResourceHash
					insertOrIncreaseVersionTTL(latestVersions, hashKey, 0) // live forever
				}
			}
		}
	}

	logger := bc.logger.Session("image-resources-for-one-off-builds")
	oneOffImageResourceVolumes, err := bc.db.GetVolumesForOneOffBuildImageResources()
	if err != nil {
		logger.Error("could-not-get-volumes-for-one-off-build-image-resources", err)
		return nil, err
	}
	for _, savedVolume := range oneOffImageResourceVolumes {
		identifier := savedVolume.Volume.VolumeIdentifier
		version, _ := json.Marshal(identifier.ResourceVersion)
		hashKey := string(version) + identifier.ResourceHash
		insertOrIncreaseVersionTTL(latestVersions, hashKey, bc.oneOffBuildImageResourceGracePeriod)
	}

	return latestVersions, nil
}
			}

			fakeSavedVersionedResource = db.SavedVersionedResource{
				ID:           123,
				Enabled:      true,
				ModifiedTime: time.Now(),
				VersionedResource: db.VersionedResource{
					Resource:     "our-resource",
					Type:         "git",
					Version:      db.Version{"some": "newest-version"},
					PipelineName: "some-pipeline",
				},
			}

			hashkey := resource.GenerateResourceHash(
				fakeSavedPipeline.Config.Resources[0].Source,
				fakeSavedPipeline.Config.Resources[0].Type,
			)
			newestReturnedSavedVolume = db.SavedVolume{
				Volume: db.Volume{
					WorkerName: "a-new-worker",
					TTL:        0,
					Handle:     "some-other-handle",
					VolumeIdentifier: db.VolumeIdentifier{
						ResourceVersion: atc.Version{"some": "newest-version"},
						ResourceHash:    hashkey,
					},
				},
				ID:        124,
				ExpiresIn: 0,
			}