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, #dashboard_url, #data_entry, #data_port, #flydata, #initialize, #newline, #redshift_cluster, #redshift_console_url, #register_crontab, #retrieve_data_entries, #separator, #show_purpose_name, #source

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
14
15
16
# 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'
    on 'y', 'yes', 'Skip command prompt assuming yes to all questions.  Use this for batch operation.'
    on 'force-run', 'Run forcefully, ignoring exclusive run info'
    on 'auto-create', 'Create tables on Redshift automatically'
  end
end

Instance Method Details

#agent_locked?Boolean

call the method only when no legit process is running.

Returns:

  • (Boolean)

153
154
155
# File 'lib/flydata/command/sender.rb', line 153

def agent_locked?
  File.exists?(FLYDATA_LOCK)
end

#flush_client_buffer(options = {}) ⇒ Object


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/flydata/command/sender.rb', line 98

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 'Unable to flush client buffer'
end

#kill_all(options = {}) ⇒ Object


144
145
146
147
148
149
150
151
# File 'lib/flydata/command/sender.rb', line 144

def kill_all(options = {})
  if Kernel.system("ps x | grep 'flydata' | grep -v grep | grep fluentd | awk '{print \"kill -9 \" $1}' | sh")
    log_info_stdout("Done.") unless options[:quiet]
    return true
  else
    raise 'Could not terminate FlyData process.'
  end
end

#process_exist?Boolean

Returns:

  • (Boolean)

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

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


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/flydata/command/sender.rb', line 118

def restart(options = {})
  if process_exist?
    log_info_stdout('Restarting sender process.') unless options[:quiet]
    begin
      stop(options)
    rescue => e
      log_warn_stderr "Force stopping the FlyData process. error:#{e.to_s}"
      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


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
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/flydata/command/sender.rb', line 17

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

  dp = flydata.data_port.get
  if dp['paused']
    raise "This application has been stopped. Process cannot be started."
  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)

  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_initial_sync
  options[:quiet] = true
  Flydata::Command::Sync.new.try_initial_sync(
    source_pos_ready_callback: start_fluentd,
    no_email: opts.no_email?,
    auto_create: options[:auto_create] || opts.auto_create?,
    slop_opts: opts,
  )
  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}/dashboard")
    log_info_stdout <<EOF
Please Note: Records and Total Size are updated every 10-20 minutes.
EOF
  end
end

#statusObject


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

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

#stop(options = {}) ⇒ Object


85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/flydata/command/sender.rb', line 85

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 'Failed to stop sender process.'
end

#wait_until_server_ready(options = {}) ⇒ Object


157
158
159
160
161
162
163
164
165
# File 'lib/flydata/command/sender.rb', line 157

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