kinetic 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0fdcf06cf8d1fc5fff905da13c55c97c5c30f6bc
4
- data.tar.gz: ff4a1483557bb7abd8863a87000795b3648ad0db
3
+ metadata.gz: e122b819ed7edf00284f34983a5c86871638256b
4
+ data.tar.gz: 320b36ce33d0ec79469250b62710fad9c363ecfb
5
5
  SHA512:
6
- metadata.gz: 84695516ec05676070029529c68f77aaafd9a72204f329121cbf39eca176a6fe8546074b8b3072d867ce4820ed8bb6817360355e57eba23090e3a790c6906ea5
7
- data.tar.gz: 40ad8453c43d7e53a00f0552dfb703329a946e41085211694cf1432a8f94b349d8545734ede32f73b1b9757af7b023169c3054c773b480617b965b89d57a81f7
6
+ metadata.gz: b6b6d4454c50f76b388c3e46cd0c6182b083d57b10871a2661da0056f5f0115709fc9d11fd92cc38d95851740ee87dc3bf98d80d4d80b8e1adbc61afdf048724
7
+ data.tar.gz: fd867edf038cd1514c775c397192dae3c8e5ee35696fb185c33adef8579b6f7ddb4d242286e1e331c1b061347a08927750f64c03ca97d1cd94c2e5ecdeefc045
@@ -5,4 +5,6 @@ set :port, 5672
5
5
 
6
6
  on('message') do |message|
7
7
  puts message
8
- end
8
+ end
9
+
10
+ timed_event(:simple_event, 5) { Kinetic::Publisher.publish('message', message: 'Hello World!') }
@@ -2,6 +2,7 @@
2
2
  require 'logger'
3
3
  require 'fileutils'
4
4
  require 'yaml'
5
+ require 'singleton'
5
6
 
6
7
  # external dependencies
7
8
  require 'amqp'
@@ -9,6 +10,7 @@ require 'confstruct'
9
10
  require 'kgio'
10
11
  require 'active_support/core_ext/module/delegation'
11
12
  require 'active_support/core_ext/hash/reverse_merge'
13
+ require 'active_support/concern'
12
14
  require 'active_support/inflections'
13
15
  require 'awesome_print'
14
16
  require 'awesome_print/core_ext/logger'
@@ -21,57 +23,49 @@ require_relative '../kinetic/master'
21
23
  require_relative '../kinetic/worker'
22
24
  require_relative '../kinetic/dsl'
23
25
  require_relative '../kinetic/errors'
26
+ require_relative '../kinetic/publisher'
24
27
 
25
28
  module Kinetic
26
29
 
27
30
  module Delegator
28
31
 
29
- %w{on config logging set config_file deserialize}.each { |m| delegate m.to_sym, to: :application }
32
+ Kinetic::DSL.public_instance_methods.each.each { |m| delegate m, to: :application }
30
33
 
31
34
  def application
32
- Application
35
+ Application.instance
33
36
  end
34
37
 
35
38
 
36
39
  end
37
40
 
38
41
  class Base
42
+ include Singleton
43
+ include Kinetic::DSL
44
+
45
+ def initialize
46
+ logger.info "Kinetic #{Kinetic::VERSION}"
47
+ logger.debug "Loading serializer #{config.serializer}"
48
+ require "kinetic/serializers/#{config.serializer.downcase}"
49
+ @serializer = Object.const_get("Kinetic::Serializers::#{config.serializer}")
50
+ end
39
51
 
40
- class << self
41
- include Kinetic::DSL
42
-
43
-
44
- def after_fork_procs
45
- @after_fork_procs ||= []
46
- end
47
-
48
- def exchanges
49
- @exchanges ||= {}
50
- end
51
-
52
- def deserialize(message)
53
- @serializer.deserialize(message)
54
- end
55
-
56
- private
57
-
58
- def set_serializer(serializer)
59
- logger.debug "Loading serializer #{serializer}"
60
- require "kinetic/serializers/#{serializer.downcase}"
61
- @serializer = Object.const_get("Kinetic::Serializers::#{serializer}")
62
- end
52
+ private
63
53
 
64
- def direct
65
- exchanges[:direct] ||= {}
66
- end
54
+ def direct
55
+ exchanges[:direct] ||= {}
56
+ end
67
57
 
58
+ def after_fork_procs
59
+ @after_fork_procs ||= []
68
60
  end
69
61
 
70
- def initialize
71
- self.class.send(:set_serializer, config.serializer)
62
+ def exchanges
63
+ @exchanges ||= {}
72
64
  end
73
65
 
74
- delegate :logger, :set, :get, :get!, :exchanges, :after_fork_procs, :deserialize, :config, to: :class
66
+ def timed_events
67
+ @timed_events ||= {}
68
+ end
75
69
 
76
70
  end
77
71
 
@@ -17,7 +17,7 @@ module Kinetic
17
17
  class Application < Base
18
18
  end
19
19
 
20
- at_exit { Master.new(Application.new).send(ARGV[0] || 'run') if $!.nil? }
20
+ at_exit { Master.new(Application.instance).send(ARGV[0] || 'run') if $!.nil? }
21
21
  else
22
22
  puts CLI::PARSER.banner
23
23
  exit(1)
@@ -1,3 +1,4 @@
1
+ require 'confstruct'
1
2
  module Kinetic
2
3
 
3
4
  # Configuration can be set either by passing the {Kinetic::DSL#set set} method and or by using the
@@ -1,9 +1,13 @@
1
1
  require_relative 'dsl/configuration'
2
+ require_relative 'dsl/timed_events'
3
+ require_relative 'dsl/serialization'
2
4
  module Kinetic
3
5
 
4
6
  module DSL
5
7
 
6
8
  include Configuration
9
+ include TimedEvents
10
+ include Serialization
7
11
 
8
12
  def after_fork(&block)
9
13
  raise Kinetic::Errors::BlockMissing unless block_given?
@@ -11,7 +15,6 @@ module Kinetic
11
15
  end
12
16
 
13
17
 
14
-
15
18
  # Defines a direct queue subscription. Direct queues do not allow fuzzy matching so all messages sent to this queue
16
19
  # must exactly match the key.
17
20
  #
@@ -0,0 +1,11 @@
1
+ module Kinetic
2
+ module DSL
3
+ module Serialization
4
+
5
+ def deserialize(message)
6
+ @serializer.deserialize(message)
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module Kinetic
2
+ module DSL
3
+ module TimedEvents
4
+
5
+ # Creates a timed event that fired at the set interval. Can be used to broadcast messages
6
+ # to all connected workers.
7
+ def timed_event(label, interval, options={}, &block)
8
+ timed_events[label] = {interval: interval, options: options, block: block}
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -7,7 +7,7 @@ module Kinetic
7
7
  WORKERS = {}
8
8
  SIG_QUEUE = []
9
9
 
10
- delegate :logger, :config, :set, :after_fork_procs, to: :app
10
+ delegate :logger, :config, :set, to: :app
11
11
 
12
12
  attr_reader :app, :reexecpid
13
13
 
@@ -32,7 +32,7 @@ module Kinetic
32
32
  daemonize if config[:daemonize]
33
33
  enable_logging!
34
34
  logger.debug 'Configuration:'
35
- logger.ap app.class.send(:config).to_hash
35
+ logger.ap app.config.to_hash
36
36
  proc_name 'master'
37
37
  initialize_self_pipe!
38
38
  initialize_signal_traps!
@@ -129,7 +129,7 @@ module Kinetic
129
129
  begin
130
130
  proc_name "worker-#{worker_nr}"
131
131
  logger.debug 'Calling after fork procs'
132
- after_fork_procs.each { |p| p.call }
132
+ app.send(:after_fork_procs).each { |p| p.call }
133
133
  #noinspection RubyArgCount
134
134
  worker.run
135
135
  rescue => e
@@ -1,6 +1,92 @@
1
1
  require 'logger'
2
-
3
- require 'msgpack'
4
2
  require 'bunny'
3
+ require 'singleton'
4
+ require 'active_support/core_ext/module/delegation'
5
+
6
+
7
+ require_relative 'dsl/configuration'
8
+ require_relative './configuration'
9
+
10
+ module Kinetic
11
+
12
+ class Publisher
13
+ include Kinetic::DSL::Configuration
14
+ include Singleton
15
+
16
+ class << self
17
+
18
+ def method_missing(meth, *args, &block)
19
+ self.instance.send(meth, *args, &block)
20
+ end
21
+
22
+ end
23
+
24
+ def initialize
25
+ logger.debug "Using serializer #{config.serializer}"
26
+ require_relative "./serializers/#{config.serializer.to_s.downcase}"
27
+ @serializer = Kinetic::Serializers.const_get(config.serializer)
28
+ end
29
+
30
+ def publish_topic(key, message)
31
+ topic_exchange.publish(serialize(message), routing_key: key)
32
+ true
33
+ end
34
+
35
+ def publish_fanout(key, message)
36
+ fanout_exchange.publish(serialize(message), routing_key: key)
37
+ end
38
+
39
+ def publish_direct(key, message)
40
+ direct_exchange.publish(serialize(message), routing_key: key)
41
+ end
42
+
43
+ alias :publish :publish_direct
44
+
45
+ private
46
+
47
+ def topic_exchange
48
+ exchange(:topic)
49
+ end
50
+
51
+ def fanout_exchange
52
+ exchange(:fanout)
53
+ end
54
+
55
+ def direct_exchange
56
+ exchange(:direct)
57
+ end
58
+
59
+ def serialize(message)
60
+ @serializer.serialize(message)
61
+ end
62
+
63
+ def deserialize(message)
64
+ @serializer.deserialize(message)
65
+ end
66
+
67
+ private
68
+
69
+ def exchange(type)
70
+ Bunny::Exchange.new(channel, type, "#{config.name}.#{type}")
71
+ end
72
+
73
+
74
+ def channel
75
+ unless @connection
76
+ logger.info "Establishing publisher connection to amqp://#{config.host}:#{config.port}"
77
+ @connection = Bunny.new(host: config.host, port: config.port, threaded: false)
78
+ logger.debug 'Starting connection'
79
+ @connection.start
80
+ @channel = @connection.channel
81
+ end
82
+ @channel
83
+ end
84
+
85
+ def logger
86
+ @logger ||= Logger.new(File.join(config.root, config.log_file))
87
+ end
88
+
89
+
90
+ end
5
91
 
6
- #TODO: Finish publisher
92
+ end
@@ -1,3 +1,3 @@
1
1
  module Kinetic
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -29,6 +29,7 @@ module Kinetic
29
29
  initialize_channel!(connection)
30
30
  initialize_exchanges!
31
31
  initialize_subscribers!
32
+ initialize_timed_events!
32
33
  end
33
34
  end
34
35
 
@@ -60,7 +61,7 @@ module Kinetic
60
61
  logger.debug 'Initializing exchanges'
61
62
  @exchanges = {}
62
63
  prefix = config.name
63
- app.exchanges.each_key do |name|
64
+ app.send(:exchanges).each_key do |name|
64
65
  logger.debug " Initializing #{name} exchange"
65
66
  @exchanges[name] = channel.send(name, "#{prefix}.#{name}")
66
67
  end
@@ -68,12 +69,12 @@ module Kinetic
68
69
 
69
70
  def initialize_subscribers!
70
71
  logger.debug 'Initializing subscribers'
71
- app.exchanges.each do |name, value|
72
+ app.send(:exchanges).each do |name, value|
72
73
  logger.debug " Building subscribers for '#{name}' exchange"
73
74
  value.each do |queue, block|
74
75
  logger.debug " Initializing queue for '#{queue}'"
75
76
  q = channel.queue(queue)
76
- logger.debug " Binding queue to '#{name}' exchange"
77
+ logger.debug " Binding queue to '#{name}' exchange with routing_key '#{queue}'"
77
78
  q.bind(exchanges[name], routing_key: queue)
78
79
  logger.debug " Subscribing to messages for '#{queue}'"
79
80
  q.subscribe do |meta, payload|
@@ -87,5 +88,13 @@ module Kinetic
87
88
  end
88
89
  end
89
90
 
91
+ def initialize_timed_events!
92
+ logger.debug 'Initializing timed events'
93
+ app.send(:timed_events).each do |label, event|
94
+ logger.debug " Creating timer for #{label}"
95
+ event[:timer] = EM.add_periodic_timer(event[:interval]) { event[:block].call }
96
+ end
97
+ end
98
+
90
99
  end
91
100
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kinetic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jarod Reid
@@ -186,6 +186,8 @@ files:
186
186
  - lib/kinetic/configuration.rb
187
187
  - lib/kinetic/dsl.rb
188
188
  - lib/kinetic/dsl/configuration.rb
189
+ - lib/kinetic/dsl/serialization.rb
190
+ - lib/kinetic/dsl/timed_events.rb
189
191
  - lib/kinetic/errors.rb
190
192
  - lib/kinetic/master.rb
191
193
  - lib/kinetic/publisher.rb
@@ -215,7 +217,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
215
217
  version: '0'
216
218
  requirements: []
217
219
  rubyforge_project:
218
- rubygems_version: 2.0.3
220
+ rubygems_version: 2.0.14
219
221
  signing_key:
220
222
  specification_version: 4
221
223
  summary: A powerful yet simple AMQP worker framework
@@ -223,3 +225,4 @@ test_files:
223
225
  - spec/base_spec.rb
224
226
  - spec/dsl_spec.rb
225
227
  - spec/spec_helper.rb
228
+ has_rdoc: