diff options
Diffstat (limited to 'app/models/design_management/design_collection.rb')
-rw-r--r-- | app/models/design_management/design_collection.rb | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/app/models/design_management/design_collection.rb b/app/models/design_management/design_collection.rb index 96d5f4c2419..c48b36588c9 100644 --- a/app/models/design_management/design_collection.rb +++ b/app/models/design_management/design_collection.rb @@ -6,8 +6,34 @@ module DesignManagement delegate :designs, :project, to: :issue + state_machine :copy_state, initial: :ready, namespace: :copy do + after_transition any => any, do: :update_stored_copy_state! + + event :start do + transition ready: :in_progress + end + + event :end do + transition in_progress: :ready + end + + event :error do + transition in_progress: :error + end + + event :reset do + transition any => :ready + end + end + def initialize(issue) + super() # Necessary to initialize state_machine + @issue = issue + + if stored_copy_state = get_stored_copy_state + @copy_state = stored_copy_state + end end def ==(other) @@ -30,5 +56,39 @@ module DesignManagement def designs_by_filename(filenames) designs.current.where(filename: filenames) end + + private + + def update_stored_copy_state! + # As "ready" is the initial copy state we can clear the cached value + # rather than persist it. + if copy_ready? + unset_store_copy_state! + else + set_stored_copy_state! + end + end + + def copy_state_cache_key + "DesignCollection/copy_state/issue=#{issue.id}" + end + + def get_stored_copy_state + Gitlab::Redis::SharedState.with do |redis| + redis.get(copy_state_cache_key) + end + end + + def set_stored_copy_state! + Gitlab::Redis::SharedState.with do |redis| + redis.set(copy_state_cache_key, copy_state) + end + end + + def unset_store_copy_state! + Gitlab::Redis::SharedState.with do |redis| + redis.del(copy_state_cache_key) + end + end end end |