Class: Adminix::Service
- Inherits:
-
Object
- Object
- Adminix::Service
- Includes:
- Singleton
- Defined in:
- lib/adminix/service.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#new_logs ⇒ Object
Returns the value of attribute new_logs.
-
#process_id ⇒ Object
readonly
Returns the value of attribute process_id.
-
#syncing_logs ⇒ Object
readonly
Returns the value of attribute syncing_logs.
-
#watching_logs ⇒ Object
readonly
Returns the value of attribute watching_logs.
Instance Method Summary collapse
- #count_logs_lines ⇒ Object
- #fetch_options ⇒ Object
-
#initialize ⇒ Service
constructor
A new instance of Service.
- #options_to_envs ⇒ Object
- #options_to_envs_inline ⇒ Object
- #restart! ⇒ Object
- #stop! ⇒ Object
- #sync(ws_client, data) ⇒ Object
- #sync_logs(ws_client) ⇒ Object
- #to_cable ⇒ Object
Constructor Details
#initialize ⇒ Service
Returns a new instance of Service.
11 12 13 14 15 16 |
# File 'lib/adminix/service.rb', line 11 def initialize @id = config.service_id @watching_logs = [] @new_logs = false @syncing_logs = false end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
9 10 11 |
# File 'lib/adminix/service.rb', line 9 def id @id end |
#new_logs ⇒ Object
Returns the value of attribute new_logs.
8 9 10 |
# File 'lib/adminix/service.rb', line 8 def new_logs @new_logs end |
#process_id ⇒ Object (readonly)
Returns the value of attribute process_id.
9 10 11 |
# File 'lib/adminix/service.rb', line 9 def process_id @process_id end |
#syncing_logs ⇒ Object (readonly)
Returns the value of attribute syncing_logs.
9 10 11 |
# File 'lib/adminix/service.rb', line 9 def syncing_logs @syncing_logs end |
#watching_logs ⇒ Object (readonly)
Returns the value of attribute watching_logs.
9 10 11 |
# File 'lib/adminix/service.rb', line 9 def watching_logs @watching_logs end |
Instance Method Details
#count_logs_lines ⇒ Object
103 104 105 106 107 |
# File 'lib/adminix/service.rb', line 103 def count_logs_lines @watching_logs = config.watch_log_files.map do |file_path| { lines: file_lines_number(file_path), path: file_path } end end |
#fetch_options ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/adminix/service.rb', line 90 def uri = URI.parse("#{config.host}/v1/services/#{id}/options") request = Net::HTTP::Get.new(uri) request["Authorization"] = "Bearer #{config.secret_key}" opts = { use_ssl: uri.scheme == 'https' } response = Net::HTTP.start(uri.hostname, uri.port, opts) do |http| http.request(request) end JSON.parse(response.body) end |
#options_to_envs ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/adminix/service.rb', line 68 def data = data['result'].each do |o| puts "export #{o['key'].upcase}=\"#{o['value']}\"" end puts "# Run this command to configure your shell:\n" + "# eval $(adminix env)" end |
#options_to_envs_inline ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/adminix/service.rb', line 79 def data = str = 'export' data['result'].each do |o| str << " #{o['key'].upcase}=\"#{o['value']}\"" end str end |
#restart! ⇒ Object
109 110 111 |
# File 'lib/adminix/service.rb', line 109 def restart! stop! end |
#stop! ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/adminix/service.rb', line 113 def stop! system.log "Stopping process" case config.mode when 'classic' system.eval("#{config.scripts[:process_stop]} && #{config.scripts[:process_start]}") when 'docker' Process.kill('INT', 1) rescue nil end end |
#sync(ws_client, data) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/adminix/service.rb', line 31 def sync(ws_client, data) @process_id = data['process_id'] config.commands = data['commands'] || [] commands_queue = data['commands_queue'] || [] commands_queue.each do |q| if q['status'] != 'processed' res = execute_command(q['command_key'], q['process_id'], q['args'] || {}) ws_client.perform(:task_completed, res) if ws_client.subscribed? end end end |
#sync_logs(ws_client) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/adminix/service.rb', line 44 def sync_logs(ws_client) return unless new_logs @syncing_logs = true @watching_logs.each do |l| initial_length = l[:lines] current_length = file_lines_number(l[:path]) new_lines = current_length > initial_length ? current_length - initial_length : 100 lines = `tail -#{new_lines} #{l[:path]}`.split("\n") rescue [] payload = lines.map { |line| { created_at: Time.now.to_s, level: 'NOTICE', message: line } } begin puts "Syncing new logs" ws_client.perform(:add_logs, { logs: payload }) if ws_client.subscribed? rescue end end count_logs_lines @new_logs = false @syncing_logs = false end |
#to_cable ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/adminix/service.rb', line 18 def to_cable system.check_system_load { id: id, process_id: process_id, system: { processor_load: system.processor_load, memory_load: system.memory_load } } end |