Skip to content

Commit

Permalink
refactor to merge various project model and controller build methods,…
Browse files Browse the repository at this point in the history
… added `wait_until` param to build api, project.build checks for unique lock on project_job before doing anything
  • Loading branch information
ryanmark committed Jul 18, 2018
1 parent a56e309 commit e6574f1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 50 deletions.
26 changes: 18 additions & 8 deletions app/controllers/autotune/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,26 +213,36 @@ def create_google_doc
render :json => { :google_doc_url => doc_copy[:url] }
end

def update_snapshot
instance.update_snapshot(current_user)
render_accepted
end

def cancel_repeat_build
instance.cancel_repeat_build!
render_accepted
end

def build
kwargs = {}
if params[:repeat_until].present?
instance.repeat_build!(Time.zone.at(params[:repeat_until].to_i))
kwargs[:repeat_until] = Time.zone.at(params[:repeat_until].to_i)
end
if params[:wait_until].present?
kwargs[:wait_until] = Time.zone.at(params[:wait_until].to_i)
end
if params[:publish].present? && params[:publish]
kwargs[:publish] = true
end
instance.build(current_user)
if params[:update].present? && params[:update]
kwargs[:update] = true
end
instance.build(current_user, kwargs)
render_accepted
end

def build_and_publish
instance.build_and_publish(current_user)
instance.build(current_user, :publish => True)
render_accepted
end

def update_snapshot
instance.build(current_user, :update => True)
render_accepted
end

Expand Down
12 changes: 8 additions & 4 deletions app/jobs/autotune/project_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def perform(project, update: false, target: :preview, current_user: nil)
if project.bespoke?
project.sync_from_remote(:update => update, :current_user => current_user)
else
# make sure blueprint is synced before syncing from it
if project.blueprint.needs_sync?
# make sure blueprint is synced before syncing from it
project.blueprint.with_file_lock do |has_lock|
if has_lock
project.blueprint.sync_from_remote(:current_user => current_user)
Expand Down Expand Up @@ -94,8 +94,8 @@ def perform(project, update: false, target: :preview, current_user: nil)
project.file_unlock!
project.save!

project.build(current_user) if project.repeat_build?
rescue => exc
project.build(current_user, :publish => target == :publish) if project.repeat_build?
rescue StandardError => exc
project.output = project.dump_output_logger!
# If the command failed, raise a red flag
msg = \
Expand All @@ -112,11 +112,15 @@ def perform(project, update: false, target: :preview, current_user: nil)
project.file_unlock!
project.save!

project.build(current_user) if project.repeat_build?
project.build(current_user, :publish => target == :publish) if project.repeat_build?

raise
end

def unique_lock?
Autotune.lock?(unique_lock_key)
end

private

def get_full_url(url)
Expand Down
67 changes: 29 additions & 38 deletions app/models/autotune/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,45 +105,38 @@ def live?
# and build the new project.
# It publishes updates on projects already published.
# @raise The original exception when the update fails
# @see build
# @see build_and_publish
def update_snapshot(current_user)
# @see update_snapshot
def build(current_user, update: false, publish: false, repeat_until: nil, wait_until: nil)
self.status = 'building'
unless bespoke? || blueprint_version == blueprint.version

# If this is a blueprint-based project and an update is requested, update
# the version and config data from the blueprint
if update && !bespoke? && blueprint_version != blueprint.version
self.blueprint_version = blueprint.version
self.blueprint_config = blueprint.config
end

dep = deployer(publishable? ? 'preview' : 'publish', :user => current_user)
dep.prep_target
dep.after_prep_target

save!

ProjectJob.new(self, :update => true, :current_user => current_user).enqueue
rescue
update!(:status => 'broken')
raise
end

# Updates blueprint version and builds the project.
# Queues jobs to sync latest verison of blueprint, update it on the project
# and build the new project.
# It publishes updates on projects already published.
# @raise The original exception when the update fails
# @see build_and_publish
# @see update_snapshot
def build(current_user)
self.status = 'building'
target = publishable? ? 'preview' : 'publish'
target = publishable? && !publish ? 'preview' : 'publish'

dep = deployer(target, :user => current_user)
dep.prep_target
dep.after_prep_target

job = ProjectJob.new(
self, :update => update, :target => target, :current_user => current_user
)
return if job.unique_lock?

save!

ProjectJob.new(self, :target => target, :current_user => current_user).enqueue
repeat_build!(Time.zone.at(repeat_until.to_i)) if repeat_until.present?

if wait_until
job.enqueue(:wait_until => wait_until)
else
job.enqueue
end
rescue
update!(:status => 'broken')
raise
Expand All @@ -154,21 +147,19 @@ def build(current_user)
# Queues jobs to sync latest verison of blueprint, update it on the project
# and build the new project.
# @see build
# @see update_snapshot
# @raise The original exception when the update fails
def build_and_publish(current_user)
self.status = 'building'

dep = deployer('publish', :user => current_user)
dep.prep_target
dep.after_prep_target

save!
build(current_user, :publish => true)
end

ProjectJob.new(self, :target => 'publish', :current_user => current_user).enqueue
rescue
update!(:status => 'broken')
raise
# Updates blueprint version and builds the project.
# Queues jobs to sync latest verison of blueprint, update it on the project
# and build the new project.
# It publishes updates on projects already published.
# @raise The original exception when the update fails
# @see build
def update_snapshot(current_user)
build(current_user, update: true)
end

# Gets the URL for previewing the project.
Expand Down

0 comments on commit e6574f1

Please sign in to comment.