Class: Adminix::Service

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/adminix/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeService

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

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/adminix/service.rb', line 9

def id
  @id
end

#new_logsObject

Returns the value of attribute new_logs.



8
9
10
# File 'lib/adminix/service.rb', line 8

def new_logs
  @new_logs
end

#process_idObject (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_logsObject (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_logsObject (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_linesObject



94
95
96
97
98
# File 'lib/adminix/service.rb', line 94

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

#download_sourceObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/adminix/service.rb', line 114

def download_source
  data = fetch_options
  unless data['success']
    puts 'Error, please try again later!'
    return
  end

  vars = data['result'] || []
  bin = 'git'
  repo_var = vars.find { |v| v['key'] == 'git_repo' }
  branch_var = vars.find { |v| v['key'] == 'git_branch' }

  if repo_var && repo_var['value'] && branch_var
    repo = repo_var['value']
    branch = branch_var['value'] || 'master'
    `#{bin} clone #{repo} -b #{branch}`
  else
    puts 'Please define your GIT repository and branch'
  end
end

#fetch_optionsObject



90
91
92
# File 'lib/adminix/service.rb', line 90

def fetch_options
  Helpers::HTTP.get("services/#{id}/options")
end

#options_to_envsObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/adminix/service.rb', line 68

def options_to_envs
  data = fetch_options

  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_inlineObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/adminix/service.rb', line 79

def options_to_envs_inline
  data = fetch_options
  str = 'export'

  data['result'].each do |o|
    str << " #{o['key'].upcase}=\"#{o['value']}\"" 
  end

  str
end

#restart!Object



100
101
102
# File 'lib/adminix/service.rb', line 100

def restart!
  stop!
end

#stop!Object



104
105
106
107
108
109
110
111
112
# File 'lib/adminix/service.rb', line 104

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_cableObject



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