Class: XmlConvApp

Inherits:
SBSM::AdminServer
  • Object
show all
Defined in:
lib/xmlconv/util/application.rb

Constant Summary collapse

POLLING_INTERVAL =
  • 15

60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app: XmlConv::Util::RackInterface.new) ⇒ XmlConvApp

Returns a new instance of XmlConvApp.



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/xmlconv/util/application.rb', line 131

def initialize(app: XmlConv::Util::RackInterface.new)
   @rack_app = app
   super(app: app) # TODO?? , multi_threaded: true)
	@persistence_layer = ODBA.cache.fetch_named('XmlConv', self) do XmlConv::Util::Application.new end
	@persistence_layer.init
	@dispatch_queue = Queue.new
   @polling_interval = XmlConv::CONFIG.polling_interval || self::class::POLLING_INTERVAL
   puts "@polling_interval is #{@polling_interval} @persistence_layer is #{@persistence_layer.class}"
   start_polling  if @polling_interval
	start_dispatcher
	start_invoicer if XmlConv::CONFIG.run_invoicer
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



129
130
131
# File 'lib/xmlconv/util/application.rb', line 129

def app
  @app
end

#dispatch_queueObject (readonly)

Returns the value of attribute dispatch_queue.



129
130
131
# File 'lib/xmlconv/util/application.rb', line 129

def dispatch_queue
  @dispatch_queue
end

#dispatcher_threadObject (readonly)

Returns the value of attribute dispatcher_thread.



129
130
131
# File 'lib/xmlconv/util/application.rb', line 129

def dispatcher_thread
  @dispatcher_thread
end

#persistence_layerObject (readonly)

Returns the value of attribute persistence_layer.



129
130
131
# File 'lib/xmlconv/util/application.rb', line 129

def persistence_layer
  @persistence_layer
end

#polling_threadObject (readonly)

Returns the value of attribute polling_thread.



129
130
131
# File 'lib/xmlconv/util/application.rb', line 129

def polling_thread
  @polling_thread
end

Instance Method Details

#dispatch(transaction) ⇒ Object



143
144
145
# File 'lib/xmlconv/util/application.rb', line 143

def dispatch(transaction)
   @dispatch_queue.push(transaction)
end

#execute_with_response(transaction) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/xmlconv/util/application.rb', line 146

def execute_with_response(transaction)
  begin
    @persistence_layer.execute(transaction)
  rescue Exception => e
    puts "rescued #{e.class}"
  end
  transaction.response.to_s
end

#start_dispatcherObject



154
155
156
157
158
159
160
161
# File 'lib/xmlconv/util/application.rb', line 154

def start_dispatcher
	@dispatcher_thread = Thread.new {
		Thread.current.abort_on_exception = true
		loop {
			@persistence_layer.execute(@dispatch_queue.pop)
		}
	}
end

#start_invoicerObject



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/xmlconv/util/application.rb', line 162

def start_invoicer
  @invoicer_thread = Thread.new {
    Thread.current.abort_on_exception = true
    loop {
      this_month = Date.today
      next_month = this_month >> 1
      strt = Time.local(this_month.year, this_month.month)
      stop = Time.local(next_month.year, next_month.month)
      sleep(stop - Time.now)
      @persistence_layer.send_invoice(strt...stop)
    }
  }
end

#start_pollingObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/xmlconv/util/application.rb', line 175

def start_polling
	@polling_thread = Thread.new {
     Thread.current.abort_on_exception = true
		loop {
			begin
				XmlConv::Util::PollingManager.new(@persistence_layer).poll_sources
			rescue Exception => exc
				SBSM.logger.error(XmlConv::CONFIG.program_name) {
           [exc.class, exc.message].concat(exc.backtrace).join("\n")
         }
			end
			sleep(@polling_interval)
		}
	}
end