Class: CapistranoSentinel::RequestWorker

Inherits:
Object
  • Object
show all
Includes:
ApplicationHelper
Defined in:
lib/capistrano_sentinel/classes/request_worker.rb

Overview

class that handles the rake task and waits for approval from the celluloid worker

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ApplicationHelper

get_question_details, message_from_bundler?, message_is_about_a_task?, message_is_for_stdout?, msg_for_stdin?, parse_json

Methods included from Logging

error_filtered?, execute_with_rescue, find_worker_log, format_error, log_error, log_output_error, log_to_file, logger, logging_enabled?, print_to_log_file, rescue_error, setup_filename_logger, setup_logger_formatter, show_warning

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action


8
9
10
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 8

def action
  @action
end

#clientObject (readonly)

Returns the value of attribute client


8
9
10
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 8

def client
  @client
end

#executorObject (readonly)

Returns the value of attribute executor


8
9
10
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 8

def executor
  @executor
end

#job_idObject (readonly)

Returns the value of attribute job_id


8
9
10
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 8

def job_id
  @job_id
end

#stdin_resultObject (readonly)

Returns the value of attribute stdin_result


8
9
10
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 8

def stdin_result
  @stdin_result
end

#successfull_subscriptionObject (readonly)

Returns the value of attribute successfull_subscription


8
9
10
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 8

def successfull_subscription
  @successfull_subscription
end

#taskObject (readonly)

Returns the value of attribute task


8
9
10
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 8

def task
  @task
end

#task_approvedObject (readonly)

Returns the value of attribute task_approved


8
9
10
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 8

def task_approved
  @task_approved
end

Instance Method Details

#default_settingsObject


35
36
37
38
39
40
41
42
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 35

def default_settings
  @stdin_result = nil
  @job_id = @options['job_id']
  @task_approved = false
  @action = @options['action'].present? ? @options['action'] : 'invoke'
  @task = @options['task']
  @successfull_subscription = false
end

#on_close(message) ⇒ Object


114
115
116
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 114

def on_close(message)
  log_to_file("RakeWorker #{@job_id} websocket connection closed: #{message.inspect}")
end

#on_error(message) ⇒ Object


61
62
63
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 61

def on_error(message)
  log_to_file("RakeWorker #{@job_id} websocket connection error: #{message.inspect}")
end

#on_message(message) ⇒ Object


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 65

def on_message(message)
  return if message.blank? || !message.is_a?(Hash)
  message = message.stringify_keys
  log_to_file("RakeWorker #{@job_id} received after on message: #{message.inspect}")
  if message['client_action'] == 'successful_subscription'
    publish_subscription_successfull(message)
  elsif message_is_about_a_task?(message)
    task_approval(message)
  elsif msg_for_stdin?(message)
    stdin_approval(message)
  else
    show_warning "unknown message: #{message.inspect}"
  end
end

#publish_subscription_successfull(message) ⇒ Object


81
82
83
84
85
86
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 81

def publish_subscription_successfull(message)
  return unless message['client_action'] == 'successful_subscription'
  log_to_file("Rake worker #{@job_id} received after publish_subscription_successfull: #{message}")
  publish_to_worker(task_data)
  @successfull_subscription = true
end

#publish_to_worker(data) ⇒ Object


56
57
58
59
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 56

def publish_to_worker(data)
  log_to_file("RakeWorker #{@job_id} tries to publish #{data.inspect}")
  socket_client.publish("#{CapistranoSentinel::RequestHooks::PUBLISHER_PREFIX}#{@job_id}", data)
end

#socket_clientObject


31
32
33
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 31

def socket_client
  @socket_client = CapistranoSentinel::RequestHooks.socket_client
end

#stdin_approval(message) ⇒ Object


95
96
97
98
99
100
101
102
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 95

def stdin_approval(message)
  return if !CapistranoSentinel.config.wait_execution || !msg_for_stdin?(message)
  if @job_id == message['job_id']
    @stdin_result = message.fetch('result', '')
  else
    show_warning "unknown stdin_approval #{message.inspect}"
  end
end

#task_approval(message) ⇒ Object


104
105
106
107
108
109
110
111
112
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 104

def task_approval(message)
  return if !CapistranoSentinel.config.wait_execution || !message_is_about_a_task?(message)
  log_to_file("RakeWorker #{@job_id} #{task_name} task_approval : #{message.inspect}")
  if @job_id.to_s == message['job_id'].to_s && message['task'].to_s == task_name.to_s && message['approved'] == 'yes'
    @task_approved = true
  else
    show_warning "#{self.inspect} got unknown task_approval #{message} #{task_data}"
  end
end

#task_dataObject


48
49
50
51
52
53
54
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 48

def task_data
  {
    action: @action,
    task: task_name,
    job_id: @job_id
  }
end

#task_nameObject


44
45
46
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 44

def task_name
  @task.respond_to?(:name) ? @task.name : @task
end

#user_prompt_needed?(data) ⇒ Boolean

Returns:

  • (Boolean)

118
119
120
121
122
123
124
125
126
127
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 118

def user_prompt_needed?(data)
  question, default = get_question_details(data)
  log_to_file("RakeWorker #{@job_id} tries to determine question #{data.inspect} #{question.inspect} #{default.inspect}")
  return if question.blank? || @action != 'invoke'
  publish_to_worker(action: 'stdout',
  question: question,
  default: default.present? ? default.delete('()') : '',
  job_id: @job_id)
  wait_for_stdin_input if CapistranoSentinel.config.wait_execution
end

#wait_execution_of_task(name = task_name, time = 0.1) ⇒ Object


19
20
21
22
23
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 19

def wait_execution_of_task(name = task_name, time = 0.1)
  #    info "Before waiting #{name}"
  wait_for(name, time)
  #  info "After waiting #{name}"
end

#wait_for(_name, time) ⇒ Object


25
26
27
28
29
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 25

def wait_for(_name, time)
  # info "waiting for #{time} seconds on #{name}"
  sleep time
  # info "done waiting on #{name} "
end

#wait_for_stdin_inputObject


88
89
90
91
92
93
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 88

def wait_for_stdin_input
  wait_execution_of_task until @stdin_result.present?
  output = @stdin_result.clone
  @stdin_result = nil
  output
end

#work(options = {}) ⇒ Object


12
13
14
15
16
17
# File 'lib/capistrano_sentinel/classes/request_worker.rb', line 12

def work(options = {})
  @options = options.stringify_keys
  default_settings
  socket_client.actor = self
  publish_to_worker(task_data) if options['subscribed'].present?
end