diff options
author | Bob Van Landuyt <bob@gitlab.com> | 2017-03-14 10:05:45 +0100 |
---|---|---|
committer | Bob Van Landuyt <bob@gitlab.com> | 2017-03-16 12:31:27 +0100 |
commit | e36f444930a5f2b0c0c4ca322125a6c831122e90 (patch) | |
tree | 22e9e73496846a18e1fb74fc95b781fbe2fde627 /app/models | |
parent | 56f6c6ac1636cd1131eea8c619205d4e8344223d (diff) | |
download | gitlab-ce-e36f444930a5f2b0c0c4ca322125a6c831122e90.tar.gz |
Add a wrapper for pipeline statuses
This reads status from redis & stores it in redis if it was missing.
If the project has no HEAD, the cache is deleted
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/ci/pipeline_status.rb | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/app/models/ci/pipeline_status.rb b/app/models/ci/pipeline_status.rb new file mode 100644 index 00000000000..c41c43868f9 --- /dev/null +++ b/app/models/ci/pipeline_status.rb @@ -0,0 +1,84 @@ +# 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 Ci + class PipelineStatus + attr_accessor :sha, :status, :project, :loaded + + delegate :commit, to: :project + + def self.load_for_project(project) + new(project).tap do |status| + status.load_status + end + end + + def initialize(project, sha: nil, status: nil) + @project = project + @sha = sha + @status = status + end + + def has_status? + loaded? && sha.present? && status.present? + end + + def load_status + return if loaded? + + if has_cache? + load_from_cache + else + load_from_commit + store_in_cache + end + + self.loaded = true + end + + def load_from_commit + self.sha = commit.sha + self.status = commit.status + 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 unless sha + return delete_from_cache unless commit + store_in_cache if commit.sha == self.sha + end + + def load_from_cache + Gitlab::Redis.with do |redis| + self.sha, self.status = redis.hmget(cache_key, :sha, :status) + end + end + + def store_in_cache + Gitlab::Redis.with do |redis| + redis.mapped_hmset(cache_key, { sha: sha, status: status }) + end + end + + def delete_from_cache + Gitlab::Redis.with do |redis| + redis.del(cache_key) + end + end + + def has_cache? + Gitlab::Redis.with do |redis| + redis.exists(cache_key) + end + end + + def loaded? + self.loaded + end + + def cache_key + "projects/#{project.id}/build_status" + end + end +end |