2017-03-30 03:05:02 -04:00
|
|
|
# This class is not backed by a table in the main database.
|
|
|
|
# It loads the latest Pipeline for the HEAD of a repository, and caches that
|
|
|
|
# in Redis.
|
|
|
|
module Gitlab
|
|
|
|
module Cache
|
|
|
|
module Ci
|
2017-04-03 03:09:11 -04:00
|
|
|
class ProjectPipelineStatus
|
2017-04-06 08:20:29 -04:00
|
|
|
attr_accessor :sha, :status, :ref, :project, :loaded
|
2017-03-30 03:05:02 -04:00
|
|
|
|
|
|
|
delegate :commit, to: :project
|
|
|
|
|
|
|
|
def self.load_for_project(project)
|
|
|
|
new(project).tap do |status|
|
|
|
|
status.load_status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-26 08:04:22 -04:00
|
|
|
def self.load_in_batch_for_projects(projects)
|
|
|
|
cached_results_for_projects(projects).zip(projects).each do |result, project|
|
|
|
|
project.pipeline_status = new(project, result)
|
|
|
|
project.pipeline_status.load_status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.cached_results_for_projects(projects)
|
2017-07-10 23:35:47 -04:00
|
|
|
result = Gitlab::Redis::Cache.with do |redis|
|
2017-04-26 08:04:22 -04:00
|
|
|
redis.multi do
|
|
|
|
projects.each do |project|
|
|
|
|
cache_key = cache_key_for_project(project)
|
|
|
|
redis.exists(cache_key)
|
|
|
|
redis.hmget(cache_key, :sha, :status, :ref)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result.each_slice(2).map do |(cache_key_exists, (sha, status, ref))|
|
|
|
|
pipeline_info = { sha: sha, status: status, ref: ref }
|
|
|
|
{ loaded_from_cache: cache_key_exists, pipeline_info: pipeline_info }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.cache_key_for_project(project)
|
|
|
|
"projects/#{project.id}/pipeline_status"
|
|
|
|
end
|
|
|
|
|
2017-03-31 09:47:34 -04:00
|
|
|
def self.update_for_pipeline(pipeline)
|
2017-04-26 08:04:22 -04:00
|
|
|
pipeline_info = {
|
|
|
|
sha: pipeline.sha,
|
|
|
|
status: pipeline.status,
|
|
|
|
ref: pipeline.ref
|
|
|
|
}
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
new(pipeline.project, pipeline_info: pipeline_info)
|
|
|
|
.store_in_cache_if_needed
|
2017-03-31 09:47:34 -04:00
|
|
|
end
|
|
|
|
|
2017-04-26 08:04:22 -04:00
|
|
|
def initialize(project, pipeline_info: {}, loaded_from_cache: nil)
|
2017-03-30 03:05:02 -04:00
|
|
|
@project = project
|
2017-04-26 08:04:22 -04:00
|
|
|
@sha = pipeline_info[:sha]
|
|
|
|
@ref = pipeline_info[:ref]
|
|
|
|
@status = pipeline_info[:status]
|
|
|
|
@loaded = loaded_from_cache
|
2017-03-30 03:05:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def has_status?
|
|
|
|
loaded? && sha.present? && status.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_status
|
|
|
|
return if loaded?
|
|
|
|
|
|
|
|
if has_cache?
|
|
|
|
load_from_cache
|
|
|
|
else
|
2017-04-06 08:20:29 -04:00
|
|
|
load_from_project
|
2017-03-30 03:05:02 -04:00
|
|
|
store_in_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
self.loaded = true
|
|
|
|
end
|
|
|
|
|
2017-04-06 08:20:29 -04:00
|
|
|
def load_from_project
|
2017-03-30 03:05:02 -04:00
|
|
|
return unless commit
|
|
|
|
|
|
|
|
self.sha = commit.sha
|
|
|
|
self.status = commit.status
|
2017-04-06 08:20:29 -04:00
|
|
|
self.ref = project.default_branch
|
2017-03-30 03:05:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# We only cache the status for the HEAD commit of a project
|
|
|
|
# This status is rendered in project lists
|
|
|
|
def store_in_cache_if_needed
|
|
|
|
return delete_from_cache unless commit
|
2017-04-06 08:20:29 -04:00
|
|
|
return unless sha
|
|
|
|
return unless ref
|
|
|
|
|
|
|
|
if commit.sha == sha && project.default_branch == ref
|
|
|
|
store_in_cache
|
|
|
|
end
|
2017-03-30 03:05:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def load_from_cache
|
2017-07-10 23:35:47 -04:00
|
|
|
Gitlab::Redis::Cache.with do |redis|
|
2017-04-06 08:20:29 -04:00
|
|
|
self.sha, self.status, self.ref = redis.hmget(cache_key, :sha, :status, :ref)
|
2017-03-30 03:05:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def store_in_cache
|
2017-07-10 23:35:47 -04:00
|
|
|
Gitlab::Redis::Cache.with do |redis|
|
2017-04-06 08:20:29 -04:00
|
|
|
redis.mapped_hmset(cache_key, { sha: sha, status: status, ref: ref })
|
2017-03-30 03:05:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_from_cache
|
2017-07-10 23:35:47 -04:00
|
|
|
Gitlab::Redis::Cache.with do |redis|
|
2017-03-30 03:05:02 -04:00
|
|
|
redis.del(cache_key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_cache?
|
2017-04-26 08:04:22 -04:00
|
|
|
return self.loaded unless self.loaded.nil?
|
|
|
|
|
2017-07-10 23:35:47 -04:00
|
|
|
Gitlab::Redis::Cache.with do |redis|
|
2017-03-30 03:05:02 -04:00
|
|
|
redis.exists(cache_key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def loaded?
|
|
|
|
self.loaded
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_key
|
2017-04-26 08:04:22 -04:00
|
|
|
self.class.cache_key_for_project(project)
|
2017-03-30 03:05:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|