Class: Bricolage::StreamingLoad::Dispatcher
- Inherits:
-
Bricolage::SQSDataSource::MessageHandler
- Object
- Bricolage::SQSDataSource::MessageHandler
- Bricolage::StreamingLoad::Dispatcher
- Defined in:
- lib/bricolage/streamingload/dispatcher.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
Instance Method Summary collapse
-
#after_message_batch ⇒ Object
override.
- #dispatch_tasks(tasks) ⇒ Object
- #do_handle_checkpoint ⇒ Object
- #do_handle_dispatch ⇒ Object
- #event_loop ⇒ Object
- #handle_checkpoint(msg) ⇒ Object
- #handle_data(msg) ⇒ Object
- #handle_dispatch(msg) ⇒ Object
- #handle_flushtable(msg) ⇒ Object
- #handle_shutdown(msg) ⇒ Object
- #handle_unknown(msg) ⇒ Object
-
#initialize(event_queue:, task_queue:, chunk_router:, chunk_buffer:, task_logger:, dispatch_interval:, logger:) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
- #set_dispatch_timer ⇒ Object
Methods inherited from Bricolage::SQSDataSource::MessageHandler
#call_handler_method, #handle, #handleable?, #handler_method
Constructor Details
#initialize(event_queue:, task_queue:, chunk_router:, chunk_buffer:, task_logger:, dispatch_interval:, logger:) ⇒ Dispatcher
Returns a new instance of Dispatcher.
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 105 def initialize(event_queue:, task_queue:, chunk_router:, chunk_buffer:, task_logger:, dispatch_interval:, logger:) @event_queue = event_queue @task_queue = task_queue @chunk_router = chunk_router @chunk_buffer = chunk_buffer @task_logger = task_logger @dispatch_interval = dispatch_interval = nil @logger = logger @dispatch_requested = false @checkpoint_requested = false end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
118 119 120 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 118 def logger @logger end |
Class Method Details
._main ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 29 def Dispatcher._main opts = DispatcherOptions.new(ARGV) opts.parse unless opts.rest_arguments.size == 1 $stderr.puts opts.usage exit 1 end config_path, * = opts.rest_arguments config = YAML.load(File.read(config_path)) log = opts.log_file_path ? new_logger(File.(opts.log_file_path), config) : nil ctx = Context.for_application('.', environment: opts.environment, logger: log) logger = raw_logger = ctx.logger event_queue = ctx.get_data_source('sqs', config.fetch('event-queue-ds', 'sqs_event')) task_queue = ctx.get_data_source('sqs', config.fetch('task-queue-ds', 'sqs_task')) if config['alert-level'] logger = AlertingLogger.new( logger: raw_logger, sns_datasource: ctx.get_data_source('sns', config.fetch('sns-ds', 'sns')), alert_level: config.fetch('alert-level', 'warn') ) end chunk_buffer = ChunkBuffer.new( control_data_source: ctx.get_data_source('sql', config.fetch('ctl-postgres-ds', 'db_ctl')), logger: logger ) chunk_router = ChunkRouter.for_config(config.fetch('url_patterns')) task_logger = LoadTaskLogger.new( ctx.get_data_source('s3', config.fetch('ctl-s3-ds', 's3_ctl')) ) dispatcher = Dispatcher.new( event_queue: event_queue, task_queue: task_queue, chunk_router: chunk_router, chunk_buffer: chunk_buffer, task_logger: task_logger, dispatch_interval: config.fetch('dispatch-interval', 60), logger: logger ) if opts.task_id dispatcher.dispatch_tasks chunk_buffer.load_tasks_by_id([opts.task_id]) exit 0 end Process.daemon(true) if opts.daemon? create_pid_file opts.pid_file_path if opts.pid_file_path Dir.chdir '/' begin dispatcher.event_loop rescue Exception => e logger.exception e logger.error "dispatcher abort: pid=#{$$}" raise end end |
.create_pid_file(path) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 97 def Dispatcher.create_pid_file(path) File.open(path, 'w') {|f| f.puts $$ } rescue # ignore end |
.main ⇒ Object
23 24 25 26 27 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 23 def Dispatcher.main Raven.capture { _main } end |
.new_logger(path, config) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 89 def Dispatcher.new_logger(path, config) Logger.new( device: path, rotation_period: config.fetch('log-rotation-period', 'daily'), rotation_size: config.fetch('log-rotation-size', nil) ) end |
Instance Method Details
#after_message_batch ⇒ Object
override
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 129 def # must be processed first @event_queue.process_async_delete if @dispatch_requested logger.info "*** dispatch requested" do_handle_dispatch @dispatch_requested = false end if @checkpoint_requested do_handle_checkpoint @checkpoint_requested = false # is needless, but reset it just in case end end |
#dispatch_tasks(tasks) ⇒ Object
215 216 217 218 219 220 221 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 215 def dispatch_tasks(tasks) tasks.each do |task| msg = StreamingLoadV3LoaderMessage.for_load_task(task) @task_queue.put msg @task_logger.log task end end |
#do_handle_checkpoint ⇒ Object
165 166 167 168 169 170 171 172 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 165 def do_handle_checkpoint logger.info "*** checkpoint requested" logger.info "Force-flushing all objects..." tasks = @chunk_buffer.flush_all dispatch_tasks tasks logger.info "All objects flushed; shutting down..." @event_queue.initiate_terminate end |
#do_handle_dispatch ⇒ Object
193 194 195 196 197 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 193 def do_handle_dispatch tasks = @chunk_buffer.flush_partial dispatch_tasks tasks set_dispatch_timer end |
#event_loop ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 120 def event_loop logger.info "*** dispatcher started: pid=#{$$}" set_dispatch_timer @event_queue.(handler: self, message_class: DispatcherMessage) @event_queue.process_async_delete_force logger.info "*** shutdown gracefully: pid=#{$$}" end |
#handle_checkpoint(msg) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 157 def handle_checkpoint(msg) # Delay creating CHECKPOINT after the current message batch, # because any other extra events are already received. @checkpoint_requested = true # Delete this event immediately @event_queue.(msg) end |
#handle_data(msg) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 174 def handle_data(msg) unless msg.created_event? @event_queue.(msg) return end chunk = @chunk_router.route(msg) @chunk_buffer.save(chunk) @event_queue.(msg) end |
#handle_dispatch(msg) ⇒ Object
184 185 186 187 188 189 190 191 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 184 def handle_dispatch(msg) # Dispatching tasks may takes 10 minutes or more, it can exceeds visibility timeout. # To avoid this, delay dispatching until all events of current message batch are processed. if == msg. @dispatch_requested = true end @event_queue.(msg) end |
#handle_flushtable(msg) ⇒ Object
204 205 206 207 208 209 210 211 212 213 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 204 def handle_flushtable(msg) # FIXME: badly named attribute. table_name is really stream_name, which is called as data_source_id, too. stream_name = msg.table_name logger.info "*** flushtable requested: stream_name=#{stream_name}" tasks = @chunk_buffer.flush_stream(stream_name) dispatch_tasks tasks # Delete this event immediately @event_queue.(msg) end |
#handle_shutdown(msg) ⇒ Object
150 151 152 153 154 155 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 150 def handle_shutdown(msg) logger.info "*** shutdown requested" @event_queue.initiate_terminate # Delete this event immediately @event_queue.(msg) end |
#handle_unknown(msg) ⇒ Object
145 146 147 148 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 145 def handle_unknown(msg) logger.warn "unknown event: #{msg.message_body}" @event_queue.(msg) end |
#set_dispatch_timer ⇒ Object
199 200 201 202 |
# File 'lib/bricolage/streamingload/dispatcher.rb', line 199 def set_dispatch_timer res = @event_queue.(DispatchDispatcherMessage.create(delay_seconds: @dispatch_interval)) = res. end |