Class: CommandProposal::IterationsController

Inherits:
EngineController
  • Object
show all
Includes:
ParamsHelper, PermissionsHelper
Defined in:
app/controllers/command_proposal/iterations_controller.rb

Instance Method Summary collapse

Methods included from PermissionsHelper

#approval_required?, #can_approve?, #can_command?, #cmd_config, #command_user, #current_is_author?, #has_approval?, #permitted_to_use?

Methods included from ParamsHelper

#command_paginate, #current_params, #current_path, #div, #humanized_duration, #sort_order, #toggled_param, #toggled_sort_order, #true_param?, #truthy?

Methods included from ApplicationHelper

#cmd_path, #cmd_url, #engine, #runner_path, #runner_url, #string_path

Instance Method Details

#createObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/command_proposal/iterations_controller.rb', line 22

def create
  return error!("You do not have permission to run commands.") unless can_command?

  @task = ::CommandProposal::Task.find_by!(friendly_id: params[:task_id])
  # Should rescue/catch and render json
  return error!("Can only run commands on type: :console") unless @task.console?
  return error!("Session has not been approved.") unless has_approval?(@task)

  if @task.iterations.one?
    # Track console details in first iteration
    @task.first_iteration.update(started_at: Time.current, status: :started)
  end

  @task.user = command_user # Separate from update to ensure it's set first
  @task.update(code: params[:code]) # Creates a new iteration
  @iteration = @task.current_iteration
  @iteration.update(status: :approved) # Task was already approved, and this is line-by-line

  # async, but wait for the job to finish
  ::CommandProposal::CommandRunnerJob.perform_later(@iteration.id, "task:#{@task.id}")

  max_wait_seconds = 3
  loop do
    break unless max_wait_seconds.positive?

    max_wait_seconds -= sleep 0.2

    break if @iteration.reload.complete?
  end

  render json: {
    results_endpoint: cmd_path(@iteration),
    result: @iteration.result,
    status: @iteration.status
  }
end

#showObject



12
13
14
15
16
17
18
19
20
# File 'app/controllers/command_proposal/iterations_controller.rb', line 12

def show
  @iteration = ::CommandProposal::Iteration.find(params[:id])

  render json: {
    results_endpoint: cmd_path(@iteration),
    result: @iteration.result,
    status: @iteration.status
  }
end

#updateObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/command_proposal/iterations_controller.rb', line 59

def update
  @iteration = ::CommandProposal::Iteration.find(params[:id])

  begin
    alter_command if params.dig(:command_proposal_iteration, :command).present?
  rescue ::CommandProposal::Services::CommandInterpreter::Error => e
    return redirect_to cmd_path(:error, :tasks), alert: e.message
  end

  sleep 0.2

  redirect_to cmd_path(@iteration.task)
end