class CapistranoSentinel::RequestHooks

class used to handle the rake worker and sets all the hooks before and after running the worker

Constants

ENV_KEY_JOB_ID
PUBLISHER_PREFIX
SUBSCRIPTION_PREFIX

Attributes

job_id[RW]
task[RW]

Public Class Methods

job_id() click to toggle source
# File lib/capistrano_sentinel/classes/request_hooks.rb, line 8
def self.job_id
  @@job_id ||= ENV.fetch(CapistranoSentinel::RequestHooks::ENV_KEY_JOB_ID, nil) || SecureRandom.uuid
end
new(task = nil) click to toggle source
# File lib/capistrano_sentinel/classes/request_hooks.rb, line 33
def initialize(task = nil)
  @job_id  = CapistranoSentinel::RequestHooks.job_id
  @task = task.respond_to?(:fully_qualified_name) ? task.fully_qualified_name : task
end
socket_client() click to toggle source
# File lib/capistrano_sentinel/classes/request_hooks.rb, line 12
def self.socket_client
  @@socket_client ||= CapistranoSentinel::WebsocketClient.new(
  actor:            nil,
  channel:          "#{CapistranoSentinel::RequestHooks::SUBSCRIPTION_PREFIX}#{job_id}",
  auto_pong:        ENV.fetch('WS_AUTO_PONG', nil),
  read_buffer_size: ENV.fetch('WS_READ_BUFFER_SIZE', nil),
  reconnect:        ENV.fetch("WS_RECONNECT", nil),
  retry_time:       ENV.fetch("WS_RETRY_TIME", nil),
  secure:           ENV.fetch("WS_SECURE", nil),
  host:             ENV.fetch("WS_HOST", nil),
  port:             ENV.fetch("WS_PORT", nil),
  path:             ENV.fetch("WS_PATH", nil)
  )
end

Public Instance Methods

automatic_hooks(&block) click to toggle source
# File lib/capistrano_sentinel/classes/request_hooks.rb, line 38
def automatic_hooks(&block)
  if job_id.present? && @task.present?
    subscribed_already = defined?(@@socket_client)
    actor_start_working(action: 'invoke', subscribed: subscribed_already)
    if CapistranoSentinel.config.wait_execution
      actor.wait_execution_of_task until actor.task_approved
    elsif subscribed_already.blank?
      actor.wait_execution_of_task until actor.successfull_subscription
    end
    actor_execute_block(&block)
  else
    block.call
  end
end
print_question?(question) { || ... } click to toggle source
show_bundler_progress() { || ... } click to toggle source
# File lib/capistrano_sentinel/classes/request_hooks.rb, line 62
def show_bundler_progress
  actor_start_working({action: "bundle_install"}) if @task.present? && @task.to_s.size > 2
  yield if block_given?
end

Private Instance Methods

actor() click to toggle source
# File lib/capistrano_sentinel/classes/request_hooks.rb, line 69
def actor
  @actor ||= CapistranoSentinel::RequestWorker.new
  @actor
end
actor_execute_block(&block) click to toggle source
# File lib/capistrano_sentinel/classes/request_hooks.rb, line 94
def actor_execute_block(&block)
  before_hooks if CapistranoSentinel.config.hook_stdin_and_stdout
  block.call
  after_hooks if CapistranoSentinel.config.hook_stdin_and_stdout
end
actor_start_working(additionals = {}) click to toggle source
# File lib/capistrano_sentinel/classes/request_hooks.rb, line 100
def actor_start_working(additionals = {})
  additionals = additionals.present? ? additionals : {}
  data = {job_id: job_id, task: @task }.merge(additionals)
  data = data.stringify_keys
  actor.work(data)
end
after_hooks() click to toggle source
# File lib/capistrano_sentinel/classes/request_hooks.rb, line 89
def after_hooks
  input_stream.unhook
  output_stream.unhook
end
before_hooks() click to toggle source
# File lib/capistrano_sentinel/classes/request_hooks.rb, line 82
def before_hooks
  stringio = StringIO.new
  output = output_stream.hook(stringio)
  input = input_stream.hook(actor, stringio)
  [input, output]
end
input_stream() click to toggle source
# File lib/capistrano_sentinel/classes/request_hooks.rb, line 78
def input_stream
  CapistranoSentinel::InputStream
end
output_stream() click to toggle source
# File lib/capistrano_sentinel/classes/request_hooks.rb, line 74
def output_stream
  CapistranoSentinel::OutputStream
end