Module: Flydata::Helpers
- Included in:
- ApiClient, Cli, Command::Setup, Command::Sync, Credentials
- Defined in:
- lib/flydata/helpers.rb
Constant Summary collapse
- UNIT_PREFIX =
%W(TB GB MB KB B).freeze
- @@development_mode =
nil
Class Method Summary collapse
- .as_size(s) ⇒ Object
- .development? ⇒ Boolean
- .env_mode ⇒ Object
- .env_suffix ⇒ Object
- .flydata_api_host_file ⇒ Object
- .flydata_conf_file ⇒ Object
-
.format_menu_list(menu_list) ⇒ Object
format text.
- .parse_command(cmd) ⇒ Object
-
.retry_on(exception = StandardError, try_count = 3, interval = 1.0) ⇒ Object
Retry the given block if
exception
happens. - .to_command_class(class_name) ⇒ Object
- .usage_text ⇒ Object
Class Method Details
.as_size(s) ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/flydata/helpers.rb', line 107 def as_size( s ) s = s.to_f i = UNIT_PREFIX.length - 1 while s > 512 && i > 0 s /= 1024 i -= 1 end ((s > 9 || s.modulo(1) < 0.1 ? '%d' : '%.1f') % s) + ' ' + UNIT_PREFIX[i] end |
.development? ⇒ Boolean
52 53 54 55 |
# File 'lib/flydata/helpers.rb', line 52 def development? return @@development_mode unless @@development_mode.nil? @@development_mode = !!(File.open(flydata_conf_file).first.strip.end_with?('dev')) end |
.env_mode ⇒ Object
57 58 59 |
# File 'lib/flydata/helpers.rb', line 57 def env_mode development? ? 'dev' : '' end |
.env_suffix ⇒ Object
61 62 63 |
# File 'lib/flydata/helpers.rb', line 61 def env_suffix development? ? '_dev' : '' end |
.flydata_api_host_file ⇒ Object
81 82 83 |
# File 'lib/flydata/helpers.rb', line 81 def flydata_api_host_file File.join(FLYDATA_HOME, 'flydata_api_host') end |
.flydata_conf_file ⇒ Object
85 86 87 |
# File 'lib/flydata/helpers.rb', line 85 def flydata_conf_file File.join(FLYDATA_HOME, 'flydata.conf') end |
.format_menu_list(menu_list) ⇒ Object
format text
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/flydata/helpers.rb', line 66 def () max_length_list = .inject(Array.new(.first.size, 0)) do |ml, | 0.upto(.size - 1) do |i| ml[i] = [i].length if ml[i] < [i].length end ml end = .collect do || 0.upto(.size - 1).inject("") do |str, i| str = "#{str}#{[i].ljust(max_length_list[i] + 1, ' ')}" end end end |
.parse_command(cmd) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/flydata/helpers.rb', line 5 def parse_command(cmd) klass = Flydata::Command::Base method = cmd if cmd.include?(':') class_name, method = cmd.split(':') klass = to_command_class(class_name) end [klass, method] end |
.retry_on(exception = StandardError, try_count = 3, interval = 1.0) ⇒ Object
Retry the given block if exception
happens
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/flydata/helpers.rb', line 90 def retry_on(exception = StandardError, try_count = 3, interval = 1.0) count = 0 begin count += 1 yield rescue exception if count < try_count sleep interval interval *= 2 retry else raise end end end |
.to_command_class(class_name) ⇒ Object
48 49 50 |
# File 'lib/flydata/helpers.rb', line 48 def to_command_class(class_name) eval("Flydata::Command::#{class_name.camelcase}") end |
.usage_text ⇒ Object
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 |
# File 'lib/flydata/helpers.rb', line 15 def usage_text text = "" text += <<-EOM Fix the issue and try again. If the problem continues, please contact [email protected] EOM flydata_log = File.join(FLYDATA_HOME, 'flydata.log') if File.exists?(flydata_log) text += <<-EOM Also check the Agent log. Log path: #{flydata_log} EOM end text += "" text += '-' * 64 text += "\n" text += <<-EOM Usage: flydata COMMAND start # start flydata process stop # stop flydata process restart # restart flydata process status # check flydata process conf # show configuration sync [tables] # initial sync (only for mysql database) sync:generate_table_ddl [tables] # Generate CREATE TABLE script for Redshift sync:reset [tables] # reset sync EOM text += "\n" text += '-' * 64 text += "\n" text end |