#376: updated logic, UI

This commit is contained in:
Vokhmin Alexey V 2014-04-17 23:08:22 +04:00
parent b37ada2ecd
commit aea5c94219
4 changed files with 50 additions and 5 deletions

View File

@ -57,11 +57,32 @@ ActiveAdmin.register NodeInstruction do
end
member_action :force, method: :patch do
if NodeInstruction::STATUSES.include?(params[:state])
resource.send(params[:state])
flash[:info] = 'Action added to queue successfully'
sidebar 'Actions', only: :index do
locked = NodeInstruction.all_locked?
span(class: "status_tag #{locked ? 'red' : 'green'}") do
if locked
link_to 'Unlock instructions', unlock_all_admin_node_instructions_path, method: :post
else
link_to 'Lock instructions', lock_all_admin_node_instructions_path, method: :post
end
end
end
collection_action :lock_all, method: :post do
NodeInstruction.lock_all
flash[:info] = 'Locked successfully'
redirect_to admin_node_instructions_path
end
collection_action :unlock_all, method: :post do
NodeInstruction.unlock_all
flash[:info] = 'Unlocked successfully'
redirect_to admin_node_instructions_path
end
member_action :force, method: :patch do
resource.send(params[:state])
flash[:info] = 'Updated successfully'
redirect_to admin_node_instruction_path(resource)
end

View File

@ -2,6 +2,7 @@ class RestartNodesJob
@queue = :low
def self.perform
return if NodeInstruction.all_locked?
available_nodes = RpmBuildNode.all.map{ |n| n.user_id }.compact.uniq
NodeInstruction.where(status: NodeInstruction::READY).
where('user_id NOT IN (?)', available_nodes).find_each(&:restart)

View File

@ -6,6 +6,8 @@ class NodeInstruction < ActiveRecord::Base
FAILED = 'failed'
]
LOCK_KEY = 'NodeInstruction::lock-key'
belongs_to :user
attr_encrypted :instruction, key: APP_CONFIG['keys']['node_instruction_secret_key']
@ -21,7 +23,9 @@ class NodeInstruction < ActiveRecord::Base
state_machine :status, initial: :ready do
after_transition on: :restart, do: :perform_restart
after_transition(on: :restart) do |instruction, transition|
instruction.perform_restart
end
event :ready do
transition %i(ready restarting disabled failed) => :ready
@ -41,6 +45,8 @@ class NodeInstruction < ActiveRecord::Base
end
def perform_restart
restart_failed if NodeInstruction.all_locked?
success = false
output = ''
instruction.lines.each do |command|
@ -62,4 +68,16 @@ class NodeInstruction < ActiveRecord::Base
end
later :perform_restart, queue: :low
def self.all_locked?
Redis.current.get(LOCK_KEY).present?
end
def self.lock_all
Redis.current.set(LOCK_KEY, 1)
end
def self.unlock_all
Redis.current.del(LOCK_KEY)
end
end

View File

@ -0,0 +1,5 @@
require 'spec_helper'
describe Admin::NodeInstructionsController do
it_should_behave_like 'an admin controller'
end