Commit 9ba1c009 authored by Gabriel Mazetto's avatar Gabriel Mazetto

RefreshKey endpoint, worker and service

parent 5fcaad9d
module Geo
class ScheduleKeyChangeService
attr_reader :id, :action
def initialize(key_change)
@id = key_change['id']
@action = key_change['action']
end
def execute
GeoKeyRefreshWorker.perform_async(project['id'], project['clone_url'])
end
end
end
class GeoKeyRefreshWorker
include Sidekiq::Worker
sidekiq_options queue: :default, retry: 55
sidekiq_retry_in do |count|
count <= 30 ? linear_backoff_strategy(count) : geometric_backoff_strategy(count)
end
def perform(key_id, action)
action = action.to_sym
case action
when :create
# ActiveRecord::RecordNotFound when not found (so job will retry)
key = Key.find(key_id)
key.add_to_shell
when :delete
# ActiveRecord::RecordNotFound when not found (so job will retry)
key = Key.find(key_id)
key.remove_from_shell
end
end
private
def linear_backoff_strategy(count)
rand(1..20) + count
end
def geometric_backoff_strategy(count)
# This strategy is based on the original one from sidekiq
count = count-30 # we must start counting after 30
(count ** 4) + 15 + (rand(30)*(count+1))
end
end
......@@ -3,12 +3,11 @@ module API
before { authenticated_as_admin! }
resource :geo do
# Enqueue a batch of IDs of modified projects to have their
# repositories updated
#
# Example request:
# POST /refresh_projects
# POST /geo/refresh_projects
post 'refresh_projects' do
required_attributes! [:projects]
::Geo::ScheduleRepoUpdateService.new(params[:projects]).execute
......@@ -18,11 +17,20 @@ module API
# wiki repositories updated
#
# Example request:
# POST /refresh_wikis
# POST /geo/refresh_wikis
post 'refresh_wikis' do
required_attributes! [:projects]
::Geo::ScheduleWikiRepoUpdateService.new(params[:projects]).execute
end
# Enqueue a change operation for specific key ID
#
# Example request:
# POST /geo/refresh_key
post 'refresh_key' do
required_attributes! [:key_change]
::Geo::ScheduleKeyChangeService.new(params[:key_change]).execute
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment