bricolage-streamingload 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17aa54eda4d063cb571f3a7671a4e6413ea079e1
4
- data.tar.gz: 1e879a10e505c01a9f66393a079e18de997a3478
3
+ metadata.gz: b2043736baf77c16a2df73b7233e7f1839ea8c44
4
+ data.tar.gz: 4418e6433eaee7eca223f073a8d684b6325ac2c8
5
5
  SHA512:
6
- metadata.gz: 5f5778b2ecff8e2bf8d06e1ab00e8e1ee394772a24e4812b74b921c8303566638a212632c896b9b46c7ed5918357db89c65116595664628dba1c2e3cfbef3375
7
- data.tar.gz: ebb53db3b87334f5c725e28665f9716f2246d1aef4389d635627e31b07c356e0f9a9edea451162ca39505da82dba22f5fd710d8bbdcb24f8e0fd583c2c734955
6
+ metadata.gz: 078a0cb67b3d26a3f7dceedeedfb6e7631d39199d2d7dd762067be06e29dd1b26eb696d7d1f0fef305415b156b4b4507855170c065a7316e1d5325ee0bfb3608
7
+ data.tar.gz: 239686023066c23a915d68e313a7ed49fe015d2671c33f3e7a51f48ac1ed14d1354c6a117177dc66986da6d1a5ff20e65e34660f54b6bdc50edc203ed47295db
@@ -349,7 +349,7 @@ module Bricolage
349
349
  def SQSMessage.for_sqs_result(result)
350
350
  result.messages.flat_map {|msg|
351
351
  body = JSON.parse(msg.body)
352
- records = body['Records'] or next []
352
+ records = body['Records'] or next [UnknownSQSMessage.for_sqs_record(msg, nil)]
353
353
  records.map {|rec| get_concrete_class(msg, rec).for_sqs_record(msg, rec) }
354
354
  }
355
355
  end
@@ -361,17 +361,21 @@ module Bricolage
361
361
  end
362
362
 
363
363
  def SQSMessage.parse_sqs_record(msg, rec)
364
- time_str = rec['eventTime']
365
- tm = time_str ? (Time.parse(time_str) rescue nil) : nil
366
364
  {
367
365
  message_id: msg.message_id,
368
366
  receipt_handle: msg.receipt_handle,
369
- name: rec['eventName'],
370
- time: tm,
371
- source: rec['eventSource']
367
+ name: (rec ? rec['eventName'] : nil),
368
+ time: get_event_time(rec),
369
+ source: (rec ? rec['eventSource'] : nil)
372
370
  }
373
371
  end
374
372
 
373
+ def SQSMessage.get_event_time(rec)
374
+ return nil unless rec
375
+ str = rec['eventTime'] or return nil
376
+ Time.parse(str) rescue nil
377
+ end
378
+
375
379
  def initialize(name:, time:, source:,
376
380
  message_id: nil, receipt_handle: nil, delay_seconds: nil,
377
381
  **message_params)
@@ -416,4 +420,23 @@ module Bricolage
416
420
 
417
421
  end # class SQSMessage
418
422
 
423
+
424
+ class UnknownSQSMessage < SQSMessage
425
+
426
+ def UnknownSQSMessage.parse_sqs_record(msg, _rec)
427
+ { message_body: msg.body }
428
+ end
429
+
430
+ def message_type
431
+ 'unknown'
432
+ end
433
+
434
+ def init_message(message_body:)
435
+ @message_body = message_body
436
+ end
437
+
438
+ attr_reader :message_body
439
+
440
+ end # class UnknownSQSMessage
441
+
419
442
  end # module Bricolage
@@ -27,19 +27,22 @@ module Bricolage
27
27
  end
28
28
  config_path, * = opts.rest_arguments
29
29
  config = YAML.load(File.read(config_path))
30
- logger = opts.log_file_path ? new_logger(opts.log_file_path, config) : nil
31
- ctx = Context.for_application('.', environment: opts.environment, logger: logger)
30
+ log = opts.log_file_path ? new_logger(opts.log_file_path, config) : nil
31
+ ctx = Context.for_application('.', environment: opts.environment, logger: log)
32
+ logger = raw_logger = ctx.logger
32
33
  event_queue = ctx.get_data_source('sqs', config.fetch('event-queue-ds', 'sqs_event'))
33
34
  task_queue = ctx.get_data_source('sqs', config.fetch('task-queue-ds', 'sqs_task'))
34
- alert_logger = AlertingLogger.new(
35
- logger: ctx.logger,
36
- sns_datasource: ctx.get_data_source('sns', config.fetch('sns-ds', 'sns')),
37
- alert_level: config.fetch('alert-level', 'warn')
38
- )
35
+ if config['alert-level']
36
+ logger = AlertingLogger.new(
37
+ logger: raw_logger,
38
+ sns_datasource: ctx.get_data_source('sns', config.fetch('sns-ds', 'sns')),
39
+ alert_level: config.fetch('alert-level', 'warn')
40
+ )
41
+ end
39
42
 
40
43
  object_buffer = ObjectBuffer.new(
41
- control_data_source: ctx.get_data_source('sql', config.fetch('ctl-postgres-ds', 'db_data')),
42
- logger: alert_logger
44
+ control_data_source: ctx.get_data_source('sql', config.fetch('ctl-postgres-ds', 'db_ctl')),
45
+ logger: logger
43
46
  )
44
47
 
45
48
  url_patterns = URLPatterns.for_config(config.fetch('url_patterns'))
@@ -50,14 +53,15 @@ module Bricolage
50
53
  object_buffer: object_buffer,
51
54
  url_patterns: url_patterns,
52
55
  dispatch_interval: 60,
53
- logger: alert_logger
56
+ logger: logger
54
57
  )
55
58
 
56
59
  Process.daemon(true) if opts.daemon?
57
60
  create_pid_file opts.pid_file_path if opts.pid_file_path
58
61
  dispatcher.event_loop
59
62
  rescue Exception => e
60
- alert_logger.error e.message
63
+ logger.exception e
64
+ logger.error "dispatcher abort: pid=#{$$}"
61
65
  raise
62
66
  end
63
67
 
@@ -92,11 +96,11 @@ module Bricolage
92
96
  attr_reader :logger
93
97
 
94
98
  def event_loop
95
- logger.info "dispatcher started"
99
+ logger.info "*** dispatcher started: pid=#{$$}"
96
100
  set_dispatch_timer
97
101
  @event_queue.handle_messages(handler: self, message_class: Event)
98
102
  @event_queue.process_async_delete_force
99
- logger.info "shutdown gracefully"
103
+ logger.info "*** shutdown gracefully: pid=#{$$}"
100
104
  end
101
105
 
102
106
  # override
@@ -105,6 +109,7 @@ module Bricolage
105
109
  @event_queue.process_async_delete
106
110
 
107
111
  if @dispatch_requested
112
+ logger.info "*** dispatch requested"
108
113
  dispatch_tasks
109
114
  @dispatch_requested = false
110
115
  end
@@ -115,7 +120,13 @@ module Bricolage
115
120
  end
116
121
  end
117
122
 
123
+ def handle_unknown(e)
124
+ logger.warn "unknown event: #{e.message_body}"
125
+ @event_queue.delete_message_async(e)
126
+ end
127
+
118
128
  def handle_shutdown(e)
129
+ logger.info "*** shutdown requested"
119
130
  @event_queue.initiate_terminate
120
131
  # Delete this event immediately
121
132
  @event_queue.delete_message(e)
@@ -130,7 +141,7 @@ module Bricolage
130
141
  end
131
142
 
132
143
  def create_checkpoint
133
- logger.info "*** Creating checkpoint requested ***"
144
+ logger.info "*** checkpoint requested"
134
145
  logger.info "Force-flushing all objects..."
135
146
  tasks = @object_buffer.flush_tasks_force
136
147
  send_tasks tasks
@@ -149,7 +160,6 @@ module Bricolage
149
160
  end
150
161
 
151
162
  def handle_dispatch(e)
152
- logger.info "dispatching tasks requested"
153
163
  # Dispatching tasks may takes 10 minutes or more, it can exceeds visibility timeout.
154
164
  # To avoid this, delay dispatching until all events of current message batch are processed.
155
165
  if @dispatch_message_id == e.message_id
@@ -170,7 +180,7 @@ module Bricolage
170
180
  end
171
181
 
172
182
  def handle_flushtable(e)
173
- logger.info "flushing #{e.table_name} requested"
183
+ logger.info "*** flushtable requested: table=#{e.table_name}"
174
184
  tasks = @object_buffer.flush_table_force(e.table_name)
175
185
  send_tasks tasks
176
186
  # Delete this event immediately
@@ -12,10 +12,8 @@ module Bricolage
12
12
  when rec['eventName'] == 'dispatch' then DispatchEvent
13
13
  when rec['eventName'] == 'flushtable' then FlushTableEvent
14
14
  when rec['eventName'] == 'checkpoint' then CheckPointEvent
15
- when rec['eventSource'] == 'aws:s3'
16
- S3ObjectEvent
17
- else
18
- raise "[FATAL] unknown SQS message record: eventSource=#{rec['eventSource']} event=#{rec['eventName']} message_id=#{msg.message_id}"
15
+ when !!rec['s3'] then S3ObjectEvent
16
+ else UnknownSQSMessage
19
17
  end
20
18
  end
21
19
 
@@ -105,6 +105,12 @@ module Bricolage
105
105
  @ctl_ds.open {|conn| LoadTask.load(conn, task_id, force: force) }
106
106
  end
107
107
 
108
+ # message handler
109
+ def handle_unknown(task)
110
+ @logger.warn "unknown task: #{task.message_body}"
111
+ @task_queue.delete_message task
112
+ end
113
+
108
114
  # message handler
109
115
  def handle_streaming_load_v3(task)
110
116
  Dir.chdir(@working_dir) {
@@ -10,8 +10,7 @@ module Bricolage
10
10
  def Task.get_concrete_class(msg, rec)
11
11
  case
12
12
  when rec['eventName'] == 'streaming_load_v3' then LoadTask
13
- else
14
- raise "[FATAL] unknown SQS message record: eventSource=#{rec['eventSource']} event=#{rec['eventName']} message_id=#{msg.message_id}"
13
+ else UnknownSQSMessage
15
14
  end
16
15
  end
17
16
 
@@ -1,5 +1,5 @@
1
1
  module Bricolage
2
2
  module StreamingLoad
3
- VERSION = '0.6.0'
3
+ VERSION = '0.6.1'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bricolage-streamingload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Minero Aoki