Class: CommandProposal::Services::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/command_proposal/services/runner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



38
39
40
# File 'lib/command_proposal/services/runner.rb', line 38

def initialize
  @session = session
end

Instance Attribute Details

#session=(value) ⇒ Object

Sets the attribute session

Parameters:

  • value

    the value to set the attribute session to.



4
5
6
# File 'lib/command_proposal/services/runner.rb', line 4

def session=(value)
  @session = value
end

Class Method Details

.command(friendly_id, user, params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/command_proposal/services/runner.rb', line 13

def self.command(friendly_id, user, params={})
  # Hack magic because requires are not playing well with spring
  require "command_proposal/services/command_interpreter"

  params = params.to_unsafe_h if params.is_a?(ActionController::Parameters)

  iteration = ::CommandProposal::Services::CommandInterpreter.command(
    ::CommandProposal::Task.find_by!(friendly_id: friendly_id).primary_iteration,
    :run,
    user,
    { args: params }
  )

  start = Time.current
  wait_time = 5 # seconds
  loop do
    sleep 0.4

    break if iteration.reload.complete?
    break if Time.current - start > wait_time
  end

  iteration
end

.execute(friendly_id) ⇒ Object

Add expiration and things like that…



7
8
9
10
11
# File 'lib/command_proposal/services/runner.rb', line 7

def self.execute(friendly_id)
  task = ::CommandProposal::Task.find_by!(friendly_id: friendly_id)

  new.execute(task.primary_iteration)
end

Instance Method Details

#execute(iteration) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/command_proposal/services/runner.rb', line 42

def execute(iteration)
  @iteration = iteration
  prepare

  run

  complete
  proposal = ::CommandProposal::Service::ProposalPresenter.new(@iteration)
  @iteration = nil
  proposal
end

#quick_fail(iteration, msg) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/command_proposal/services/runner.rb', line 54

def quick_fail(iteration, msg)
  @iteration = iteration
  prepare

  @iteration.status = :failed
  @iteration.result = msg

  complete
  proposal = ::CommandProposal::Service::ProposalPresenter.new(@iteration)
  @iteration = nil
  proposal
end

#quick_run(friendly_id) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/command_proposal/services/runner.rb', line 67

def quick_run(friendly_id)
  task = ::CommandProposal::Task.find_by!(friendly_id: friendly_id)
  iteration = task&.primary_iteration

  raise CommandProposal::Error, ":#{friendly_id} does not have approval to run." unless iteration&.approved?

  @session.eval(iteration.code)
end