diff options
author | rhenium <rhenium@rhe.jp> | 2014-05-04 05:56:15 +0900 |
---|---|---|
committer | rhenium <rhenium@rhe.jp> | 2014-05-04 05:56:15 +0900 |
commit | 193d3cfde19324c910131afef57612ae87ecc776 (patch) | |
tree | 3de5d1189b92df334fee697473721fcc79603fc5 | |
parent | 20fb53a32a10182c82efedc15698bd1927cb6519 (diff) | |
download | aclog-193d3cfde19324c910131afef57612ae87ecc776.tar.gz |
add status page
-rw-r--r-- | app/controllers/about_controller.rb | 9 | ||||
-rw-r--r-- | app/controllers/sessions_controller.rb | 5 | ||||
-rw-r--r-- | app/models/account.rb | 17 | ||||
-rw-r--r-- | app/models/worker_manager.rb | 28 | ||||
-rw-r--r-- | app/views/about/status.html.haml | 28 | ||||
-rw-r--r-- | config/routes.rb | 2 | ||||
-rw-r--r-- | lib/aclog/exceptions.rb | 2 | ||||
-rw-r--r-- | lib/collector/control_server.rb | 16 | ||||
-rw-r--r-- | lib/collector/daemon.rb | 5 | ||||
-rw-r--r-- | lib/collector/node_connection.rb | 3 | ||||
-rw-r--r-- | lib/collector/node_manager.rb | 39 |
11 files changed, 117 insertions, 37 deletions
diff --git a/app/controllers/about_controller.rb b/app/controllers/about_controller.rb index 5e60063..bd58998 100644 --- a/app/controllers/about_controller.rb +++ b/app/controllers/about_controller.rb @@ -2,4 +2,13 @@ class AboutController < ApplicationController def index render layout: "index" end + + def status + @worker_status = WorkerManager.status + + if logged_in? + @your_node_number = current_user.account.id % Settings.collector.nodes_count + end + rescue + end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index f2201c8..f9a500a 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -5,7 +5,10 @@ class SessionsController < ApplicationController account = Account.create_or_update(user_id: auth["uid"], oauth_token: auth["credentials"]["token"], oauth_token_secret: auth["credentials"]["secret"]) - account.update_connection + begin + WorkerManager.update_account(account) + rescue Aclog::Exceptions::WorkerConnectionError + end User.create_from_json(id: account.user_id, screen_name: auth["extra"]["raw_info"]["screen_name"], diff --git a/app/models/account.rb b/app/models/account.rb index 4014200..ecad01e 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -26,21 +26,10 @@ class Account < ActiveRecord::Base def deactivate! self.status = Account::INACTIVE - self.save! if self.changed? + self.save! - update_connection - end - - def update_connection - transport = MessagePack::RPC::UNIXTransport.new - client = MessagePack::RPC::Client.new(transport, Rails.root.join("tmp", "sockets", "receiver.sock").to_s) - if active? - client.call(:register_account, Marshal.dump(self)) - else - client.call(:deactivate_account, Marshal.dump(self)) - end - rescue Errno::ECONNREFUSED, Errno::ENOENT - logger.error("Account#update_connection: couldn't connect to the receiver") + WorkerManager.update_account(self) + rescue Aclog::Exceptions::WorkerConnectionError end def client diff --git a/app/models/worker_manager.rb b/app/models/worker_manager.rb index a015a98..e5fc8b8 100644 --- a/app/models/worker_manager.rb +++ b/app/models/worker_manager.rb @@ -1,4 +1,30 @@ +require "msgpack/rpc/transport/unix" class WorkerManager - def alive? + class << self + def alive? + client && true rescue false + end + + def update_account(account) + if account.active? + client.call(:register_account, Marshal.dump(account)) + else + client.call(:deactivate_account, Marshal.dump(account)) + end + end + + def status + Marshal.load(client.call(:status)) + end + + private + def client + @client ||= begin + transport = MessagePack::RPC::UNIXTransport.new + client = MessagePack::RPC::Client.new(transport, Rails.root.join("tmp", "sockets", "receiver.sock").to_s) + end + rescue Errno::ECONNREFUSED, Errno::ENOENT + raise Aclog::Exceptions::WorkerConnectionError, "Couldn't connect to the background worker" + end end end diff --git a/app/views/about/status.html.haml b/app/views/about/status.html.haml new file mode 100644 index 0000000..195e5fa --- /dev/null +++ b/app/views/about/status.html.haml @@ -0,0 +1,28 @@ +- title "Status" +.container + %h1 Status + - if @worker_status + %h2 Worker Status + %table.table + %thead + %tr + %th Group + %th Status + %th Uptime + %tbody + - @worker_status[:active_node_statuses].each_with_index do |node, i| + - if node + %tr + %td= i + %td Working + %td= "#{(Time.now - node[:start_time]).to_i / 60} minutes" + - else + %tr.danger + %td= i + %td Down + %td - + - if logged_in? + %p Your group is #{@your_node_number}. + - else + .alert.alert-danger + %strong Couldn't communicate with the collector service. diff --git a/config/routes.rb b/config/routes.rb index 5e50c84..8e8c500 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,8 @@ Rails.application.routes.draw do mount Api => "/api" + get "/i/status" => "about#status", as: "status" + # Internals / SessionsController get "/i/callback" => "sessions#create" get "/i/logout" => "sessions#destroy", as: "logout" diff --git a/lib/aclog/exceptions.rb b/lib/aclog/exceptions.rb index 1a58755..5ffe609 100644 --- a/lib/aclog/exceptions.rb +++ b/lib/aclog/exceptions.rb @@ -16,5 +16,7 @@ module Aclog class OAuthEchoError < Unauthorized; end class OAuthEchoUnauthorized < OAuthEchoError; end + + class WorkerConnectionError < AclogError; end end end diff --git a/lib/collector/control_server.rb b/lib/collector/control_server.rb index 413345e..5a22d08 100644 --- a/lib/collector/control_server.rb +++ b/lib/collector/control_server.rb @@ -7,5 +7,21 @@ module Collector def deactivate_account(account) NodeManager.unregister_account(Marshal.load(account)) end + + def status + active_node_statuses = Settings.collector.nodes_count.times.map do |number| + node = NodeManager.active_connections[number] + if node + { start_time: node.start_time } + else + nil + end + end + + res = { start_time: Daemon.start_time, + active_node_statuses: active_node_statuses, + inactive_nodes_count: NodeManager.inactive_connections.size } + Marshal.dump(res) + end end end diff --git a/lib/collector/daemon.rb b/lib/collector/daemon.rb index 8c7194f..f77c6eb 100644 --- a/lib/collector/daemon.rb +++ b/lib/collector/daemon.rb @@ -1,9 +1,12 @@ require "msgpack/rpc/transport/unix" module Collector - class Daemon + module Daemon class << self + attr_reader :start_time + def start + @start_time = Time.now set_loggers EM.run do diff --git a/lib/collector/node_connection.rb b/lib/collector/node_connection.rb index 06fbf3f..9b9134b 100644 --- a/lib/collector/node_connection.rb +++ b/lib/collector/node_connection.rb @@ -1,6 +1,6 @@ module Collector class NodeConnection < EM::Connection - attr_reader :connection_id + attr_reader :connection_id, :start_time @@_id = 0 @@ -9,6 +9,7 @@ module Collector @connection_id = (@@_id += 1) @authenticated = false @closing = false + @start_time = Time.now end def unbind diff --git a/lib/collector/node_manager.rb b/lib/collector/node_manager.rb index 8136fe8..f47c7cf 100644 --- a/lib/collector/node_manager.rb +++ b/lib/collector/node_manager.rb @@ -1,58 +1,59 @@ module Collector - class NodeManager - @@node_connections = [] - @@active_connections = Array.new(Settings.collector.nodes_count) - @@inactive_connections = [] + module NodeManager + @node_connections = [] + @active_connections = Array.new(Settings.collector.nodes_count) + @inactive_connections = [] class << self + attr_reader :node_connections, :active_connections, :inactive_connections + def register(node_connection) - @@node_connections << node_connection - @@inactive_connections << node_connection + self.node_connections << node_connection + self.inactive_connections << node_connection bind end def unregister(node_connection) - @@node_connections.delete(node_connection) - i = @@active_connections.find_index(node_connection) + self.node_connections.delete(node_connection) + i = self.active_connections.find_index(node_connection) if i - @@active_connections[i] = nil + self.active_connections[i] = nil else - @@inactive_connections.delete(node_connection) + self.inactive_connections.delete(node_connection) end bind end def register_account(account) n = account.id % Settings.collector.nodes_count - if @@active_connections[n] - @@active_connections[n].register_account(account) + if self.active_connections[n] + self.active_connections[n].register_account(account) end end def unregister_account(account) n = account.id % Settings.collector.nodes_count - if @@active_connections[n] - @@active_connections[n].unregister_account(account) + if self.active_connections[n] + self.active_connections[n].unregister_account(account) end end private def bind - first_inactive_id = @@active_connections.find_index(nil) + first_inactive_id = self.active_connections.find_index(nil) if first_inactive_id - con = @@inactive_connections.shift + con = self.inactive_connections.shift if con - @@active_connections[first_inactive_id] = con + self.active_connections[first_inactive_id] = con Rails.logger.warn("[NodeManager] Registered node ##{con.connection_id} as group ##{first_inactive_id}") Account.for_node(first_inactive_id).each do |a| con.register_account(a) end else - Rails.logger.warn("[NodeManager] Not enough nodes: (#{@@active_connections.count {|c| c }}/#{Settings.collector.nodes_count})") + Rails.logger.warn("[NodeManager] Not enough nodes: (#{self.active_connections.count {|c| c }}/#{Settings.collector.nodes_count})") end end end - end end end |