Class: Adminix::Entities::Service
- Inherits:
-
Object
- Object
- Adminix::Entities::Service
- Defined in:
- lib/adminix/entities/service.rb
Instance Attribute Summary collapse
-
#device_id ⇒ Object
readonly
Returns the value of attribute device_id.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#jobs ⇒ Object
readonly
Returns the value of attribute jobs.
-
#load_stamps ⇒ Object
readonly
Returns the value of attribute load_stamps.
-
#logs ⇒ Object
readonly
Returns the value of attribute logs.
-
#performed_actions ⇒ Object
readonly
Returns the value of attribute performed_actions.
-
#variables ⇒ Object
Returns the value of attribute variables.
Instance Method Summary collapse
- #add_logs(logs) ⇒ Object
- #add_new_jobs(list) ⇒ Object
- #add_system_load(sys_load) ⇒ Object
- #add_variable(new_var) ⇒ Object
- #allow_sending_logs ⇒ Object
- #allow_sending_system_load ⇒ Object
- #bash_variables(file = nil) ⇒ Object
- #clean_load_stamps ⇒ Object
- #clean_sent_logs ⇒ Object
- #completed_jobs ⇒ Object
- #completed_jobs_to_api ⇒ Object
- #define_device_id ⇒ Object
- #define_performed_action(key, value) ⇒ Object
- #ensure_credentials_exists! ⇒ Object
- #generate_device_id(id = nil) ⇒ Object
- #id_defined? ⇒ Boolean
-
#initialize(opts = {}) ⇒ Service
constructor
A new instance of Service.
- #load_stamps_to_api ⇒ Object
- #lock ⇒ Object
- #locked? ⇒ Boolean
- #logs_to_api ⇒ Object
- #new_jobs ⇒ Object
- #new_jobs? ⇒ Boolean
- #read_device_id ⇒ Object
- #remove_completed_jobs ⇒ Object
- #reset_performed_actions ⇒ Object
- #shift_load_stamps(number) ⇒ Object
- #shift_logs(number) ⇒ Object
-
#sync_payload ⇒ Object
TODO: real total memory and disk.
- #unlock ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Service
Returns a new instance of Service.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/adminix/entities/service.rb', line 9 def initialize(opts = {}) @id = opts[:id] @device_id_path = "#{Helpers::Command.home}/.config/adminix/device_id" @variables = [] @jobs = [] @logs = [] @load_stamps = [] @send_logs = false @locked = false @send_system_load = false @performed_actions = { watcher_started: true, watcher_updated: false, process_stopped: false, process_started: false, process_updated: false } define_device_id end |
Instance Attribute Details
#device_id ⇒ Object (readonly)
Returns the value of attribute device_id.
6 7 8 |
# File 'lib/adminix/entities/service.rb', line 6 def device_id @device_id end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/adminix/entities/service.rb', line 6 def id @id end |
#jobs ⇒ Object (readonly)
Returns the value of attribute jobs.
6 7 8 |
# File 'lib/adminix/entities/service.rb', line 6 def jobs @jobs end |
#load_stamps ⇒ Object (readonly)
Returns the value of attribute load_stamps.
6 7 8 |
# File 'lib/adminix/entities/service.rb', line 6 def load_stamps @load_stamps end |
#logs ⇒ Object (readonly)
Returns the value of attribute logs.
6 7 8 |
# File 'lib/adminix/entities/service.rb', line 6 def logs @logs end |
#performed_actions ⇒ Object (readonly)
Returns the value of attribute performed_actions.
6 7 8 |
# File 'lib/adminix/entities/service.rb', line 6 def performed_actions @performed_actions end |
#variables ⇒ Object
Returns the value of attribute variables.
7 8 9 |
# File 'lib/adminix/entities/service.rb', line 7 def variables @variables end |
Instance Method Details
#add_logs(logs) ⇒ Object
57 58 59 60 |
# File 'lib/adminix/entities/service.rb', line 57 def add_logs(logs) return if locked? @logs += logs end |
#add_new_jobs(list) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/adminix/entities/service.rb', line 48 def add_new_jobs(list) list.each do |data| @jobs << Entities::Job.new( id: data['id'], script: data['script'] ) end end |
#add_system_load(sys_load) ⇒ Object
76 77 78 |
# File 'lib/adminix/entities/service.rb', line 76 def add_system_load(sys_load) @load_stamps << sys_load end |
#add_variable(new_var) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/adminix/entities/service.rb', line 66 def add_variable(new_var) var = @variables.find { |v| v.key == new_var.key } if var index = @variables.index(var) @variables[index] = new_var else @variables << new_var end end |
#allow_sending_logs ⇒ Object
84 85 86 87 88 |
# File 'lib/adminix/entities/service.rb', line 84 def allow_sending_logs @send_logs = true return if @logs.count.zero? Adminix.logger.info("Sending #{@logs.count} new logs") end |
#allow_sending_system_load ⇒ Object
90 91 92 93 94 |
# File 'lib/adminix/entities/service.rb', line 90 def allow_sending_system_load @send_system_load = true return if @load_stamps.count.zero? Adminix.logger.info("Sending #{@load_stamps.count} new system load stamps") end |
#bash_variables(file = nil) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/adminix/entities/service.rb', line 173 def bash_variables(file = nil) output = '' if file content = (variables.map { |v| "#{v.key}=#{v.value}" }).join("\n") Helpers::Files.rm(file) Helpers::Files.write_plain_file(file, content) output = file else variables.each { |v| output << "#{v.to_bash_export}\n" } output << "# Run this command to configure your shell:\n" \ '# eval $(adminix env)' end output end |
#clean_load_stamps ⇒ Object
42 43 44 45 46 |
# File 'lib/adminix/entities/service.rb', line 42 def clean_load_stamps return unless @send_system_load @send_system_load = false @load_stamps = [] end |
#clean_sent_logs ⇒ Object
36 37 38 39 40 |
# File 'lib/adminix/entities/service.rb', line 36 def clean_sent_logs return unless @send_logs @send_logs = false @logs = [] end |
#completed_jobs ⇒ Object
149 150 151 |
# File 'lib/adminix/entities/service.rb', line 149 def completed_jobs @jobs.select(&:completed?) end |
#completed_jobs_to_api ⇒ Object
153 154 155 |
# File 'lib/adminix/entities/service.rb', line 153 def completed_jobs_to_api completed_jobs.map(&:to_api) end |
#define_device_id ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/adminix/entities/service.rb', line 101 def define_device_id if Helpers::Files.file_exists?(@device_id_path) read_device_id else generate_device_id end end |
#define_performed_action(key, value) ⇒ Object
169 170 171 |
# File 'lib/adminix/entities/service.rb', line 169 def define_performed_action(key, value) @performed_actions[key] = value end |
#ensure_credentials_exists! ⇒ Object
96 97 98 99 |
# File 'lib/adminix/entities/service.rb', line 96 def ensure_credentials_exists! return unless id_defined? Helpers::Output.display_error_and_exit('Credentials are not provided') end |
#generate_device_id(id = nil) ⇒ Object
119 120 121 122 |
# File 'lib/adminix/entities/service.rb', line 119 def generate_device_id(id = nil) @device_id = id || SecureRandom.uuid Helpers::Files.write_plain_file(@device_id_path, @device_id) end |
#id_defined? ⇒ Boolean
190 191 192 |
# File 'lib/adminix/entities/service.rb', line 190 def id_defined? !(id || '').empty? end |
#load_stamps_to_api ⇒ Object
161 162 163 |
# File 'lib/adminix/entities/service.rb', line 161 def load_stamps_to_api @send_system_load ? @load_stamps.map(&:to_api) : [] end |
#lock ⇒ Object
202 203 204 |
# File 'lib/adminix/entities/service.rb', line 202 def lock @locked = true end |
#locked? ⇒ Boolean
198 199 200 |
# File 'lib/adminix/entities/service.rb', line 198 def locked? @locked == true end |
#logs_to_api ⇒ Object
157 158 159 |
# File 'lib/adminix/entities/service.rb', line 157 def logs_to_api @send_logs ? @logs.map(&:to_api) : [] end |
#new_jobs ⇒ Object
145 146 147 |
# File 'lib/adminix/entities/service.rb', line 145 def new_jobs @jobs.select(&:in_queue?) end |
#new_jobs? ⇒ Boolean
194 195 196 |
# File 'lib/adminix/entities/service.rb', line 194 def new_jobs? new_jobs.any? end |
#read_device_id ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/adminix/entities/service.rb', line 109 def read_device_id content = Helpers::Files.read_plain_file(@device_id_path) .split("\n").first.to_s if content.empty? Helpers::Output.display_error_and_exit("Device ID can't be empty") else @device_id = content end end |
#remove_completed_jobs ⇒ Object
30 31 32 33 34 |
# File 'lib/adminix/entities/service.rb', line 30 def remove_completed_jobs @jobs.each do |job| @jobs -= [job] if job.completed? end end |
#reset_performed_actions ⇒ Object
165 166 167 |
# File 'lib/adminix/entities/service.rb', line 165 def reset_performed_actions @performed_actions.keys.map { |key| @performed_actions[key] = false } end |
#shift_load_stamps(number) ⇒ Object
80 81 82 |
# File 'lib/adminix/entities/service.rb', line 80 def shift_load_stamps(number) @load_stamps.shift(number) end |
#shift_logs(number) ⇒ Object
62 63 64 |
# File 'lib/adminix/entities/service.rb', line 62 def shift_logs(number) @logs.shift(number) end |
#sync_payload ⇒ Object
TODO: real total memory and disk
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/adminix/entities/service.rb', line 125 def sync_payload { device_id: @device_id, watcher_started: @performed_actions[:watcher_started], watcher_updated: @performed_actions[:watcher_updated], process_stopped: @performed_actions[:process_stopped], process_started: @performed_actions[:process_started], process_updated: @performed_actions[:process_updated], completed_jobs: completed_jobs_to_api, logs: logs_to_api, system_load: { stamps: load_stamps_to_api, total: { memory: 1024, disk: 10_240 } } } end |
#unlock ⇒ Object
206 207 208 |
# File 'lib/adminix/entities/service.rb', line 206 def unlock @locked = false end |