Class: OsCtld::History

Inherits:
Object
  • Object
show all
Defined in:
lib/osctld/history.rb

Defined Under Namespace

Classes: Reader

Constant Summary collapse

@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



40
41
42
43
44
# File 'lib/osctld/history.rb', line 40

def instance
  return @@instance if @@instance

  @@instance = new
end

Instance Method Details

#assets(pool, add) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/osctld/history.rb', line 62

def assets(pool, add)
  add.file(
    log_path(pool),
    desc: 'Pool history',
    user: 0,
    group: 0,
    mode: 0o400
  )
end

#close(pool) ⇒ Object



85
86
87
# File 'lib/osctld/history.rb', line 85

def close(pool)
  @queue << [:close, pool]
end

#do_log(files, pool, cmd, opts) ⇒ Object (protected)



132
133
134
135
136
137
138
# File 'lib/osctld/history.rb', line 132

def do_log(files, pool, cmd, opts)
  unless files.has_key?(pool.name)
    files[pool.name] = File.open(log_path(pool), 'a', 0o400)
  end

  write_log(files[pool.name], cmd, opts)
end

#log(pool, cmd, opts) ⇒ Object



89
90
91
# File 'lib/osctld/history.rb', line 89

def log(pool, cmd, opts)
  @queue << [:log, pool, cmd, opts]
end

#log_path(pool) ⇒ Object (protected)



145
146
147
# File 'lib/osctld/history.rb', line 145

def log_path(pool)
  File.join(pool.log_path, '.history')
end

#main_loopObject (protected)



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/osctld/history.rb', line 99

def main_loop
  files = {}

  loop do
    cmd, *args = @queue.pop

    case cmd
    when :stop
      break

    when :open
      pool = args.pop

      unless files.has_key?(pool.name)
        files[pool.name] = File.open(log_path(pool), 'a', 0o400)
      end

    when :close
      pool = args.pop

      if files.has_key?(pool.name)
        files[pool.name].close
        files.delete(pool.name)
      end

    when :log
      do_log(files, *args)
    end
  end

  files.each_value(&:close)
end

#open(pool) ⇒ Object



81
82
83
# File 'lib/osctld/history.rb', line 81

def open(pool)
  @queue << [:open, pool]
end

#read(pool) ⇒ Object



93
94
95
# File 'lib/osctld/history.rb', line 93

def read(pool)
  Reader.new(log_path(pool))
end

#startObject



72
73
74
# File 'lib/osctld/history.rb', line 72

def start
  @thread = Thread.new { main_loop }
end

#stopObject



76
77
78
79
# File 'lib/osctld/history.rb', line 76

def stop
  @queue << [:stop]
  @thread.join
end

#write_log(file, cmd, opts) ⇒ Object (protected)



140
141
142
143
# File 'lib/osctld/history.rb', line 140

def write_log(file, cmd, opts)
  file.puts({ time: Time.now.to_i, cmd:, opts: }.to_json)
  file.flush
end