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
# 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'
  end
end

Instance Method Details

#flush_client_buffer(options = {}) ⇒ Object



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

def flush_client_buffer(options = {})
  unless process_exist?
    return true if 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



119
120
121
122
123
124
125
126
# File 'lib/flydata/command/sender.rb', line 119

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)


115
116
117
118
# File 'lib/flydata/command/sender.rb', line 115

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/flydata/command/sender.rb', line 93

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



13
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
# File 'lib/flydata/command/sender.rb', line 13

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

  # 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)
  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



108
109
110
111
112
113
114
# File 'lib/flydata/command/sender.rb', line 108

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

#stop(options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/flydata/command/sender.rb', line 61

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