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

Constructor Details

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

Class Method Details

.slop_startObject



4
5
6
7
8
# File 'lib/flydata/command/sender.rb', line 4

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



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 55

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

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

  retry_count = 12
  1.upto(retry_count) do |i|
    return true if client_buffer_empty?
    say("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(optiosn = {}) ⇒ Object



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

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

#process_exist?Boolean

Returns:

  • (Boolean)


95
96
97
98
# File 'lib/flydata/command/sender.rb', line 95

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

#restart(options = {}) ⇒ Object



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

def restart(options = {})
  if process_exist?
    say('Restarting sender process.') unless options[:quiet]
    if Kernel.system("kill -HUP `cat #{FLYDATA_HOME}/flydata.pid`")
      say('Done.') unless options[:quiet]
      return true
    else
      raise 'Something has gone wrong..'
    end
  else
    say("Process doesn't exist.") unless options[:quiet]
    start(options)
  end
end

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

For backward compatibility. Use only as options going forward



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

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?
    say("Process is still running. Please stop process first.") unless options[:quiet]
    return
  end

  wait_until_server_ready(options)

  # Start sender(fluentd) process
  say('Starting sender process.') unless options[:quiet]
  Dir.chdir(FLYDATA_HOME){
    Kernel.system("bash #{File.dirname(__FILE__)}/../../../bin/serverinfo", :out => ["#{FLYDATA_HOME}/flydata.log",'a'], :err => ["#{FLYDATA_HOME}/flydata.log",'a'])
    daemon_option = opts.no_daemon? ? "" : "-d #{FLYDATA_HOME}/flydata.pid"
    Kernel.system("fluentd #{daemon_option} -l #{FLYDATA_HOME}/flydata.log -c #{FLYDATA_HOME}/flydata.conf -p #{File.dirname(__FILE__)}/../fluent-plugins")
  }
  Kernel.sleep 5

  wait_until_client_ready(options)
  #wait_until_logs_uploaded
  if options[:show_final_message] && !options[:quiet]
    data_port = flydata.data_port.get
    say("Go to your Dashboard! #{flydata.flydata_api_host}/data_ports/#{data_port['id']}")
    say <<EOF
Please Note: Records and Total Size are updated every 10-20 minutes.
EOF
  end
end

#statusObject



88
89
90
91
92
93
94
# File 'lib/flydata/command/sender.rb', line 88

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

#stop(options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/flydata/command/sender.rb', line 42

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

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