Class: ProcessBot::Process::Handlers::Sidekiq

Inherits:
Object
  • Object
show all
Defined in:
lib/process_bot/process/handlers/sidekiq.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(process) ⇒ Sidekiq

Returns a new instance of Sidekiq.



4
5
6
7
# File 'lib/process_bot/process/handlers/sidekiq.rb', line 4

def initialize(process)
  @process = process
  @options = process.options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'lib/process_bot/process/handlers/sidekiq.rb', line 2

def options
  @options
end

#processObject (readonly)

Returns the value of attribute process.



2
3
4
# File 'lib/process_bot/process/handlers/sidekiq.rb', line 2

def process
  @process
end

Instance Method Details

#current_pidObject



9
10
11
# File 'lib/process_bot/process/handlers/sidekiq.rb', line 9

def current_pid
  process.current_pid
end

#daemonizeObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/process_bot/process/handlers/sidekiq.rb', line 13

def daemonize
  logger.logs "Daemonize!"

  pid = Process.fork do
    Process.daemon
    yield
  end

  Process.detach(pid) if pid
end

#false_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/process_bot/process/handlers/sidekiq.rb', line 24

def false_value?(value)
  !value || value == "false"
end

#fetch(*args, **opts) ⇒ Object



28
29
30
# File 'lib/process_bot/process/handlers/sidekiq.rb', line 28

def fetch(*args, **opts)
  options.fetch(*args, **opts)
end

#graceful(**args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/process_bot/process/handlers/sidekiq.rb', line 70

def graceful(**args)
  wait_for_gracefully_stopped = args.fetch(:wait_for_gracefully_stopped, true)
  process.set_stopped

  unless current_pid
    warn "Sidekiq not running with a PID"
    return
  end

  Process.kill("TSTP", current_pid)

  if false_value?(wait_for_gracefully_stopped)
    logger.logs "Dont wait for gracefully stopped - doing that in fork..."

    daemonize do
      wait_for_no_jobs_and_stop_sidekiq
      exit
    end
  else
    logger.logs "Wait for gracefully stopped..."
    wait_for_no_jobs_and_stop_sidekiq
  end
end

#loggerObject



32
33
34
# File 'lib/process_bot/process/handlers/sidekiq.rb', line 32

def logger
  @logger ||= ProcessBot::Logger.new(options: options)
end

#set(*args, **opts) ⇒ Object



42
43
44
# File 'lib/process_bot/process/handlers/sidekiq.rb', line 42

def set(*args, **opts)
  options.set(*args, **opts)
end

#set_option(key, value) ⇒ Object



36
37
38
39
40
# File 'lib/process_bot/process/handlers/sidekiq.rb', line 36

def set_option(key, value)
  raise "Unknown option for Sidekiq handler: #{key}" unless options.key?(key)

  set(key, value)
end

#start_commandObject

rubocop:disable Metrics/AbcSize



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/process_bot/process/handlers/sidekiq.rb', line 46

def start_command # rubocop:disable Metrics/AbcSize
  args = []

  options.options.each do |key, value|
    next unless (match = key.to_s.match(/\Asidekiq_(.+)\Z/))

    sidekiq_key = match[1]

    if sidekiq_key == "queue"
      value.split(",").each do |queue|
        args.push "--queue #{queue}"
      end
    else
      args.push "--#{sidekiq_key} #{value}"
    end
  end

  command = "bash -c 'cd #{options.fetch(:release_path)} && exec "
  command << "#{options.fetch(:bundle_prefix)} " if options.present?(:bundle_prefix)
  command << "bundle exec sidekiq #{args.compact.join(' ')}"
  command << "'"
  command
end

#stop(**_args) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/process_bot/process/handlers/sidekiq.rb', line 94

def stop(**_args)
  process.set_stopped

  unless current_pid
    warn "#{handler_name} not running with a PID"
    return
  end

  Process.kill("TERM", current_pid)
end

#wait_for_no_jobsObject

rubocop:disable Metrics/AbcSize



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/process_bot/process/handlers/sidekiq.rb', line 105

def wait_for_no_jobs # rubocop:disable Metrics/AbcSize
  loop do
    found_process = false

    Knj::Unix_proc.list("grep" => current_pid) do |process|
      process_command = process.data.fetch("cmd")
      process_pid = process.data.fetch("pid").to_i
      next unless process_pid == current_pid

      found_process = true
      sidekiq_regex = /\Asidekiq (\d+).(\d+).(\d+) (#{options.possible_process_titles_joined_regex}) \[(\d+) of (\d+)(\]|) (.+?)(\]|)\Z/
      match = process_command.match(sidekiq_regex)
      raise "Couldnt match Sidekiq command: #{process_command} with Sidekiq regex: #{sidekiq_regex}" unless match

      running_jobs = match[5].to_i

      logger.logs "running_jobs: #{running_jobs}"

      return if running_jobs.zero? # rubocop:disable Lint/NonLocalExitFromIterator
    end

    raise "Couldn't find running process with PID #{current_pid}" unless found_process

    sleep 1
  end
end

#wait_for_no_jobs_and_stop_sidekiqObject



132
133
134
135
136
# File 'lib/process_bot/process/handlers/sidekiq.rb', line 132

def wait_for_no_jobs_and_stop_sidekiq
  logger.logs "Wait for no jobs and Stop sidekiq"
  wait_for_no_jobs
  stop
end