Skip to content

Commit

Permalink
Show progress of current orchestration task for repos
Browse files Browse the repository at this point in the history
  • Loading branch information
iNecas committed Jan 15, 2014
1 parent a2a2475 commit 3a73654
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 14 deletions.
14 changes: 13 additions & 1 deletion app/controllers/api/v2/dyntasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,19 @@ def index
# https://github.com/angular/angular.js/commit/2a2123441c2b749b8f316a24c3ca3f77a9132a01
uuids = uuids.map { |uuid| uuid.split(',') }.flatten

render :json => uuids.map { |uuid| { :uuid => uuid, :progress => rand } }
execution_plans = uuids.map do |uuid|
begin
Orchestrate.world.persistence.load_execution_plan(uuid)
rescue KeyError
raise HttpErrors::BadRequest, _("Task #{uuid} was not found")
end
end

json = execution_plans.map do |execution_plan|
{ :uuid => execution_plan.id,
:progress => execution_plan.progress }
end
render :json => json
end

end
2 changes: 1 addition & 1 deletion app/lib/katello/glue/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def destroy_event
end

def self.trigger(event_class, *args)
uuid, promise = ::Orchestrate.world.trigger(event_class, *args)
uuid, promise = ::Orchestrate.trigger(event_class, *args)
::Logging.logger['glue'].debug("Started plan with #{uuid}")
promise.wait
::Logging.logger['glue'].debug("Finished plan with #{uuid}")
Expand Down
9 changes: 9 additions & 0 deletions app/models/dynflow_execution_plan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class DynflowExecutionPlan < ActiveRecord::Base

self.table_name = 'dynflow_execution_plans'
self.primary_key = :uuid

def self.create_or_update(*args)
raise "Read only model - it's managed by Dynflow persistence layer"
end
end
42 changes: 42 additions & 0 deletions app/models/dynflow_lock.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class DynflowLock < ActiveRecord::Base
attr_accessible :resource_id, :resource_type, :uuid

belongs_to :dynflow_execution_plan,
class_name: 'DynflowExecutionPlan',
foreign_key: :uuid
belongs_to :dynflow_task, foreign_key: :uuid
belongs_to :resource, polymorphic: true

scope :active, -> do
includes(:dynflow_execution_plan)
.where('dynflow_execution_plans.state != ?', :stopped)
end

scope :inactive, -> do
includes(:dynflow_execution_plan)
.where('dynflow_execution_plans.state = ?', :stopped)
end

scope :for_model, -> model do
where(resource_id: model.id, resource_type: model.class.name)
end

def self.active_lock(model)
active.for_model(model).first
end

def self.lock!(uuid, model)
if model.new_record?
raise "Model #{model} has to be saved before locking it"
end
if lock = DynflowLock.active_lock(model)
raise "Model #{model} has already been locked by task #{lock.uuid}"
end

DynflowLock.create!(:uuid => uuid) do |new_lock|
new_lock.resource = model
end
end


end
12 changes: 12 additions & 0 deletions app/models/dynflow_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class DynflowTask < ActiveRecord::Base

self.primary_key = :uuid

attr_accessible :action, :organization_id, :user_id, :uuid

belongs_to :dynflow_execution_plan,
class_name: 'DynflowExecutionPlan',
foreign_key: :uuid
has_many :dynflow_locks, foreign_key: :uuid

end
4 changes: 4 additions & 0 deletions app/views/katello/api/v2/repositories/show.json.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ attributes :major, :minor
attributes :gpg_key_id
attributes :content_id, :content_view_version_id, :library_instance_id

node :current_task do |repo|
DynflowLock.active_lock(repo).try(:uuid)
end

node :content_counts do |repo|
if repo.respond_to?(:pulp_repo_facts)
repo.pulp_repo_facts['content_unit_counts']
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20131025092335_create_dynflow_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateDynflowTasks < ActiveRecord::Migration
def change
create_table :dynflow_tasks, :id => false do |t|
t.string :uuid, index: true
t.string :action, index: true
t.integer :user_id, index: true
t.integer :organization_id, index: true

t.timestamps
end
end
end
9 changes: 9 additions & 0 deletions db/migrate/20131025093810_create_dynflow_locks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateDynflowLocks < ActiveRecord::Migration
def change
create_table :dynflow_locks do |t|
t.string :uuid, index: true
t.string :resource_type, index: true
t.integer :resource_id, index: true
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@


<div class="details fl">
<taskprogress uuid="1"></taskprogress>

<taskprogress uuid="2"></taskprogress>
<div class="divider"></div>
<taskprogress uuid="{{ repository.current_task }}"></taskprogress>

<section>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ angular.module('Bastion.widgets')

function updateProgress() {
var uuids = []
angular.forEach(callbacks, function(callback, uuid) { uuids.push(uuid); })
angular.forEach(callbacks, function(callback, uuid) { uuids.push(uuid); })
if (uuids.length == 0) {
return;
}
Expand Down Expand Up @@ -54,12 +54,16 @@ angular.module('Bastion.widgets')
scope: {
uuid: '@',
},
link: function (scope, element, args) {
tasksStatusProvider.register(args.uuid, function(progress) {
scope.progress = progress;
});
element.bind('$destroy', function() {
tasksStatusProvider.unregister(args.uuid);
link: function (scope, element, attrs) {
scope.$watch('uuid', function(uuid) {
if(uuid) {
tasksStatusProvider.register(uuid, function(progress) {
scope.progress = progress;
});
element.bind('$destroy', function() {
tasksStatusProvider.unregister(uuid);
});
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<section>
<section ng-show="uuid">
<h4>
{{ "Orchestration Status" | i18n }}
</h4>
Expand All @@ -8,4 +8,6 @@ <h4>
<span class="info-value">Synchronization</span>
</div>
<progress percent="progress"></progress>
<div class="divider"></div>
</section>

9 changes: 9 additions & 0 deletions lib/orchestrate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@ def self.world

@world = Dynflow::World.new(world_options)
end

def self.trigger(action, *args)
uuid, f = world.trigger(action, *args)
DynflowTask.create!(uuid: uuid, action: action.name, user_id: User.current.id) do |task|
# to set additional task meta-data
yield task if block_given?
end
return uuid, f
end
end
35 changes: 35 additions & 0 deletions lib/orchestrate/helpers/lock.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Orchestrate
module Helpers

# Helpers for remote actions
# wraps the plan/run/finalize methods to include the info about the user
# that triggered the action.
module Lock

def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods

def generate_phase(phase_module)
super.tap do |phase_class|
if phase_module == Dynflow::Action::PlanPhase
phase_class.send(:include, PlanMethods)
end
end
end

end

module PlanMethods

def lock(model)
DynflowLock.lock!(execution_plan_id, model)
end

end

end
end
end
4 changes: 4 additions & 0 deletions lib/orchestrate/katello/pulp/repository_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ def update_progress(done, pulp_task)
output.update pulp_task_progress: pulp_task, done: done
end

def run_progress_weight
10
end

end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/orchestrate/katello/repository_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ module Katello
class RepositorySync < Dynflow::Action

include Helpers::RemoteAction
include Helpers::Lock

input_format do
param :id, Integer
end

def plan(repo)
lock(repo)
plan_action(Pulp::RepositorySync, pulp_id: repo.pulp_id)
plan_self(:id => repo.id)
end
Expand Down

0 comments on commit 3a73654

Please sign in to comment.