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.
-
#process_id ⇒ Object
readonly
Returns the value of attribute process_id.
Instance Method Summary collapse
- #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(watcher, data) ⇒ Object
- #to_cable ⇒ Object
Constructor Details
#initialize ⇒ Service
Returns a new instance of Service.
10 11 12 |
# File 'lib/adminix/service.rb', line 10 def initialize @id = config.service_id end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/adminix/service.rb', line 8 def id @id end |
#process_id ⇒ Object (readonly)
Returns the value of attribute process_id.
8 9 10 |
# File 'lib/adminix/service.rb', line 8 def process_id @process_id end |
Instance Method Details
#fetch_options ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/adminix/service.rb', line 62 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
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/adminix/service.rb', line 40 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
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/adminix/service.rb', line 51 def data = str = 'export' data['result'].each do |o| str << " #{o['key'].upcase}=#{o['value']}" end str end |
#restart! ⇒ Object
75 76 77 |
# File 'lib/adminix/service.rb', line 75 def restart! stop! end |
#stop! ⇒ Object
79 80 81 82 |
# File 'lib/adminix/service.rb', line 79 def stop! system.log "Stopping process" Process.kill('INT', 1) rescue nil end |
#sync(watcher, data) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/adminix/service.rb', line 27 def sync(watcher, 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'] || {}) watcher.(:task_completed, res) end end end |
#to_cable ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/adminix/service.rb', line 14 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 |