qwirk 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. data/README.md +9 -14
  2. data/lib/qwirk.rb +26 -17
  3. data/lib/qwirk/adapter.rb +3 -45
  4. data/lib/qwirk/adapter/base.rb +2 -0
  5. data/lib/qwirk/adapter/base/expanding_worker_config.rb +133 -0
  6. data/lib/qwirk/adapter/base/worker_config.rb +104 -0
  7. data/lib/qwirk/adapter/in_memory.rb +13 -0
  8. data/lib/qwirk/{queue_adapter/in_mem → adapter/in_memory}/factory.rb +2 -2
  9. data/lib/qwirk/{queue_adapter/in_mem → adapter/in_memory}/publisher.rb +4 -4
  10. data/lib/qwirk/{queue_adapter/in_mem → adapter/in_memory}/queue.rb +3 -2
  11. data/lib/qwirk/{queue_adapter/in_mem → adapter/in_memory}/reply_queue.rb +3 -2
  12. data/lib/qwirk/{queue_adapter/in_mem → adapter/in_memory}/topic.rb +2 -2
  13. data/lib/qwirk/{queue_adapter/in_mem → adapter/in_memory}/worker.rb +6 -8
  14. data/lib/qwirk/adapter/in_memory/worker_config.rb +50 -0
  15. data/lib/qwirk/adapter/inline.rb +9 -0
  16. data/lib/qwirk/adapter/inline/publisher.rb +86 -0
  17. data/lib/qwirk/adapter/inline/worker.rb +55 -0
  18. data/lib/qwirk/adapter/inline/worker_config.rb +30 -0
  19. data/lib/qwirk/adapter_factory.rb +48 -0
  20. data/lib/qwirk/base_worker.rb +18 -28
  21. data/lib/qwirk/batch/file_worker.rb +4 -4
  22. data/lib/qwirk/manager.rb +11 -8
  23. data/lib/qwirk/marshal_strategy/none.rb +1 -1
  24. data/lib/qwirk/publish_handle.rb +22 -11
  25. data/lib/qwirk/publisher.rb +9 -9
  26. data/lib/qwirk/{request_worker.rb → reply_worker.rb} +3 -3
  27. data/lib/qwirk/worker.rb +27 -29
  28. data/test/jms_fail_test.rb +11 -11
  29. data/test/jms_requestor_block_test.rb +12 -12
  30. data/test/jms_requestor_test.rb +8 -8
  31. data/test/jms_test.rb +10 -10
  32. metadata +104 -185
  33. data/examples/README +0 -1
  34. data/examples/activemq.xml +0 -84
  35. data/examples/advanced_requestor/README.md +0 -15
  36. data/examples/advanced_requestor/base_request_worker.rb +0 -18
  37. data/examples/advanced_requestor/char_count_worker.rb +0 -16
  38. data/examples/advanced_requestor/config.ru +0 -24
  39. data/examples/advanced_requestor/exception_raiser_worker.rb +0 -17
  40. data/examples/advanced_requestor/length_worker.rb +0 -14
  41. data/examples/advanced_requestor/print_worker.rb +0 -14
  42. data/examples/advanced_requestor/publisher.rb +0 -49
  43. data/examples/advanced_requestor/qwirk.yml +0 -16
  44. data/examples/advanced_requestor/reverse_worker.rb +0 -14
  45. data/examples/advanced_requestor/triple_worker.rb +0 -14
  46. data/examples/batch/my_batch_worker.rb +0 -30
  47. data/examples/batch/my_line_worker.rb +0 -8
  48. data/examples/qwirk.yml +0 -20
  49. data/examples/requestor/README.md +0 -13
  50. data/examples/requestor/config.ru +0 -13
  51. data/examples/requestor/qwirk_persist.yml +0 -5
  52. data/examples/requestor/requestor.rb +0 -68
  53. data/examples/requestor/reverse_echo_worker.rb +0 -15
  54. data/examples/setup.rb +0 -13
  55. data/examples/shared/README.md +0 -24
  56. data/examples/shared/config.ru +0 -13
  57. data/examples/shared/publisher.rb +0 -49
  58. data/examples/shared/qwirk_persist.yml +0 -5
  59. data/examples/shared/shared_worker.rb +0 -16
  60. data/examples/simple/README +0 -53
  61. data/examples/simple/bar_worker.rb +0 -10
  62. data/examples/simple/baz_worker.rb +0 -10
  63. data/examples/simple/config.ru +0 -14
  64. data/examples/simple/publisher.rb +0 -49
  65. data/examples/simple/qwirk_persist.yml +0 -4
  66. data/examples/simple/tmp/kahadb/db-1.log +0 -0
  67. data/examples/simple/tmp/kahadb/db.data +0 -0
  68. data/examples/simple/tmp/kahadb/db.redo +0 -0
  69. data/examples/task/README +0 -47
  70. data/examples/task/config.ru +0 -14
  71. data/examples/task/foo_worker.rb +0 -10
  72. data/examples/task/messages.out +0 -1000
  73. data/examples/task/publisher.rb +0 -25
  74. data/examples/task/qwirk_persist.yml +0 -5
  75. data/examples/task/task.rb +0 -36
  76. data/lib/qwirk/queue_adapter.rb +0 -3
  77. data/lib/qwirk/queue_adapter/active_mq.rb +0 -13
  78. data/lib/qwirk/queue_adapter/active_mq/publisher.rb +0 -12
  79. data/lib/qwirk/queue_adapter/active_mq/worker_config.rb +0 -16
  80. data/lib/qwirk/queue_adapter/in_mem.rb +0 -7
  81. data/lib/qwirk/queue_adapter/in_mem/worker_config.rb +0 -59
  82. data/lib/qwirk/queue_adapter/jms.rb +0 -50
  83. data/lib/qwirk/queue_adapter/jms/connection.rb +0 -42
  84. data/lib/qwirk/queue_adapter/jms/consumer.rb +0 -37
  85. data/lib/qwirk/queue_adapter/jms/publisher.rb +0 -126
  86. data/lib/qwirk/queue_adapter/jms/worker.rb +0 -89
  87. data/lib/qwirk/queue_adapter/jms/worker_config.rb +0 -38
  88. data/lib/qwirk/version.rb +0 -3
  89. data/lib/qwirk/worker_config.rb +0 -187
@@ -1,25 +0,0 @@
1
- require 'rumx'
2
- require 'qwirk'
3
-
4
- class Publisher
5
- include Rumx::Bean
6
-
7
- bean_attr_reader :tasks, :list, 'Tasks', :list_type => :bean
8
-
9
- bean_operation :perform_task, :void, 'Perform task which will send <count> messages to the worker and write the output to <output_file>', [
10
- [ :task_id, :string, 'Id for this task ', 'task1' ],
11
- [ :count, :integer, 'Count of messages', 1000 ],
12
- [ :message, :string, 'String portion of the message to send', 'M' ],
13
- [ :sleep_time, :float, 'Time to sleep between messages', 0.0 ],
14
- [ :output_file, :string, 'Output file to write returned messages to', 'messages.out' ]
15
- ]
16
-
17
- def initialize(adapter_key)
18
- @adapter_key = adapter_key
19
- @tasks = []
20
- end
21
-
22
- def perform_task(task_id, count, message, sleep_time, output_file)
23
- @tasks << Task.new(@adapter_key, task_id, count, message, sleep_time, output_file)
24
- end
25
- end
@@ -1,5 +0,0 @@
1
- ---
2
- Foo:
3
- :min_count: 2
4
- :max_count: 100
5
- :sleep_time: 0.1
@@ -1,36 +0,0 @@
1
- require 'rumx'
2
- require 'qwirk'
3
-
4
- class Task
5
- include Qwirk::Task
6
-
7
- def initialize(adapter_key, task_id, total_count, message, sleep_time, output_file)
8
- publisher = Qwirk[adapter_key].create_publisher(:queue_name => 'Foo', :marshal => :bson, :persistent => false, :response => {:time_to_live => 10000, :marshal => :string})
9
- super(publisher, task_id, total_count)
10
- @out = File.open(output_file, 'w')
11
- (1..total_count).each do |i|
12
- obj = {'count' => i, 'message' => message}
13
- #puts "Publishing object: #{obj.inspect}"
14
- publish(obj)
15
- sleep sleep_time
16
- end
17
- finished_publishing
18
- end
19
-
20
- def on_response(request, response)
21
- #puts "For request #{request}, got response #{response}"
22
- @out.puts response
23
- end
24
-
25
- def on_exception(request, exception)
26
- puts "For request #{request} got exception #{exception.message}"
27
- end
28
-
29
- def on_update()
30
- end
31
-
32
- def on_done
33
- @out.close
34
- puts "We're done"
35
- end
36
- end
@@ -1,3 +0,0 @@
1
- require 'qwirk/queue_adapter/in_mem'
2
- require 'qwirk/queue_adapter/jms'
3
- require 'qwirk/queue_adapter/active_mq'
@@ -1,13 +0,0 @@
1
- require 'activemq'
2
- require 'qwirk/queue_adapter/active_mq/publisher'
3
- require 'qwirk/queue_adapter/active_mq/worker_config'
4
-
5
- module Qwirk
6
- module QueueAdapter
7
- module ActiveMQ
8
- def self.init(config)
9
- JMS.init(config)
10
- end
11
- end
12
- end
13
- end
@@ -1,12 +0,0 @@
1
- module Qwirk
2
- module QueueAdapter
3
- module ActiveMQ
4
- class Publisher < JMS::Publisher
5
- def initialize(queue_adapter, queue_name, topic_name, options, response_options)
6
- topic_name = "VirtualTopic.#{topic_name}" if topic_name
7
- super(queue_adapter, queue_name, topic_name, options, response_options)
8
- end
9
- end
10
- end
11
- end
12
- end
@@ -1,16 +0,0 @@
1
- # Handle Messaging and Queuing using ActiveMQ
2
- module Qwirk
3
- module QueueAdapter
4
- module ActiveMQ
5
- class WorkerConfig < JMS::WorkerConfig
6
- def initialize(queue_adapter, parent, queue_name, topic_name, options, response_options)
7
- if topic_name
8
- queue_name = "Consumer.#{parent.name}.VirtualTopic.#{topic_name}"
9
- topic_name = nil
10
- end
11
- super(queue_adapter, parent, queue_name, topic_name, options, response_options)
12
- end
13
- end
14
- end
15
- end
16
- end
@@ -1,7 +0,0 @@
1
- require 'qwirk/queue_adapter/in_mem/factory'
2
- require 'qwirk/queue_adapter/in_mem/publisher'
3
- require 'qwirk/queue_adapter/in_mem/queue'
4
- require 'qwirk/queue_adapter/in_mem/reply_queue'
5
- require 'qwirk/queue_adapter/in_mem/topic'
6
- require 'qwirk/queue_adapter/in_mem/worker_config'
7
- require 'qwirk/queue_adapter/in_mem/worker'
@@ -1,59 +0,0 @@
1
- # Handle Messaging and Queuing using JMS
2
- module Qwirk
3
- module QueueAdapter
4
- module InMem
5
-
6
- # This is the InMem Adapter that corresponds to Qwirk::WorkerConfig (also known as parent)
7
- class WorkerConfig
8
- include Rumx::Bean
9
-
10
- bean_reader :queue_name, :string, 'Name of the queue'
11
- bean_reader :queue_size, :integer, 'Current count of messages in the queue'
12
- bean_accessor :queue_max_size, :integer, 'Max messages allowed in the queue'
13
-
14
- attr_reader :stopped, :parent
15
-
16
- def initialize(queue_adapter, parent, queue_name, topic_name, options, response_options)
17
- @parent = parent
18
- @queue_name = queue_name
19
- @topic_name = topic_name
20
- queue_max_size = options[:queue_max_size] || 100
21
- @queue = Factory.get_worker_queue(parent.name, queue_name, topic_name, queue_max_size)
22
- end
23
-
24
- def default_marshal_sym
25
- :none
26
- end
27
-
28
- def create_worker
29
- Worker.new(@parent.name, @parent.marshaler, @queue, @queue_name, @topic_name)
30
- end
31
-
32
- def stop
33
- return if @stopped
34
- Qwirk.logger.debug { "Closing #{self}" }
35
- @queue.stop
36
- @stopped = true
37
- end
38
-
39
- ## End of required override methods for worker_config adapter
40
-
41
- def queue_name
42
- @queue.name
43
- end
44
-
45
- def queue_size
46
- @queue.size
47
- end
48
-
49
- def queue_max_size
50
- @queue.max_size
51
- end
52
-
53
- def queue_max_size=(max_size)
54
- @queue.max_size = max_size
55
- end
56
- end
57
- end
58
- end
59
- end
@@ -1,50 +0,0 @@
1
- require 'yaml'
2
-
3
- require 'qwirk/queue_adapter/jms/connection'
4
- require 'qwirk/queue_adapter/jms/publisher'
5
- require 'qwirk/queue_adapter/jms/worker_config'
6
- require 'qwirk/queue_adapter/jms/worker'
7
-
8
- module Qwirk
9
- module QueueAdapter
10
- module JMS
11
- def self.init(config)
12
- Connection.new(config)
13
- end
14
-
15
- def self.same_destination?(options1, options2)
16
- if options1[:queue_name]
17
- return options1[:queue_name] == options2[:queue_name]
18
- elsif options1[:topic_name]
19
- return options1[:topic_name] == options2[:topic_name]
20
- elsif options1[:virtual_topic_name]
21
- return options1[:virtual_topic_name] == options2[:virtual_topic_name]
22
- elsif options1[:destination]
23
- return options1[:destination] == options2[:destination]
24
- else
25
- return false
26
- end
27
- end
28
-
29
- def self.create_message(session, marshaled_object, marshal_type)
30
- case marshal_type
31
- when :text
32
- session.create_text_message(marshaled_object)
33
- when :bytes
34
- msg = session.create_bytes_message()
35
- msg.data = marshaled_object
36
- msg
37
- else raise "Invalid marshal type: #{marshal_type}"
38
- end
39
- end
40
-
41
- def self.parse_response(message)
42
- if error_yaml = message['qwirk:exception']
43
- return Qwirk::RemoteException.from_hash(YAML.load(error_yaml))
44
- end
45
- marshaler = Qwirk::MarshalStrategy.find(message['qwirk:marshal'] || :ruby)
46
- return marshaler.unmarshal(message.data)
47
- end
48
- end
49
- end
50
- end
@@ -1,42 +0,0 @@
1
- require 'jms'
2
-
3
- # Handle Messaging and Queuing
4
- module Qwirk
5
- module QueueAdapter
6
- module JMS
7
- class Connection
8
- # Initialize the messaging system and connection pool for this VM
9
- def initialize(config)
10
- @config = config
11
- @connection = ::JMS::Connection.new(config)
12
- @session_pool = @connection.create_session_pool(@config)
13
- @connection.start
14
-
15
- at_exit do
16
- close
17
- end
18
- end
19
-
20
- # Create a session targeted for a consumer (producers should use the session_pool)
21
- def create_session
22
- @connection.create_session(@config || {})
23
- end
24
-
25
- def session_pool
26
- @session_pool
27
- end
28
-
29
- def close
30
- return if @closed
31
- Qwirk.logger.info "Closing JMS connection"
32
- @session_pool.close if @session_pool
33
- if @connection
34
- @connection.stop
35
- @connection.close
36
- end
37
- @closed = true
38
- end
39
- end
40
- end
41
- end
42
- end
@@ -1,37 +0,0 @@
1
- # Handle Messaging and Queuing using JMS
2
- module Qwirk
3
- module QueueAdapter
4
- module JMS
5
- class Consumer
6
- attr_reader :stopped
7
-
8
- def initialize(connection, options)
9
- @options = options
10
- @session = connection.create_session
11
- @consumer = @session.consumer(@options)
12
- @session.start
13
- @stopped = false
14
- end
15
-
16
- def receive
17
- @message = @consumer.receive
18
- return nil unless @message
19
- return JMS.parse_response(@message)
20
- end
21
-
22
- def acknowledge_message
23
- @message.acknowledge
24
- end
25
-
26
- def stop
27
- return if @stopped
28
- Qwirk.logger.info "Stopping consumer for #{@options.inspect}"
29
- # Don't clobber the session before a reply
30
- @consumer.close if @consumer
31
- @session.close if @session
32
- @stopped = true
33
- end
34
- end
35
- end
36
- end
37
- end
@@ -1,126 +0,0 @@
1
- # Handle Messaging and Queuing using JMS
2
- module Qwirk
3
- module QueueAdapter
4
- module JMS
5
- class Publisher
6
-
7
- #attr_reader :persistent, :marshaler, :reply_queue
8
-
9
- def initialize(queue_adapter, queue_name, topic_name, options, response_options)
10
- @connection = queue_adapter.adapter_info
11
- response_options ||= {}
12
- @dest_options = {:queue_name => queue_name} if queue_name
13
- @dest_options = {:topic_name => topic_name} if topic_name
14
- @persistent_sym = options[:persistent] ? :persistent : :non_persistent
15
- @time_to_live = options[:time_to_live]
16
- @response_time_to_live_str = response_options[:time_to_live] && response_options[:time_to_live].to_s
17
- @response_persistent_str = nil
18
- @response_persistent_str = (!!response_options[:persistent]).to_s unless response_options[:persistent].nil?
19
-
20
- @connection.session_pool.session do |session|
21
- # TODO: Use sync attribute so these queues aren't constantly created.
22
- @dest_queue = session.create_destination(@dest_options)
23
- if response_options
24
- reply_queue_name = response_options[:queue_name] || :temporary
25
- @reply_queue = session.create_destination(:queue_name => reply_queue_name)
26
- end
27
- end
28
- end
29
-
30
- def default_marshal_sym
31
- :ruby
32
- end
33
-
34
- # Publish the given object and return the message_id.
35
- # TODO: Too hackish to include task_id in here, think of a better solution
36
- def publish(marshaled_object, marshaler, task_id, props)
37
- message = nil
38
- @connection.session_pool.producer(:destination => @dest_queue) do |session, producer|
39
- producer.time_to_live = @time_to_live if @time_to_live
40
- producer.delivery_mode_sym = @persistent_sym
41
- message = JMS.create_message(session, marshaled_object, marshaler.marshal_type)
42
- message.jms_reply_to = @reply_queue if @reply_queue
43
- message['qwirk:marshal'] = marshaler.to_sym.to_s
44
- message['qwirk:response:time_to_live'] = @response_time_to_live_str if @response_time_to_live_str
45
- message['qwirk:response:persistent'] = @response_persistent_str unless @response_persistent_str.nil?
46
- message['QwirkTaskID'] = task_id if task_id
47
- props.each do |key, value|
48
- message.send("#{key}=", value)
49
- end
50
- producer.send(message)
51
- end
52
- # Return the adapter_info which for JMS is the message_id. This value will be sent to with_response below.
53
- return message.jms_message_id
54
- end
55
-
56
- # See Qwirk::PublishHandle#read_response for the requirements for this method.
57
- def with_response(message_id, &block)
58
- raise "Invalid call to with_response for #{self}, not setup for responding" unless @reply_queue
59
- options = { :destination => @reply_queue, :selector => "JMSCorrelationID = '#{message_id}'" }
60
- @connection.session_pool.consumer(options) do |session, consumer|
61
- yield MyConsumer.new(consumer)
62
- end
63
- end
64
-
65
- # See Qwirk::Publisher#create_task_consumer for the requirements for this method.
66
- def create_producer_consumer_pair(task_id, marshaler)
67
- producer = MyTaskProducer.new(self, @reply_queue, task_id, marshaler)
68
- consumer = Consumer.new(@connection, :destination => reply_queue, :selector => "QwirkTaskID = '#{task_id}'")
69
- return producer, consumer
70
- end
71
-
72
- def create_fail_producer_consumer_pair(task_id, marshaler)
73
- fail_queue = nil
74
- @connection.session_pool.session do |session|
75
- fail_queue = session.create_destination(:queue_name => :temporary)
76
- end
77
- producer = MyTaskProducer.new(self, fail_queue, task_id, marshaler)
78
- consumer = Consumer.new(@connection, :destination => reply_queue, :selector => "QwirkTaskID = '#{task_id}'")
79
- return producer, consumer
80
- end
81
-
82
- def to_s
83
- "Publisher: #{@dest_options.inspect}"
84
- end
85
-
86
- #######
87
- private
88
- #######
89
-
90
- class MyConsumer
91
- attr_reader :worker_name
92
-
93
- def initialize(consumer)
94
- @consumer = consumer
95
- end
96
-
97
- def timeout_read(timeout)
98
- msec = (timeout * 1000).to_i
99
- if msec > 100
100
- message = @consumer.receive(msec)
101
- else
102
- #message = @consumer.receive_no_wait
103
- message = @consumer.receive(100)
104
- end
105
- return nil unless message
106
- message.acknowledge
107
- return [message.jms_correlation_id, JMS.parse_response(message), message['qwirk:worker']]
108
- end
109
- end
110
-
111
- class MyTaskProducer
112
- def initialize(publisher, reply_queue, task_id, marshaler)
113
- @publisher = publisher
114
- @reply_queue = reply_queue
115
- @task_id = task_id
116
- @marshaler = marshaler
117
- end
118
-
119
- def send(marshaled_object)
120
- @publisher.publish(marshaled_object, @marshaler, @task_id, {})
121
- end
122
- end
123
- end
124
- end
125
- end
126
- end