modern_times 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
@@ -0,0 +1,8 @@
1
+ require 'base_worker'
2
+
3
+ class BarWorker < BaseWorker
4
+
5
+ def perform
6
+ raise "Raising my exception #{self.class.name}"
7
+ end
8
+ end
@@ -0,0 +1,23 @@
1
+ class BaseWorker
2
+ include ModernTimes::Base::Worker
3
+
4
+ def start
5
+ while true do
6
+ sleep 1
7
+ perform
8
+ end
9
+ rescue Exception => e
10
+ puts "Caught #{e.inspect}"
11
+ end
12
+
13
+ def perform
14
+ raise "Need to override perform method in #{self.class.name}"
15
+ end
16
+
17
+ def stop
18
+ end
19
+
20
+ def status
21
+ "Need to override status method in #{self.class.name}"
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ # Allow examples to be run in-place without requiring a gem install
2
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib'
3
+
4
+ require 'rubygems'
5
+ require 'modern_times'
6
+ require 'bar_worker'
7
+
8
+ manager = ModernTimes::Manager.new
9
+ manager.stop_on_signal
10
+ manager.add(BarWorker, 2, {})
11
+ manager.join
@@ -8,6 +8,9 @@ class ReverseEchoWorker
8
8
  sleep $1.to_f
9
9
  puts "#{self}: Finished sleeping at #{Time.now}"
10
10
  end
11
+ if obj =~ /^Exception/
12
+ raise Exception, 'You requested an exception'
13
+ end
11
14
  obj.reverse
12
15
  end
13
16
  end
data/lib/modern_times.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'rubygems'
2
- require 'modern_times/exception'
2
+ require 'modern_times/remote_exception'
3
3
  require 'modern_times/marshal_strategy'
4
4
  require 'modern_times/base'
5
5
  require 'modern_times/jms'
@@ -26,7 +26,7 @@ module ModernTimes
26
26
  def setup
27
27
  end
28
28
 
29
- def start(name)
29
+ def start
30
30
  raise "Need to override start method in #{self.class.name}"
31
31
  end
32
32
 
@@ -19,5 +19,17 @@ module ModernTimes
19
19
  return false
20
20
  end
21
21
  end
22
+
23
+ def self.create_message(session, marshaler, object)
24
+ case marshaler.marshal_type
25
+ when :text
26
+ session.create_text_message(marshaler.marshal(object))
27
+ when :bytes
28
+ msg = session.create_bytes_message()
29
+ msg.data = marshaler.marshal(object)
30
+ msg
31
+ else raise "Invalid marshal type: #{marshaler.marshal_type}"
32
+ end
33
+ end
22
34
  end
23
35
  end
@@ -38,15 +38,7 @@ module ModernTimes
38
38
  def publish(object, props={})
39
39
  message = nil
40
40
  Connection.session_pool.producer(@real_producer_options) do |session, producer|
41
- message = case @marshaler.marshal_type
42
- when :text
43
- session.create_text_message(@marshaler.marshal(object))
44
- when :bytes
45
- msg = session.create_bytes_message()
46
- msg.data = @marshaler.marshal(object)
47
- msg
48
- else raise "Invalid marshal type: #{@marshaler.marshal_type}"
49
- end
41
+ message = ModernTimes::JMS.create_message(session, @marshaler, object)
50
42
  message.jms_delivery_mode_sym = @persistent
51
43
  props.each do |key, value|
52
44
  message.send("#{key}=", value)
@@ -1,4 +1,5 @@
1
1
  require 'timeout'
2
+ require 'yaml'
2
3
 
3
4
  module ModernTimes
4
5
  module JMSRequestor
@@ -26,6 +27,9 @@ module ModernTimes
26
27
  end
27
28
  end
28
29
  raise Timeout::Error, "Timeout waiting for for response from message #{@message.jms_message_id} on queue #{@reply_queue}" unless response
30
+ if error_yaml = response['Exception']
31
+ raise ModernTimes::RemoteException.from_hash(YAML.load(error_yaml))
32
+ end
29
33
  return @requestor.marshaler.unmarshal(response.data)
30
34
  end
31
35
  end
@@ -4,6 +4,7 @@ module ModernTimes
4
4
  # Base Worker Class for any class that will be processing messages from queues
5
5
  module Worker
6
6
  include ModernTimes::JMS::Worker
7
+ attr_reader :error_count
7
8
 
8
9
  module ClassMethods
9
10
  def create_supervisor(manager, worker_options)
@@ -20,11 +21,12 @@ module ModernTimes
20
21
 
21
22
  def initialize(opts={})
22
23
  super
23
- @time_mutex = Mutex.new
24
- @count = 0
25
- @min_time = nil
26
- @max_time = 0.0
27
- @total_time = 0.0
24
+ @time_mutex = Mutex.new
25
+ @count = 0
26
+ @error_count = 0
27
+ @min_time = nil
28
+ @max_time = 0.0
29
+ @total_time = 0.0
28
30
  end
29
31
 
30
32
  def perform(object)
@@ -32,9 +34,8 @@ module ModernTimes
32
34
  response = request(object)
33
35
  response_time = Time.now - start_time
34
36
  session.producer(:destination => message.reply_to) do |producer|
35
- reply_message = session.message(self.class.marshaler.marshal(response))
37
+ reply_message = ModernTimes::JMS.create_message(session, self.class.marshaler, response)
36
38
  reply_message.jms_correlation_id = message.jms_message_id
37
- #producer.send_with_retry(reply_message)
38
39
  producer.send(reply_message)
39
40
  end
40
41
  @time_mutex.synchronize do
@@ -43,6 +44,20 @@ module ModernTimes
43
44
  @min_time = response_time if !@min_time || response_time < @min_time
44
45
  @max_time = response_time if response_time > @max_time
45
46
  end
47
+ rescue Exception => e
48
+ @time_mutex.synchronize do
49
+ @error_count += 1
50
+ end
51
+ begin
52
+ session.producer(:destination => message.reply_to) do |producer|
53
+ reply_message = ModernTimes::JMS.create_message(session, ModernTimes::MarshalStrategy::String, "Exception: #{e.message}")
54
+ reply_message.jms_correlation_id = message.jms_message_id
55
+ reply_message['Exception'] = ModernTimes::RemoteException.new(e).to_hash.to_yaml
56
+ producer.send(reply_message)
57
+ end
58
+ rescue Exception => e
59
+ ModernTimes.logger.error("Exception in exception reply: #{e.message}\n\t#{e.backtrace.join("\n\t")}")
60
+ end
46
61
  end
47
62
 
48
63
  def total_time
@@ -24,14 +24,14 @@ module ModernTimes
24
24
  begin
25
25
  worker_klass = Object.const_get(worker_klass.to_s)
26
26
  rescue
27
- raise ModernTimes::Exception, "Invalid class: #{worker_klass}"
27
+ raise "Invalid class: #{worker_klass}"
28
28
  end
29
29
  end
30
30
  if @allowed_workers && !@allowed_workers.include?(worker_klass)
31
- raise ModernTimes::Exception, "Error: #{worker_klass.name} is not an allowed worker"
31
+ raise "Error: #{worker_klass.name} is not an allowed worker"
32
32
  end
33
33
  supervisor = worker_klass.create_supervisor(self, worker_options)
34
- raise ModernTimes::Exception "A supervisor with name #{supervisor.name} already exists" if find_supervisor(supervisor.name)
34
+ raise "A supervisor with name #{supervisor.name} already exists" if find_supervisor(supervisor.name)
35
35
  mbean = supervisor.create_mbean(@domain)
36
36
  @supervisors << supervisor
37
37
  supervisor.worker_count = num_workers
@@ -2,6 +2,7 @@ require 'modern_times/marshal_strategy/bson'
2
2
  require 'modern_times/marshal_strategy/json'
3
3
  require 'modern_times/marshal_strategy/ruby'
4
4
  require 'modern_times/marshal_strategy/string'
5
+ require 'modern_times/marshal_strategy/yaml'
5
6
 
6
7
  # Defines some default marshaling strategies for use in marshaling/unmarshaling objects
7
8
  # written and read via jms. Implementing classes must define the following methods:
@@ -35,6 +36,7 @@ module ModernTimes
35
36
  when :string then String
36
37
  when :json then JSON
37
38
  when :bson then BSON
39
+ when :yaml then YAML
38
40
  else raise "Invalid marshal strategy: #{options[:marshal]}"
39
41
  end
40
42
  elsif marshal_option.respond_to?(:marshal_type)
@@ -0,0 +1,19 @@
1
+ module ModernTimes
2
+ module MarshalStrategy
3
+ module YAML
4
+ extend self
5
+
6
+ def marshal_type
7
+ :text
8
+ end
9
+
10
+ def marshal(object)
11
+ object.to_yaml
12
+ end
13
+
14
+ def unmarshal(msg)
15
+ ::YAML.load(msg)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -45,7 +45,7 @@ module ModernTimes
45
45
  Rails.logger.info "Messaging disabled"
46
46
  @is_jms_enabled = false
47
47
  ModernTimes::JMS::Publisher.setup_dummy_publishing(rails_workers)
48
- ModernTimes::JMSRequestor::Requestor.setup_dummy_publishing(rails_workers)
48
+ ModernTimes::JMSRequestor::Requestor.setup_dummy_requesting(rails_workers)
49
49
  end
50
50
  end
51
51
 
@@ -0,0 +1,42 @@
1
+ module ModernTimes
2
+ class RemoteException < Exception
3
+ attr_accessor :originating_exception_name
4
+ attr_accessor :originating_exception_message
5
+
6
+ def initialize(originating_exception=nil, message=nil)
7
+ super(message)
8
+ if originating_exception
9
+ @originating_exception_name = originating_exception.class.name
10
+ @originating_exception_message = originating_exception.message
11
+ set_backtrace(originating_exception.backtrace)
12
+ end
13
+ @message = message
14
+ end
15
+
16
+ def message
17
+ @message || @originating_exception_message
18
+ end
19
+
20
+ def message=(msg)
21
+ @message = msg
22
+ end
23
+
24
+ def to_hash
25
+ {
26
+ 'message' => @message,
27
+ 'originating_exception_name' => @originating_exception_name,
28
+ 'originating_exception_message' => @originating_exception_message,
29
+ 'backtrace' => backtrace
30
+ }
31
+ end
32
+
33
+ def self.from_hash(hash)
34
+ exc = new
35
+ exc.message = hash['message']
36
+ exc.originating_exception_name = hash['originating_exception_name']
37
+ exc.originating_exception_message = hash['originating_exception_message']
38
+ exc.set_backtrace(hash['backtrace'])
39
+ return exc
40
+ end
41
+ end
42
+ end
data/test/base_test.rb CHANGED
@@ -130,12 +130,12 @@ class BaseTest < Test::Unit::TestCase
130
130
  end
131
131
 
132
132
  should "not be allowed" do
133
- e = assert_raises ModernTimes::Exception do
133
+ e = assert_raises RuntimeError do
134
134
  @manager.add(DummyWorker, 2, {:foo => 42})
135
135
  end
136
136
  assert_match %r%is not an allowed worker%, e.message
137
137
 
138
- e = assert_raises ModernTimes::Exception do
138
+ e = assert_raises RuntimeError do
139
139
  @manager.add('FdajfsdklasdfWorker', 2, {:foo => 42})
140
140
  end
141
141
  assert_match %r%Invalid class%, e.message
data/test/jms_test.rb CHANGED
@@ -202,8 +202,8 @@ class JMSTest < Test::Unit::TestCase
202
202
  teardown do
203
203
  end
204
204
 
205
- #[BSONTest, JSONTest, RubyTest, StringTest].each do |marshal_module|
206
- [BSONTest, JSONTest, StringTest].each do |marshal_module|
205
+ [BSONTest, JSONTest, RubyTest, StringTest].each do |marshal_module|
206
+ #[BSONTest, JSONTest, StringTest].each do |marshal_module|
207
207
  marshal_module.name =~ /(.*)Test/
208
208
  marshal_type = $1
209
209
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 2
9
- version: 0.2.2
8
+ - 3
9
+ version: 0.2.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brad Pardee
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-22 00:00:00 -04:00
18
+ date: 2011-04-28 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -79,7 +79,6 @@ files:
79
79
  - lib/modern_times/base/supervisor.rb
80
80
  - lib/modern_times/base/supervisor_mbean.rb
81
81
  - lib/modern_times/base/worker.rb
82
- - lib/modern_times/exception.rb
83
82
  - lib/modern_times/jms.rb
84
83
  - lib/modern_times/jms/connection.rb
85
84
  - lib/modern_times/jms/publisher.rb
@@ -100,7 +99,9 @@ files:
100
99
  - lib/modern_times/marshal_strategy/json.rb
101
100
  - lib/modern_times/marshal_strategy/ruby.rb
102
101
  - lib/modern_times/marshal_strategy/string.rb
102
+ - lib/modern_times/marshal_strategy/yaml.rb
103
103
  - lib/modern_times/railsable.rb
104
+ - lib/modern_times/remote_exception.rb
104
105
  - test/base_test.rb
105
106
  - test/jms.yml
106
107
  - test/jms_requestor_test.rb
@@ -137,6 +138,9 @@ signing_key:
137
138
  specification_version: 3
138
139
  summary: Asynchronous task library
139
140
  test_files:
141
+ - examples/exception_test/bar_worker.rb
142
+ - examples/exception_test/base_worker.rb
143
+ - examples/exception_test/manager.rb
140
144
  - examples/requestor/manager.rb
141
145
  - examples/requestor/request.rb
142
146
  - examples/requestor/reverse_echo_worker.rb
@@ -1,4 +0,0 @@
1
- module ModernTimes
2
- class Exception < ::Exception
3
- end
4
- end