miu 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/miu.rb CHANGED
@@ -1,6 +1,14 @@
1
1
  require 'miu/version'
2
2
 
3
3
  module Miu
4
+ autoload :CLI, 'miu/cli'
5
+ autoload :Command, 'miu/command'
6
+ autoload :Server, 'miu/server'
7
+ autoload :Message, 'miu/message'
8
+ autoload :Socket, 'miu/socket'
9
+ autoload :Publisher, 'miu/publisher'
10
+ autoload :Subscriber, 'miu/subscriber'
11
+
4
12
  class << self
5
13
  def root
6
14
  @root ||= find_root 'Gemfile'
@@ -10,33 +18,20 @@ module Miu
10
18
  22200
11
19
  end
12
20
 
13
- def default_rpc_port
21
+ def default_god_port
14
22
  default_port
15
23
  end
16
24
 
17
- def default_god_port
25
+ def default_pub_port
18
26
  default_port + 1
19
27
  end
20
28
 
21
- def default_god_config
22
- 'config/miu.god'
23
- end
24
-
25
- def default_fluent_config
26
- 'config/fluent.conf'
27
- end
28
-
29
- def plugins
30
- @plugins ||= {}
29
+ def default_sub_port
30
+ default_port + 2
31
31
  end
32
32
 
33
- def register(name, plugin, options = {}, &block)
34
- name = name.to_s
35
- usage = options[:usage] || "#{name} [COMMAND]"
36
- desc = options[:desc] || plugin.to_s
37
-
38
- plugins[name] = plugin
39
- Miu::CLI.register generate_subcommand(name, plugin, &block), name, usage, desc if block
33
+ def default_god_config
34
+ 'config/miu.god'
40
35
  end
41
36
 
42
37
  def find_root(flag, base = nil)
@@ -49,5 +44,19 @@ module Miu
49
44
  raise 'Could not find root path' unless path
50
45
  Pathname.new File.realpath(path)
51
46
  end
47
+
48
+ def plugins
49
+ @plugins ||= {}
50
+ end
51
+
52
+ def register(name, plugin, options = {}, &block)
53
+ Miu.plugins[name] = plugin
54
+ if block
55
+ usage = options[:usage] || "#{name} [COMMAND]"
56
+ desc = options[:desc] || plugin.to_s
57
+ command = Miu::Command.new name, plugin, &block
58
+ Miu::CLI.register command, name, usage, desc
59
+ end
60
+ end
52
61
  end
53
62
  end
@@ -1,5 +1,5 @@
1
- require 'thor'
2
1
  require 'miu'
2
+ require 'thor'
3
3
 
4
4
  module Miu
5
5
  class CLI < ::Thor
@@ -23,28 +23,39 @@ module Miu
23
23
  say "Miu #{Miu::VERSION}"
24
24
  end
25
25
 
26
- desc 'list', 'Lists plugins'
27
- def list
28
- table = Miu.plugins.map { |k, v| [k, "# #{v}" ] }
29
-
30
- say 'Plugins:'
31
- print_table table, :indent => 2, :truncate => true
32
- say
33
- end
34
-
35
26
  desc 'init', 'Generates a miu configuration files'
36
27
  def init
37
28
  copy_file 'Gemfile'
38
29
  inside 'config' do
39
- template 'fluent.conf'
40
30
  template 'miu.god'
41
31
  end
42
32
  empty_directory 'log'
43
33
  empty_directory 'tmp/pids'
44
34
  end
45
35
 
36
+ desc 'list', 'Lists plugins'
37
+ def list
38
+ require 'miu/plugins'
39
+ table = Miu.plugins.map { |k, v| [k, "# #{v}" ] }
40
+ say 'Plugins:'
41
+ print_table table, :indent => 2, :truncate => true
42
+ say
43
+ end
44
+
46
45
  desc 'start', 'Start miu'
46
+ option 'pub-host', :type => :string, :default => '127.0.0.1', :desc => 'pub host address'
47
+ option 'pub-port', :type => :numeric, :default => Miu.default_pub_port, :desc => 'pub listen port'
48
+ option 'sub-host', :type => :string, :default => '127.0.0.1', :desc => 'sub host address'
49
+ option 'sub-port', :type => :numeric, :default => Miu.default_sub_port, :desc => 'sub listen port'
47
50
  def start
51
+ opts = options.dup
52
+ opts.keys.each { |k| opts[k.gsub('-', '_')] = opts.delete(k) if k.is_a?(::String) }
53
+ server = Miu::Server.new opts
54
+ server.run
55
+ end
56
+
57
+ desc 'supervise', 'Supervise miu and plugins'
58
+ def supervise
48
59
  require 'god'
49
60
  run "bundle exec god -c #{Miu.default_god_config}"
50
61
  end
@@ -57,7 +68,3 @@ module Miu
57
68
  end
58
69
  end
59
70
  end
60
-
61
- # load built-in plugins
62
- require 'miu/plugins'
63
-
@@ -0,0 +1,30 @@
1
+ require 'miu'
2
+
3
+ module Miu
4
+ class Command
5
+ def self.new(name, plugin, options = {}, &block)
6
+ require 'thor'
7
+ Class.new ::Thor do
8
+ include ::Thor::Actions
9
+ add_runtime_options!
10
+
11
+ class << self
12
+ def source_root
13
+ Miu.find_root('Gemfile', plugin.called_from)
14
+ end
15
+
16
+ def destination_root
17
+ Miu.root
18
+ end
19
+
20
+ def banner(task, namespace = nil, subcommand = nil)
21
+ super task, namespace, subcommand || options.fetch(:subcommand, true)
22
+ end
23
+ end
24
+
25
+ namespace name
26
+ class_eval &block if block
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,24 @@
1
+ require 'msgpack'
2
+
3
+ module Miu
4
+ class Message
5
+ attr_accessor :tag, :time, :body
6
+
7
+ def initialize(*args)
8
+ @tag = args.shift
9
+ @body = args.pop
10
+ @time = args.shift || Time.now.to_i
11
+ end
12
+
13
+ def dump
14
+ [@tag, @time, @body.to_msgpack].map(&:to_s)
15
+ end
16
+
17
+ def self.load(parts)
18
+ tag = parts.shift
19
+ time = parts.shift
20
+ body = MessagePack.unpack(parts.shift)
21
+ new tag, time, body
22
+ end
23
+ end
24
+ end
@@ -17,42 +17,10 @@ module Miu
17
17
  attr_accessor :called_from
18
18
 
19
19
  def register(*args, &block)
20
- require 'miu/cli'
21
20
  options = args.last.is_a?(::Hash) ? args.pop : {}
22
21
  name = args.shift
23
22
  plugin = args.shift || self
24
- usage = options[:usage] || "#{name} [COMMAND]"
25
- desc = options[:desc] || plugin.to_s
26
-
27
- Miu.plugins[name] = plugin
28
- Miu::CLI.register generate_subcommand(name, plugin, &block), name, usage, desc if block
29
- end
30
-
31
- private
32
-
33
- def generate_subcommand(name, plugin, &block)
34
- require 'thor'
35
- Class.new ::Thor do
36
- include ::Thor::Actions
37
- add_runtime_options!
38
-
39
- class << self
40
- def source_root
41
- Miu.find_root('Gemfile', plugin.called_from)
42
- end
43
-
44
- def destination_root
45
- Miu.root
46
- end
47
-
48
- def banner(task, namespace = nil, subcommand = true)
49
- super
50
- end
51
- end
52
-
53
- namespace name
54
- class_eval &block if block
55
- end
23
+ Miu.register name, plugin, options, &block
56
24
  end
57
25
  end
58
26
  end
@@ -1,2 +1,4 @@
1
- require 'miu/plugins/null'
2
-
1
+ module Miu
2
+ module Plugins
3
+ end
4
+ end
@@ -0,0 +1,31 @@
1
+ require 'miu'
2
+ require 'miu/socket'
3
+ require 'miu/message'
4
+ require 'msgpack'
5
+
6
+ module Miu
7
+ class Publisher < Socket
8
+ def initialize(options = {})
9
+ options[:port] ||= Miu.default_sub_port
10
+ super socket_type, options
11
+
12
+ yield self if block_given?
13
+ end
14
+
15
+ # tag, time = nil, body
16
+ def send(*args)
17
+ message = Message.new *args
18
+ @socket.send_strings message.dump
19
+ end
20
+
21
+ private
22
+
23
+ def socket_type
24
+ if ZMQ::LibZMQ.version3?
25
+ ZMQ::XPUB
26
+ else
27
+ ZMQ::PUB
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,55 @@
1
+ require 'miu'
2
+ require 'miu/publisher'
3
+ require 'miu/subscriber'
4
+
5
+ module Miu
6
+ class Server
7
+ attr_reader :options
8
+ attr_reader :context, :publisher, :subscriber
9
+
10
+ def initialize(options = {})
11
+ @options = options
12
+ end
13
+
14
+ def run
15
+ pub_address = "#{@options[:pub_host]}:#{@options[:pub_port]}"
16
+ sub_address = "#{@options[:sub_host]}:#{@options[:sub_port]}"
17
+
18
+ @context = ZMQ::Context.new
19
+ @publisher = Publisher.new({
20
+ :context => @context,
21
+ :host => @options[:pub_host],
22
+ :port => @options[:pub_port]
23
+ })
24
+ @subscriber = Subscriber.new({
25
+ :context => @context,
26
+ :host => @options[:sub_host],
27
+ :port => @options[:sub_port]
28
+ })
29
+
30
+ @publisher.bind
31
+ @subscriber.bind
32
+
33
+ puts "Starting miu"
34
+ puts "pub: #{@publisher.host}:#{@publisher.port}"
35
+ puts "sub: #{@subscriber.host}:#{@subscriber.port}"
36
+
37
+ trap(:INT) do
38
+ puts "Quit"
39
+ close
40
+ exit
41
+ end
42
+
43
+ loop do
44
+ @subscriber.forward @publisher
45
+ end
46
+ end
47
+
48
+ def close
49
+ @subscriber.close
50
+ @publisher.close
51
+ @context.terminate
52
+ end
53
+ alias_method :shutdown, :close
54
+ end
55
+ end
@@ -0,0 +1,47 @@
1
+ require 'miu'
2
+ require 'ffi-rzmq'
3
+
4
+ module Miu
5
+ class Socket
6
+ attr_reader :host, :port
7
+ attr_reader :context, :socket
8
+
9
+ def initialize(socket_type, options = {})
10
+ @host = options[:host] || '127.0.0.1'
11
+ @port = options[:port]
12
+
13
+ if options[:context]
14
+ @context = options[:context]
15
+ @terminate_context = false
16
+ else
17
+ @context = ZMQ::Context.new(options[:io_threads] || 1)
18
+ @terminate_context = true
19
+ end
20
+
21
+ @socket = @context.socket socket_type
22
+ end
23
+
24
+ def bind
25
+ @socket.bind "tcp://#{@host}:#{@port}"
26
+ end
27
+
28
+ def connect
29
+ @socket.connect "tcp://#{@host}:#{@port}"
30
+ end
31
+
32
+ def forward(forwarder)
33
+ loop do
34
+ message = ZMQ::Message.new
35
+ @socket.recvmsg message
36
+ more = @socket.more_parts?
37
+ forwarder.socket.sendmsg message, (more ? ZMQ::SNDMORE : 0)
38
+ break unless more
39
+ end
40
+ end
41
+
42
+ def close
43
+ @socket.close
44
+ @context.terminate if @terminate_context
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,50 @@
1
+ require 'miu'
2
+ require 'miu/socket'
3
+
4
+ module Miu
5
+ class Subscriber < Socket
6
+ attr_reader :subscribe
7
+
8
+ def initialize(options = {})
9
+ options[:port] ||= Miu.default_pub_port
10
+ super socket_type, options
11
+
12
+ self.subscribe = options[:subscribe]
13
+ yield self if block_given?
14
+ end
15
+
16
+ def subscribe
17
+ @subscribe
18
+ end
19
+
20
+ def subscribe=(value)
21
+ @subscribe = value.to_s
22
+ @socket.setsockopt ZMQ::SUBSCRIBE, @subscribe
23
+ end
24
+
25
+ def recv
26
+ parts = []
27
+ @socket.recv_strings parts
28
+ Message.load parts
29
+ end
30
+
31
+ def each
32
+ if block_given?
33
+ loop do
34
+ message = recv rescue nil
35
+ yield message if message
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def socket_type
43
+ if ZMQ::LibZMQ.version3?
44
+ ZMQ::XSUB
45
+ else
46
+ ZMQ::SUB
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,3 +1,3 @@
1
1
  module Miu
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -2,7 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  group :assets do
4
4
  gem 'god'
5
- gem 'fluentd'
6
5
  end
7
6
 
8
7
  gem 'miu'
8
+
@@ -17,9 +17,9 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
- gem.add_dependency 'bundler'
21
- gem.add_dependency 'thor'
22
- gem.add_dependency 'saorin'
23
-
24
- gem.add_development_dependency 'rake'
20
+ gem.add_dependency 'bundler', '>= 1.2.0'
21
+ gem.add_dependency 'thor', '>= 0.17.0'
22
+ gem.add_dependency 'ffi-rzmq', '>= 1.0.0'
23
+ gem.add_dependency 'msgpack', '>= 0.5.3'
24
+ gem.add_development_dependency 'rake', '>= 10.0.3'
25
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: miu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-09 00:00:00.000000000 Z
12
+ date: 2013-03-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: 1.2.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: '0'
29
+ version: 1.2.0
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: thor
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
37
+ version: 0.17.0
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,15 +42,15 @@ dependencies:
42
42
  requirements:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
45
+ version: 0.17.0
46
46
  - !ruby/object:Gem::Dependency
47
- name: saorin
47
+ name: ffi-rzmq
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
- version: '0'
53
+ version: 1.0.0
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,23 @@ dependencies:
58
58
  requirements:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 1.0.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: msgpack
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 0.5.3
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 0.5.3
62
78
  - !ruby/object:Gem::Dependency
63
79
  name: rake
64
80
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +82,7 @@ dependencies:
66
82
  requirements:
67
83
  - - ! '>='
68
84
  - !ruby/object:Gem::Version
69
- version: '0'
85
+ version: 10.0.3
70
86
  type: :development
71
87
  prerelease: false
72
88
  version_requirements: !ruby/object:Gem::Requirement
@@ -74,7 +90,7 @@ dependencies:
74
90
  requirements:
75
91
  - - ! '>='
76
92
  - !ruby/object:Gem::Version
77
- version: '0'
93
+ version: 10.0.3
78
94
  description: miu miu
79
95
  email:
80
96
  - mail@mashiro.org
@@ -91,12 +107,17 @@ files:
91
107
  - bin/miu
92
108
  - lib/miu.rb
93
109
  - lib/miu/cli.rb
110
+ - lib/miu/command.rb
111
+ - lib/miu/message.rb
94
112
  - lib/miu/plugin.rb
95
113
  - lib/miu/plugins.rb
96
114
  - lib/miu/plugins/null.rb
115
+ - lib/miu/publisher.rb
116
+ - lib/miu/server.rb
117
+ - lib/miu/socket.rb
118
+ - lib/miu/subscriber.rb
97
119
  - lib/miu/version.rb
98
120
  - lib/templates/Gemfile
99
- - lib/templates/config/fluent.conf
100
121
  - lib/templates/config/miu.god
101
122
  - miu.gemspec
102
123
  homepage: ''
@@ -113,7 +134,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
134
  version: '0'
114
135
  segments:
115
136
  - 0
116
- hash: -3747572747096164549
137
+ hash: -2326395156696171372
117
138
  required_rubygems_version: !ruby/object:Gem::Requirement
118
139
  none: false
119
140
  requirements:
@@ -122,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
143
  version: '0'
123
144
  segments:
124
145
  - 0
125
- hash: -3747572747096164549
146
+ hash: -2326395156696171372
126
147
  requirements: []
127
148
  rubyforge_project:
128
149
  rubygems_version: 1.8.24