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
# File 'lib/osctld/history.rb', line 40

def instance
  return @@instance if @@instance
  @@instance = new
end

Instance Method Details

#assets(pool, add) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/osctld/history.rb', line 59

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

#close(pool) ⇒ Object



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

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

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



128
129
130
131
132
133
134
# File 'lib/osctld/history.rb', line 128

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

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

#log(pool, cmd, opts) ⇒ Object



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

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

#log_path(pool) ⇒ Object (protected)



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

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

#main_loopObject (protected)



95
96
97
98
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
# File 'lib/osctld/history.rb', line 95

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', 0400)
      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 { |f| f.close }
end

#open(pool) ⇒ Object



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

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

#read(pool) ⇒ Object



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

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

#startObject



69
70
71
# File 'lib/osctld/history.rb', line 69

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

#stopObject



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

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

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



136
137
138
139
# File 'lib/osctld/history.rb', line 136

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