class CommandProposal::Services::CommandInterpreter
Public Class Methods
command(iteration, command, user, params={})
click to toggle source
# File lib/command_proposal/services/command_interpreter.rb, line 9 def self.command(iteration, command, user, params={}) new(iteration, command, user, params).command end
new(iteration, command, user, params={})
click to toggle source
# File lib/command_proposal/services/command_interpreter.rb, line 13 def initialize(iteration, command, user, params={}) @iteration = iteration @task = iteration.task @command = command.to_s.to_sym @user = user @params = params command_user(@user) if @user.present? end
Public Instance Methods
check_can_approve?()
click to toggle source
# File lib/command_proposal/services/command_interpreter.rb, line 103 def check_can_approve? return true if can_approve?(@iteration) error!("You cannot approve your own command.") end
check_can_command?()
click to toggle source
# File lib/command_proposal/services/command_interpreter.rb, line 97 def check_can_command? return true if can_command? error!("Sorry, you do not have permission to do this.") end
command()
click to toggle source
# File lib/command_proposal/services/command_interpreter.rb, line 22 def command case @command when :request then command_request when :approve then command_approve when :run then command_run when :cancel then command_cancel when :close then command_close end @iteration end
command_approve()
click to toggle source
# File lib/command_proposal/services/command_interpreter.rb, line 54 def command_approve error!("Command is not ready for approval.") unless @iteration.pending? check_can_command? && check_can_approve? @iteration.update(status: :approved, approver: @user, approved_at: Time.current) end
command_cancel()
click to toggle source
# File lib/command_proposal/services/command_interpreter.rb, line 75 def command_cancel check_can_command? return if @iteration.complete? @iteration.update(status: :cancelling) return if ::CommandProposal.sessions.key?("task:#{@task.id}") ::CommandProposal::Services::ShutDown.terminate(@iteration) end
command_close()
click to toggle source
# File lib/command_proposal/services/command_interpreter.rb, line 85 def command_close check_can_command? return unless @iteration.task.console? if ::CommandProposal.sessions.key?("task:#{@task.id}") @task.first_iteration.update(status: :success, completed_at: Time.current) else @task.first_iteration.update(status: :terminated, completed_at: Time.current) end ::CommandProposal.sessions.delete("task:#{@task.id}") end
command_request()
click to toggle source
# File lib/command_proposal/services/command_interpreter.rb, line 34 def command_request check_can_command? if @iteration.complete? && (@task.task? || @task.function?) previous_iteration = @iteration # Creates a new iteration with the same code so we don't lose results @task.user = @user # Sets the task user to assign as the requester @task.update(code: @iteration.code) @iteration = @task.current_iteration if @task.function? && previous_iteration.approved_at? @params.merge!(previous_iteration.attributes.slice("approved_at", "approver_id")) @params.merge!(status: :approved) return # Don't trigger the callback end end proposal = ::CommandProposal::Service::ProposalPresenter.new(@iteration) ::CommandProposal.configuration.proposal_callback&.call(proposal) end
command_run()
click to toggle source
# File lib/command_proposal/services/command_interpreter.rb, line 61 def command_run check_can_command? # Rollback the create/update if anything fails ActiveRecord::Base.transaction do command_request if @task.function? && @iteration.approved_at? && @iteration.complete? @iteration.update(@params) error!("Cannot run without approval.") unless has_approval?(@task) end ::CommandProposal::CommandRunnerJob.perform_later(@iteration.id) end
error!(msg)
click to toggle source
# File lib/command_proposal/services/command_interpreter.rb, line 109 def error!(msg) raise ::CommandProposal::Services::CommandInterpreter::Error.new(msg) end