Class: CapistranoSentinel::RequestHooks

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano_sentinel/classes/request_hooks.rb

Overview

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

Constant Summary collapse

ENV_KEY_JOB_ID =
'multi_cap_job_id'
SUBSCRIPTION_PREFIX =
"rake_worker_"
PUBLISHER_PREFIX =
"celluloid_worker_"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task = nil) ⇒ RequestHooks


33
34
35
36
# 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

Instance Attribute Details

#job_idObject

Returns the value of attribute job_id


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

def job_id
  @job_id
end

#taskObject

Returns the value of attribute task


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

def task
  @task
end

Class Method Details

.job_idObject


8
9
10
# 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

.socket_clientObject


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Method Details

#automatic_hooks(&block) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

53
54
55
56
57
58
59
# File 'lib/capistrano_sentinel/classes/request_hooks.rb', line 53

def print_question?(question)
  if CapistranoSentinel.config.hook_stdin_and_stdout && job_id.present?
    actor.user_prompt_needed?(question)
  else
    yield if block_given?
  end
end

#show_bundler_progressObject


62
63
64
65
# 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