Module: Ap4r::AsyncHelper::Base

Defined in:
lib/ap4r/async_helper.rb

Constant Summary collapse

Converters =
{}
DRUBY_HOST =
ENV['AP4R_DRUBY_HOST'] || 'localhost'
DRUBY_PORT =
ENV['AP4R_DRUBY_PORT'] || '6438'
DRUBY_URI =
"druby://#{DRUBY_HOST}:#{DRUBY_PORT}"
@@druby_uris =
[DRUBY_URI]
@@druby_uri_options =
{ :rotate => false, :fail_over => false, :fail_reuse => false }
@@druby_uri_retry_count =
0
@@druby_uris_size =
1
@@default_dispatch_mode =
:HTTP
@@default_rm_options =
{ :delivery => :once, :dispatch_mode => @@default_dispatch_mode }
@@default_queue_prefix =
"queue."

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.druby_uris(uris, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ap4r/async_helper.rb', line 34

def druby_uris(uris, options = {})
  @@druby_uris = []
  if uris.empty?
    @@druby_uris << DRUBY_URI
  else
    @@druby_uris << uris
    @@druby_uris.flatten!
  end
  @@druby_uri_options = @@druby_uri_options.merge(options)
  @@druby_uris_size = @@druby_uris.size
  @@druby_uri_retry_count = 0
end

Instance Method Details

#async_dispatch(url_options = {}, async_params = {}, rm_options = {}, &block) ⇒ Object

This method is aliased as ::Ap4r::Client#async_to



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ap4r/async_helper.rb', line 97

def async_dispatch(url_options = {}, async_params = {}, rm_options = {}, &block)

  if logger.debug?
    logger.debug("url_options: ")
    logger.debug(url_options.inspect)
    logger.debug("async_params: ")
    logger.debug(async_params.inspect)
    logger.debug("rm_options: ")
    logger.debug(rm_options.inspect)
  end

  rm_options = @@default_rm_options.merge(rm_options || {})

  # Only async_params is not cloned. options and rm_options are cloned before now.
  # This is a current contract between this class and converter classes.
  converter = Converters[rm_options[:dispatch_mode]].new(url_options, async_params, rm_options, self)
  # logger.debug("druby uri for queue-manager : #{@@druby_uris.first}")

  queue_name = converter.queue_name
  queue_message = converter.make_params
  queue_headers = converter.make_rm_options

  message_builder = ::Ap4r::MessageBuilder.new(queue_name, queue_message, queue_headers)
  if block_given?
    message_builder.instance_eval(&block)
  end
  queue_name = message_builder.queue_name
  queue_headers = message_builder.message_headers
  # TODO: proces flow of Converter and MessageBuilder should (probably) be reversed 2007/09/19 by shino
  # This branching is ad-hoc fix
  if queue_headers[:dispatch_mode] == :HTTP
    queue_message = message_builder.format_message_body
  else
    queue_message = message_builder.message_body
  end


  if Thread.current[:use_saf]
    stored_message = ::Ap4r::StoredMessage.store(queue_name, queue_message, queue_headers)

    Thread.current[:stored_messages].store(
                                           stored_message.id,
                                           {
                                             :queue_message => queue_message,
                                             :queue_name => queue_name,
                                             :queue_headers => queue_headers
                                           } )
    return stored_message.id
  end

  __queue_put(queue_name, queue_message, queue_headers)
end

#transaction_with_saf(active_record_class = ::Ap4r::StoredMessage, *objects, &block) ⇒ Object

This method is aliased as ::Ap4r::Client#transaction



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ap4r/async_helper.rb', line 50

def transaction_with_saf(active_record_class = ::Ap4r::StoredMessage, *objects, &block)

  Thread.current[:use_saf] = true
  Thread.current[:stored_messages] = {}

  # store
  active_record_class ||= ::Ap4r::StoredMessage
  active_record_class.transaction(*objects, &block)

  # forward
  forwarded_messages = {}
  begin

    # TODO: reconsider forwarding strategy, 2006/10/13 kato-k
    # Once some error occured, such as disconnect reliable-msg or server crush,
    # which is smart to keep to put a message or stop to do it?
    # In the case of being many async messages, the former strategy is not so good.
    #
    # TODO: add delayed forward mode 2007/05/02 by shino
    Thread.current[:stored_messages].each {|k,v|
      __queue_put(v[:queue_name], v[:queue_message], v[:queue_headers])
      forwarded_messages[k] = v
    }
  rescue Exception => err
    # Don't raise any Exception. Application logic has already completed and messages are saved.
    logger.warn("Failed to put a message into queue: #{err}")
  end

  begin
    StoredMessage.transaction do
      options = {:delete_mode => @@saf_delete_mode || :physical}
      forwarded_messages.keys.each {|id|
        ::Ap4r::StoredMessage.destroy_if_exists(id, options)
      }
    end
  rescue Exception => err
    # Don't raise any Exception. Application logic has already completed and messages are saved.
    logger.warn("Failed to put a message into queue: #{err}")
  end

ensure
  Thread.current[:use_saf] = false
  Thread.current[:stored_messages] = nil
end