Class: Flydata::Command::Sender

Inherits:
Base
  • Object
show all
Defined in:
lib/flydata/command/sender.rb

Instance Attribute Summary

Attributes inherited from Base

#opts

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#ask_input_table_name, #ask_yes_no, #choose_one, #data_entry, #flydata, #initialize, #newline, #register_crontab, #retrieve_data_entries, #separator, #show_purpose_name

Methods included from ExclusiveRunnable

included

Methods included from Flydata::CommandLoggable

#before_logging, #log_error_stderr, #log_info_stdout, #log_warn_stderr

Constructor Details

This class inherits a constructor from Flydata::Command::Base

Class Method Details

.slop_startObject



8
9
10
11
12
13
# File 'lib/flydata/command/sender.rb', line 8

def self.slop_start
  Slop.new do
    on 'n', 'no-daemon', 'Start FlyData agent as a regular program'
    on 'e', 'no-email', 'Skip sending init-sync-start notification email'
  end
end

Instance Method Details

#agent_locked?Boolean

call the method only when no legit process is running.

Returns:

  • (Boolean)


142
143
144
# File 'lib/flydata/command/sender.rb', line 142

def agent_locked?
  File.exists?(FLYDATA_LOCK)
end

#flush_client_buffer(options = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/flydata/command/sender.rb', line 87

def flush_client_buffer(options = {})
  force_flush = options.has_key?(:force) ? options[:force] : true
  unless process_exist?
    return true if !force_flush || client_buffer_empty?
    log_info_stdout("Process doesn't exist. But, the client buffer is not empty!!") unless options[:quiet]
    raw_start(options)
  end

  log_info_stdout('Stopping input plugins and flushing the client buffer.') unless options[:quiet]
  Kernel.system("kill -USR1 `cat #{pid_file}`")

  retry_count = 12
  1.upto(retry_count) do |i|
    return true if client_buffer_empty?
    log_info_stdout("Waiting for the buffer to get empty... (#{i}/#{retry_count})") unless options[:quiet]
    Kernel.sleep 5
  end

  raise 'Something is wrong! Unable to flush client buffer'
end

#kill_all(options = {}) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/flydata/command/sender.rb', line 133

def kill_all(options = {})
  if Kernel.system("ps ax | grep 'flydata' | grep -v grep | grep fluentd | awk '{print \"kill  \" $1}' | sh")
    log_info_stdout("Done.") unless options[:quiet]
    return true
  else
    raise 'Something has gone wrong...'
  end
end

#process_exist?Boolean

Returns:

  • (Boolean)


129
130
131
132
# File 'lib/flydata/command/sender.rb', line 129

def process_exist?
  # Returns true if the process is running
  `[ -f #{pid_file} ] && pgrep -P \`cat #{pid_file}\``.to_i > 0
end

#restart(options = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/flydata/command/sender.rb', line 107

def restart(options = {})
  if process_exist?
    log_info_stdout('Restarting sender process.') unless options[:quiet]
    begin
      stop(options)
    rescue
      log_warn_stderr 'Something has gone wrong... force stopping the FlyData process...'
      kill_all(options)
    end
  else
    log_info_stdout("Process doesn't exist.") unless options[:quiet]
  end
  start(options)
  say('Process started.') unless options[:quiet]
end

#start(options_or_show_final_message = {show_final_message: true}) ⇒ Object

For backward compatibility. Use only as options going forward



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/flydata/command/sender.rb', line 14

def start(options_or_show_final_message = {show_final_message: true}) # For backward compatibility. Use only as options going forward
  if options_or_show_final_message.kind_of? Hash
    options = options_or_show_final_message
  else
    options = {show_final_message: options_or_show_final_message}
  end
  # Check if process exist
  if process_exist?
    log_info_stdout("Process is still running. Please stop process first.") unless options[:quiet]
    return
  end

  if agent_locked?
    log_info_stdout("Agent was not shut down properly.  Agent will check the status and fix itself if necessary.")
    repair_opts = Flydata::Command::Sync.slop_repair
    repair_opts.parse!(["-y"])
    sync = Flydata::Command::Sync.new(repair_opts)
    sync.send(:_repair) # call internal method to bypass command lock
    if agent_locked?
      raise "Agent was not able to recover from the previous unexpected shutdown.  Please contact [email protected] to resolve the issue."
    end
  end

  # Ends orphan_proceses if there is any
  orphan_processes.each do |pid|
    Process.kill(:TERM, pid)
  end

  wait_until_server_ready(options)

  dp = flydata.data_port.get
  AgentCompatibilityCheck.new(dp).check

  fluentd_started = false
  start_fluentd = Proc.new do
    # Start sender(fluentd) process
    log_info_stdout("Starting FlyData Agent sender process.") unless options[:quiet]
    raw_start(options)

    wait_until_client_ready(options)
    #wait_until_logs_uploaded
    fluentd_started = true
  end

  quiet_option = options[:quiet]
  # surpress messages if fluentd is started in #try_mysql_sync
  options[:quiet] = true
  Flydata::Command::Sync.new.try_mysql_sync(
    binlog_ready_callback: start_fluentd,
    no_email: opts.no_email?)
  options[:quiet] = quiet_option
  start_fluentd.call unless fluentd_started
  if options[:show_final_message] && !options[:quiet]
    data_port = flydata.data_port.get
    log_info_stdout("Go to your Dashboard! #{flydata.flydata_api_host}/data_ports/#{data_port['id']}")
    log_info_stdout <<EOF
Please Note: Records and Total Size are updated every 10-20 minutes.
EOF
  end
end

#statusObject



122
123
124
125
126
127
128
# File 'lib/flydata/command/sender.rb', line 122

def status
  if process_exist?
    log_info_stdout("Process is running.")
  else
    log_info_stdout("Process is not running.")
  end
end

#stop(options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/flydata/command/sender.rb', line 74

def stop(options = {})
  unless process_exist?
    log_info_stdout("Process doesn't exist.") unless options[:quiet]
    return true
  end

  log_info_stdout('Stopping sender process.') unless options[:quiet]
  if Kernel.system("kill `cat #{pid_file}`") and wait_until_client_stop(options)
    log_info_stdout('Done.') unless options[:quiet]
    return true
  end
  raise 'Something has gone wrong..'
end

#wait_until_server_ready(options = {}) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/flydata/command/sender.rb', line 146

def wait_until_server_ready(options = {})
  retry_count = 60 # 60 x 30(sec) = 1800(sec) = 30(min)
  1.upto(retry_count) do |i|
    return true if server_ready?
    log_info_stdout("Waiting for the server side to become active... (#{i})") unless options[:quiet]
    Kernel.sleep 30
  end
  false
end