euston-daemons 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. data/Gemfile +4 -0
  2. data/Gemfile.lock +96 -0
  3. data/LICENSE +19 -0
  4. data/Rakefile +161 -0
  5. data/euston-daemons.gemspec +72 -0
  6. data/lib/euston-daemons/command_processor_daemon/config/environment.rb +17 -0
  7. data/lib/euston-daemons/command_processor_daemon/lib/components/command_handler_component.rb +55 -0
  8. data/lib/euston-daemons/command_processor_daemon/lib/daemon.rb +43 -0
  9. data/lib/euston-daemons/command_processor_daemon/lib/settings.rb +19 -0
  10. data/lib/euston-daemons/command_processor_daemon/rake_task.rb +31 -0
  11. data/lib/euston-daemons/event_processor_daemon/config/environment.rb +23 -0
  12. data/lib/euston-daemons/event_processor_daemon/lib/components/event_handler_component.rb +72 -0
  13. data/lib/euston-daemons/event_processor_daemon/lib/daemon.rb +72 -0
  14. data/lib/euston-daemons/event_processor_daemon/lib/settings.rb +22 -0
  15. data/lib/euston-daemons/event_processor_daemon/rake_task.rb +37 -0
  16. data/lib/euston-daemons/framework/basic_component.rb +33 -0
  17. data/lib/euston-daemons/framework/component_shutdown.rb +22 -0
  18. data/lib/euston-daemons/framework/daemon.rb +27 -0
  19. data/lib/euston-daemons/framework/queue.rb +71 -0
  20. data/lib/euston-daemons/message_buffer_daemon/config/environment.rb +30 -0
  21. data/lib/euston-daemons/message_buffer_daemon/lib/components/buffer_component.rb +74 -0
  22. data/lib/euston-daemons/message_buffer_daemon/lib/components/event_store_component.rb +51 -0
  23. data/lib/euston-daemons/message_buffer_daemon/lib/daemon.rb +48 -0
  24. data/lib/euston-daemons/message_buffer_daemon/lib/message_logger.rb +54 -0
  25. data/lib/euston-daemons/message_buffer_daemon/lib/publisher.rb +56 -0
  26. data/lib/euston-daemons/message_buffer_daemon/lib/read_model/message_buffer.rb +54 -0
  27. data/lib/euston-daemons/message_buffer_daemon/lib/read_model/message_log.rb +36 -0
  28. data/lib/euston-daemons/message_buffer_daemon/lib/settings.rb +14 -0
  29. data/lib/euston-daemons/message_buffer_daemon/lib/subscriber.rb +60 -0
  30. data/lib/euston-daemons/message_buffer_daemon/rake_task.rb +30 -0
  31. data/lib/euston-daemons/rake_task.rb +116 -0
  32. data/lib/euston-daemons/version.rb +5 -0
  33. data/lib/euston-daemons.rb +17 -0
  34. metadata +201 -0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+ gemspec
3
+
4
+ gem 'mongoid', :git => 'https://github.com/guyboertje/mongoid.git'
data/Gemfile.lock ADDED
@@ -0,0 +1,96 @@
1
+ GIT
2
+ remote: https://github.com/guyboertje/mongoid.git
3
+ revision: 3e5d48a14baf274998202dda9e6540cd4aeb7fe6
4
+ specs:
5
+ mongoid (2.1.0)
6
+ activemodel (~> 3.0)
7
+ jmongo
8
+ mongo (~> 1.3)
9
+ tzinfo (~> 0.3.22)
10
+
11
+ PATH
12
+ remote: .
13
+ specs:
14
+ euston-daemons (1.0.1)
15
+ activemodel (~> 3.0.0)
16
+ activesupport (~> 3.0.0)
17
+ erb-yaml (~> 1.0.0)
18
+ euston (~> 1.0.0)
19
+ euston-eventstore (~> 1.0.0)
20
+ hollywood (~> 1.0.0)
21
+ i18n (~> 0.5.0)
22
+ mongoid-glue (~> 1.0.0)
23
+ require_all (~> 1.2.0)
24
+ safely (~> 0.3.0)
25
+
26
+ GEM
27
+ remote: http://rubygems.org/
28
+ specs:
29
+ activemodel (3.0.10)
30
+ activesupport (= 3.0.10)
31
+ builder (~> 2.1.2)
32
+ i18n (~> 0.5.0)
33
+ activesupport (3.0.10)
34
+ bson (1.3.1)
35
+ bson (1.3.1-java)
36
+ bson_ext (1.3.1)
37
+ builder (2.1.2)
38
+ diff-lcs (1.1.3)
39
+ erb-yaml (1.0.0)
40
+ hash-keys (>= 1.0.0, < 2.0.0)
41
+ euston (1.0.0)
42
+ activesupport (~> 3.0.9)
43
+ euston-eventstore (~> 1.0.0)
44
+ require_all (~> 1.2.0)
45
+ euston-eventstore (1.0.4)
46
+ activesupport (~> 3.0.9)
47
+ bson_ext (~> 1.3.1)
48
+ hash-keys (~> 1.0.0)
49
+ json (~> 1.5.0)
50
+ mongo (~> 1.3.1)
51
+ require_all (~> 1.2.0)
52
+ uuid (~> 2.3.0)
53
+ euston-eventstore (1.0.4-java)
54
+ activesupport (~> 3.0.9)
55
+ hash-keys (~> 1.0.0)
56
+ jmongo (~> 1.0.0)
57
+ json-jruby (~> 1.5.0)
58
+ require_all (~> 1.2.0)
59
+ uuid (~> 2.3.0)
60
+ hash-keys (1.0.0)
61
+ hollywood (1.0.0)
62
+ i18n (0.5.0)
63
+ jmongo (1.0.3)
64
+ require_all (~> 1.2)
65
+ json (1.5.0)
66
+ json (1.5.0-java)
67
+ json-jruby (1.5.0-java)
68
+ json (= 1.5.0)
69
+ macaddr (1.4.0)
70
+ systemu (~> 2.2.0)
71
+ mongo (1.3.1)
72
+ bson (>= 1.3.1)
73
+ mongoid-glue (1.0.0)
74
+ require_all (1.2.0)
75
+ rspec (2.6.0)
76
+ rspec-core (~> 2.6.0)
77
+ rspec-expectations (~> 2.6.0)
78
+ rspec-mocks (~> 2.6.0)
79
+ rspec-core (2.6.4)
80
+ rspec-expectations (2.6.0)
81
+ diff-lcs (~> 1.1.2)
82
+ rspec-mocks (2.6.0)
83
+ safely (0.3.0)
84
+ systemu (2.2.0)
85
+ tzinfo (0.3.29)
86
+ uuid (2.3.4)
87
+ macaddr (~> 1.0)
88
+
89
+ PLATFORMS
90
+ java
91
+ ruby
92
+
93
+ DEPENDENCIES
94
+ euston-daemons!
95
+ mongoid!
96
+ rspec (~> 2.6.0)
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2011 by Lee Henson, Guy Boertje
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,161 @@
1
+ require 'date'
2
+ require 'rspec/core/rake_task'
3
+
4
+ #############################################################################
5
+ #
6
+ # Helper functions
7
+ #
8
+ #############################################################################
9
+
10
+ def name
11
+ @name ||= Dir['*.gemspec'].first.split('.').first
12
+ end
13
+
14
+ def version
15
+ line = File.read("lib/#{name}/version.rb")[/^\s*VERSION\s*=\s*.*/]
16
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
17
+ end
18
+
19
+ def date
20
+ Date.today.to_s
21
+ end
22
+
23
+ def rubyforge_project
24
+ name
25
+ end
26
+
27
+ def gemspec_file
28
+ "#{name}.gemspec"
29
+ end
30
+
31
+ def gem_file
32
+ "#{name}-#{version}.gem"
33
+ end
34
+
35
+ def replace_header(head, header_name, provider = nil)
36
+ if provider
37
+ value = send(provider)
38
+ else
39
+ value = "'#{send(header_name)}'"
40
+ end
41
+
42
+ provider ||= header_name
43
+ head.sub!(/(\.#{header_name}\s*= ).*/) { "#{$1}#{value}"}
44
+ end
45
+
46
+ def platform
47
+ jruby? ? '-java' : ''
48
+ end
49
+
50
+ def platform_dependant_gem_file
51
+ "#{name}-#{version}#{platform}.gem"
52
+ end
53
+
54
+ def platform_dependent_version
55
+ "'#{version}#{platform}'"
56
+ end
57
+
58
+ def jruby?
59
+ RUBY_PLATFORM.to_s == 'java'
60
+ end
61
+
62
+ def trim_array_ends array
63
+ array.shift
64
+ array.pop
65
+ array
66
+ end
67
+
68
+ #############################################################################
69
+ #
70
+ # Custom tasks
71
+ #
72
+ #############################################################################
73
+
74
+ default_rspec_opts = %w[--colour --format Fuubar]
75
+
76
+ desc "Run all examples"
77
+ RSpec::Core::RakeTask.new(:spec) do |t|
78
+ t.rspec_opts = default_rspec_opts
79
+ end
80
+
81
+ #############################################################################
82
+ #
83
+ # Packaging tasks
84
+ #
85
+ #############################################################################
86
+
87
+ def built_gem
88
+ @built_gem ||= Dir["#{name}*.gem"].first
89
+ end
90
+
91
+ desc "Create tag v#{platform_dependent_version} and build and push #{platform_dependant_gem_file} to Rubygems"
92
+ task :release => :build do
93
+ unless `git branch` =~ /^\* master$/
94
+ puts "You must be on the master branch to release!"
95
+ exit!
96
+ end
97
+
98
+ sh "git commit --allow-empty -a -m 'Release #{platform_dependent_version}'"
99
+ sh "git tag v#{platform_dependent_version}"
100
+ sh "git push origin master"
101
+ sh "git push origin v#{platform_dependent_version}"
102
+
103
+ command = "gem push pkg/#{platform_dependant_gem_file}"
104
+
105
+ if jruby?
106
+ puts "--------------------------------------------------------------------------------------"
107
+ puts "can't push to rubygems using jruby at the moment, so switch to mri and run: #{command}"
108
+ puts "--------------------------------------------------------------------------------------"
109
+ else
110
+ sh command
111
+ end
112
+ end
113
+
114
+ desc "Build #{platform_dependant_gem_file} into the pkg directory"
115
+ task :build => :gemspec do
116
+ sh "mkdir -p pkg"
117
+ sh "gem build #{gemspec_file}"
118
+ sh "mv #{built_gem} pkg"
119
+ end
120
+
121
+ desc "Generate #{gemspec_file}"
122
+ task :gemspec => :validate do
123
+ # read spec file and split out manifest section
124
+ spec = File.read(gemspec_file)
125
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
126
+
127
+ # replace name version and date
128
+ replace_header(head, :name)
129
+ replace_header(head, :version)
130
+ replace_header(head, :date)
131
+ #comment this out if your rubyforge_project has a different name
132
+ #replace_header(head, :rubyforge_project)
133
+
134
+ # determine file list from git ls-files
135
+ files = `git ls-files`.
136
+ split("\n").
137
+ sort.
138
+ reject { |file| file =~ /^\./ }.
139
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
140
+ map { |file| " #{file}" }.
141
+ join("\n")
142
+
143
+ # piece file back together and write
144
+ manifest = " s.files = %w[\n#{files}\n ]\n"
145
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
146
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
147
+ puts "Updated #{gemspec_file}"
148
+ end
149
+
150
+ desc "Validate #{gemspec_file}"
151
+ task :validate do
152
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
153
+ unless libfiles.empty?
154
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
155
+ exit!
156
+ end
157
+ unless Dir['VERSION*'].empty?
158
+ puts "A `VERSION` file at root level violates Gem best practices."
159
+ exit!
160
+ end
161
+ end
@@ -0,0 +1,72 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'euston-daemons'
3
+ s.version = '1.0.1'
4
+ s.platform = RUBY_PLATFORM.to_s == 'java' ? 'java' : Gem::Platform::RUBY
5
+ s.authors = ['Lee Henson', 'Guy Boertje']
6
+ s.email = ['lee.m.henson@gmail.com', 'guyboertje@gmail.com']
7
+ s.homepage = "http://github.com/leemhenson/euston-daemons"
8
+ s.summary = %q{Message buffer, command processor and event processor daemons for Euston-RabbitMq}
9
+ s.description = %q{}
10
+
11
+ # = MANIFEST =
12
+ s.files = %w[
13
+ Gemfile
14
+ Gemfile.lock
15
+ LICENSE
16
+ Rakefile
17
+ euston-daemons.gemspec
18
+ lib/euston-daemons.rb
19
+ lib/euston-daemons/command_processor_daemon/config/environment.rb
20
+ lib/euston-daemons/command_processor_daemon/lib/components/command_handler_component.rb
21
+ lib/euston-daemons/command_processor_daemon/lib/daemon.rb
22
+ lib/euston-daemons/command_processor_daemon/lib/settings.rb
23
+ lib/euston-daemons/command_processor_daemon/rake_task.rb
24
+ lib/euston-daemons/event_processor_daemon/config/environment.rb
25
+ lib/euston-daemons/event_processor_daemon/lib/components/event_handler_component.rb
26
+ lib/euston-daemons/event_processor_daemon/lib/daemon.rb
27
+ lib/euston-daemons/event_processor_daemon/lib/settings.rb
28
+ lib/euston-daemons/event_processor_daemon/rake_task.rb
29
+ lib/euston-daemons/framework/basic_component.rb
30
+ lib/euston-daemons/framework/component_shutdown.rb
31
+ lib/euston-daemons/framework/daemon.rb
32
+ lib/euston-daemons/framework/queue.rb
33
+ lib/euston-daemons/message_buffer_daemon/config/environment.rb
34
+ lib/euston-daemons/message_buffer_daemon/lib/components/buffer_component.rb
35
+ lib/euston-daemons/message_buffer_daemon/lib/components/event_store_component.rb
36
+ lib/euston-daemons/message_buffer_daemon/lib/daemon.rb
37
+ lib/euston-daemons/message_buffer_daemon/lib/message_logger.rb
38
+ lib/euston-daemons/message_buffer_daemon/lib/publisher.rb
39
+ lib/euston-daemons/message_buffer_daemon/lib/read_model/message_buffer.rb
40
+ lib/euston-daemons/message_buffer_daemon/lib/read_model/message_log.rb
41
+ lib/euston-daemons/message_buffer_daemon/lib/settings.rb
42
+ lib/euston-daemons/message_buffer_daemon/lib/subscriber.rb
43
+ lib/euston-daemons/message_buffer_daemon/rake_task.rb
44
+ lib/euston-daemons/rake_task.rb
45
+ lib/euston-daemons/version.rb
46
+ ]
47
+ # = MANIFEST =
48
+
49
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
50
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
51
+
52
+ s.add_dependency 'activemodel', '~> 3.0.0'
53
+ s.add_dependency 'activesupport', '~> 3.0.0'
54
+ s.add_dependency 'erb-yaml', '~> 1.0.0'
55
+ s.add_dependency 'hollywood', '~> 1.0.0'
56
+ s.add_dependency 'i18n', '~> 0.5.0'
57
+ s.add_dependency 'require_all', '~> 1.2.0'
58
+ s.add_dependency 'safely', '~> 0.3.0'
59
+
60
+ if RUBY_PLATFORM.to_s == 'java'
61
+ s.add_dependency 'euston-rabbitmq', '~> 1.0.0'
62
+ s.add_dependency 'jessica', '~> 1.0.0'
63
+ s.add_dependency 'jmongo', '~> 1.0.0'
64
+ end
65
+
66
+ s.add_dependency 'euston', '~> 1.0.0'
67
+ s.add_dependency 'euston-eventstore', '~> 1.0.0'
68
+ s.add_dependency 'mongoid-glue', '~> 1.0.0'
69
+ # s.add_dependency 'mongoid'
70
+
71
+ s.add_development_dependency 'rspec', '~> 2.6.0'
72
+ end
@@ -0,0 +1,17 @@
1
+ require 'jessica'
2
+ require 'euston'
3
+ require 'euston-eventstore'
4
+ require 'euston-rabbitmq'
5
+ require_rel '../lib'
6
+
7
+ Safely::Strategy::Log.logger = EUSTON_LOG
8
+ AMQP.settings.merge! ErbYaml.read(AMQP_CONFIG_PATH, EUSTON_ENV)
9
+ Euston::CommandProcessorDaemon::Settings.configure ErbYaml.read(DAEMON_CONFIG_PATH, EUSTON_ENV)
10
+
11
+ Euston::EventStore::Persistence::Mongodb::Config.instance.logger = EUSTON_LOG
12
+ Euston::EventStore::Persistence::Mongodb::Config.instance.database = Euston::CommandProcessorDaemon::Settings.mongo_db_name
13
+
14
+ Euston::RabbitMq.event_store = Euston::EventStore::Persistence::Mongodb::MongoPersistenceFactory.build
15
+ Euston::RabbitMq.event_store.init
16
+
17
+ Euston::Repository.event_store = Euston::EventStore::OptimisticEventStore.new Euston::RabbitMq.event_store
@@ -0,0 +1,55 @@
1
+ module Euston
2
+ module CommandProcessorDaemon
3
+ class CommandHandlerComponent
4
+ attr_reader :rmq_client, :cli_thread
5
+
6
+ def initialize
7
+ @rmq_client = AMQP::Channel.new
8
+ end
9
+
10
+ def start
11
+ @cli_thread = Thread.new do
12
+ begin
13
+ command_handler_bindings = Euston::RabbitMq::CommandHandlerBindings.new(@rmq_client)
14
+ command_handler_bindings.add_namespace Euston::RabbitMq::CommandHandlers
15
+
16
+ if Object.const_defined? 'COMMAND_HANDLER_NAMESPACES'
17
+ COMMAND_HANDLER_NAMESPACES.each do |handler_namespace|
18
+ ns = Object
19
+ found = true
20
+
21
+ handler_namespace.split('::').each do |c|
22
+ if found && ns.const_defined?(c)
23
+ ns = ns.const_get c.to_sym
24
+ elsif found
25
+ EUSTON_LOG.warn "Couldn't find requested command handler namespace: #{handler_namespace}"
26
+ found = false
27
+ end
28
+ end
29
+
30
+ if found
31
+ EUSTON_LOG.debug "Successfully found command handler namespace: #{handler_namespace}"
32
+ command_handler_bindings.add_namespace ns
33
+ end
34
+ end
35
+ end
36
+
37
+ command_handler_bindings.finalize_bindings
38
+ rescue => e
39
+ Thread.current[:exception] = e
40
+ Safely.report! e
41
+ end
42
+ end
43
+ end
44
+
45
+ def thread_state
46
+ @cli_thread.status
47
+ end
48
+
49
+ def stop
50
+ @cli_thread[:stop] = true
51
+ @rmq_client.disconnect
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,43 @@
1
+ module Euston
2
+ module CommandProcessorDaemon
3
+ class Daemon
4
+ include Euston::Daemon
5
+
6
+ attr_reader :clients,:queue #,:ctx
7
+
8
+ def initialize #(ctx)
9
+ @queue = Queue.new
10
+ # @ctx = ctx
11
+ @clients = {}
12
+
13
+ Settings.client_instances.times do |i|
14
+ @clients["command_component_#{i.succ}"] = CommandHandlerComponent.new
15
+ end
16
+ end
17
+
18
+ def run
19
+ @clients.each do |name, component|
20
+ sleep 0.25
21
+ EUSTON_LOG.debug "Starting component: #{name}"
22
+ component.start
23
+ end
24
+
25
+ EUSTON_LOG.debug "Components started"
26
+
27
+ @clients.each do |name, component|
28
+ EUSTON_LOG.debug("Thread state of #{name}: #{component.thread_state.inspect}")
29
+ end
30
+
31
+ @queue.pop #<-------- stops here until interrupted
32
+
33
+ @clients.each do |name, component|
34
+ EUSTON_LOG.debug "Stopping component: #{name}"
35
+ component.stop
36
+ end
37
+
38
+ report_shutdown_reasons
39
+ EUSTON_LOG.debug "Components stopped"
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ module Euston
2
+ module CommandProcessorDaemon
3
+ module Settings
4
+ def self.configure(cfg=nil)
5
+ @config ||= {}
6
+ @config.merge!(cfg) if cfg && cfg.is_a?(Hash)
7
+ end
8
+ def self.client_instances
9
+ @config[:client_instances] || 1
10
+ end
11
+ def self.mongo_db_name
12
+ @config[:mongo_db_name]
13
+ end
14
+ def self.debug
15
+ @config[:debug]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,31 @@
1
+ module Euston
2
+ class CommandProcessorRakeTask < Euston::Daemons::RakeTask
3
+ attr_accessor :amqp_config_path, :command_handler_namespaces, :daemon_config_path
4
+
5
+ def initialize
6
+ @command_handler_namespaces = []
7
+ super(:command_processor_daemon)
8
+ end
9
+
10
+ def before_creating_task
11
+ @daemon_path = File.expand_path(File.dirname __FILE__) + File::SEPARATOR
12
+ @daemon_class = 'Euston::CommandProcessorDaemon::Daemon'
13
+ end
14
+
15
+ def initialize_paths
16
+ EUSTON_LOG.debug "AMQP config path: #{@amqp_config_path}"
17
+ Object.const_set :AMQP_CONFIG_PATH, @amqp_config_path
18
+
19
+ EUSTON_LOG.debug "Daemon config path: #{@daemon_config_path}"
20
+ Object.const_set :DAEMON_CONFIG_PATH, @daemon_config_path
21
+
22
+ EUSTON_LOG.debug "Command handler namespaces: #{@command_handler_namespaces}"
23
+ Object.const_set :COMMAND_HANDLER_NAMESPACES, @command_handler_namespaces
24
+ end
25
+
26
+ def load_environment
27
+ EUSTON_LOG.debug "Loading environment"
28
+ require_rel 'config/environment.rb'
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ require 'jessica'
2
+ require 'euston'
3
+ require 'euston-eventstore'
4
+ require 'euston-rabbitmq'
5
+
6
+ require_rel '../lib'
7
+
8
+ Safely::Strategy::Log.logger = EUSTON_LOG
9
+ AMQP.settings.merge! ErbYaml.read(AMQP_CONFIG_PATH, EUSTON_ENV)
10
+ Euston::EventProcessorDaemon::Settings.configure ErbYaml.read(DAEMON_CONFIG_PATH, EUSTON_ENV)
11
+
12
+ hash = ErbYaml.read(MONGOID_CONFIG_PATH, EUSTON_ENV)
13
+
14
+ read_connection = Mongo::Connection.new hash[:host], hash[:port], :safe => hash[:safe] #, :logger => EUSTON_LOG
15
+
16
+ Mongoid.configure do |config|
17
+ config.master = Mongo::DB.new(hash[:read_model_database], read_connection)
18
+ config.logger = EUSTON_LOG
19
+ end
20
+
21
+ Euston::RabbitMq.read_model_mongodb = Mongo::DB.new(hash[:read_model_database], read_connection)
22
+
23
+ I18n.load_path += Dir[I18N_LOCALES_PATH]
@@ -0,0 +1,72 @@
1
+ module Euston
2
+ module EventProcessorDaemon
3
+ class EventHandlerComponent
4
+ include Euston::Daemons::ComponentShutdown
5
+
6
+ attr_reader :channel, :cli_threads
7
+
8
+ def initialize()
9
+ @channel = AMQP::Channel.new
10
+ @channel.prefetch(1)
11
+ @event_handler_bindings = Euston::RabbitMq::EventHandlerBindings.new(@channel)
12
+
13
+ if Object.const_defined? 'EVENT_HANDLER_NAMESPACES'
14
+ EVENT_HANDLER_NAMESPACES.each do |handler_namespace|
15
+ ns = Object
16
+ found = true
17
+
18
+ handler_namespace.split('::').each do |c|
19
+ if found && ns.const_defined?(c)
20
+ ns = ns.const_get c.to_sym
21
+ elsif found
22
+ EUSTON_LOG.warn "Couldn't find requested event handler namespace: #{handler_namespace}"
23
+ found = false
24
+ end
25
+ end
26
+
27
+ if found
28
+ EUSTON_LOG.debug "Successfully found event handler namespace: #{handler_namespace}"
29
+ @event_handler_bindings.add_namespace ns
30
+ end
31
+ end
32
+ end
33
+
34
+ @cli_threads = {}
35
+ end
36
+
37
+ def start
38
+ @event_handler_bindings.namespaced_handler_types.each do |namespace, handler_type|
39
+
40
+ @cli_threads["#{namespace}.#{handler_type}"] = Thread.new(namespace, handler_type) do |ns, ht|
41
+ begin
42
+ chan = AMQP::Channel.new
43
+ chan.prefetch(1)
44
+ EUSTON_LOG.debug "EventHandlerBinding for: #{ns}.#{ht}"
45
+ binding = Euston::RabbitMq::EventHandlerBinding.new(chan, ns, ht)
46
+ binding.bind
47
+ binding.listen
48
+ rescue => e
49
+ Thread.current[:exception] = e
50
+ ensure
51
+ chan.disconnect
52
+ end
53
+ check_exception_and_shutdown
54
+ end
55
+ end
56
+ end
57
+
58
+ def thread_state
59
+ out = {}
60
+ @cli_threads.each do |k, th|
61
+ out[k] = th.status
62
+ end
63
+ out.inspect
64
+ end
65
+
66
+ def stop
67
+ @cli_threads.values.each { |th| th[:stop] = true }
68
+ @channel.disconnect
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,72 @@
1
+ module Euston
2
+ module EventProcessorDaemon
3
+ class Daemon
4
+ include Euston::Daemon
5
+
6
+ #attr_reader :ctx,:servers,:bus,:queue,:event_handler
7
+ attr_reader :queue, :event_handler
8
+
9
+ def initialize() #ctx)
10
+ # @servers = []
11
+ # @ctx = ctx
12
+ # @queue = Queue.new
13
+
14
+ # bus_client_port = ReadDaemonSettings.bus_port_base
15
+ # bus_server_port = bus_client_port + 10
16
+
17
+ # BusComponentSettings.client_side_bind_address("tcp://*:#{bus_client_port}")
18
+ # BusComponentSettings.server_side_bind_address("tcp://*:#{bus_server_port}")
19
+ # ReadModelComponentSettings.bus_server_side_address("tcp://localhost:#{bus_server_port}")
20
+
21
+ # @bus = BusComponent.new(@ctx)
22
+
23
+ # ReadDaemonSettings.server_instances.times do
24
+ # @servers << ReadModelComponent.new(@ctx)
25
+ # end
26
+
27
+ @queue = Queue.new
28
+ @event_handler = EventHandlerComponent.new
29
+ end
30
+
31
+ def run
32
+ # @bus.daemon = self
33
+ # @bus.start
34
+
35
+ # @servers.each do |ele|
36
+ # sleep(0.5)
37
+ # ele.daemon = self
38
+ # ele.start
39
+ # end
40
+ @event_handler.daemon = self
41
+
42
+ name = 'event_handler'
43
+ EUSTON_LOG.debug "Starting component: #{name}"
44
+ @event_handler.start
45
+
46
+ EUSTON_LOG.debug "Components started"
47
+ EUSTON_LOG.debug "Thread state of #{name}: #{@event_handler.thread_state.inspect}"
48
+
49
+ # EUSTON_LOG.debug @bus.thread_state.inspect
50
+
51
+ # @servers.each do |svr|
52
+ # EUSTON_LOG.debug "Server thread status: #{svr.thread_state}"
53
+ # end
54
+
55
+ @queue.pop #<-------- stops here until interrupted
56
+
57
+ #exiting
58
+ # @servers.each do |ele|
59
+ # ele.stop
60
+ # end
61
+ # sleep(0.6)
62
+ # @bus.stop
63
+
64
+ EUSTON_LOG.debug "Stopping component: #{name}"
65
+ @event_handler.stop
66
+
67
+ report_shutdown_reasons
68
+ EUSTON_LOG.debug "Components stopped"
69
+ end
70
+ end
71
+ end
72
+ end