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, #flydata, #initialize, #newline, #register_crontab, #retrieve_data_entries, #separator

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



6
7
8
9
10
# File 'lib/flydata/command/sender.rb', line 6

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/flydata/command/sender.rb', line 62

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



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

def kill_all(options = {})
  if Kernel.system("ps ax | grep 'flydata' | grep -v grep | 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)


103
104
105
106
# File 'lib/flydata/command/sender.rb', line 103

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/flydata/command/sender.rb', line 81

def restart(options = {})
  if process_exist?
    log_info_stdout('Restarting sender process.') unless options[:quiet]
    begin
      stop(options)
    rescue
      log_out_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



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

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

  Flydata::Command::Sync.new.try_mysql_sync

  # Start sender(fluentd) process
  log_info_stdout("Starting sender process.") unless options[:quiet]
  raw_start(options)

  wait_until_client_ready(options)
  #wait_until_logs_uploaded
  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



96
97
98
99
100
101
102
# File 'lib/flydata/command/sender.rb', line 96

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

#stop(options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/flydata/command/sender.rb', line 49

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