nulogy_message_bus_consumer 1.0.0.alpha → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/lib/nulogy_message_bus_consumer.rb +16 -7
  3. data/lib/nulogy_message_bus_consumer/config.rb +7 -1
  4. data/lib/nulogy_message_bus_consumer/message.rb +11 -13
  5. data/lib/nulogy_message_bus_consumer/steps/timed_task.rb +42 -0
  6. data/lib/nulogy_message_bus_consumer/tasks/log_consumer_lag.rb +45 -0
  7. data/lib/nulogy_message_bus_consumer/tasks/prune_processed_messages.rb +37 -0
  8. data/lib/nulogy_message_bus_consumer/{steps → tasks}/supervise_consumer_lag.rb +15 -26
  9. data/lib/nulogy_message_bus_consumer/version.rb +1 -1
  10. data/spec/dummy/Rakefile +6 -0
  11. data/spec/dummy/app/assets/config/manifest.js +3 -0
  12. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  13. data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
  14. data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
  15. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  16. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  17. data/spec/dummy/app/javascript/packs/application.js +15 -0
  18. data/spec/dummy/app/jobs/application_job.rb +7 -0
  19. data/spec/dummy/app/mailers/application_mailer.rb +4 -0
  20. data/spec/dummy/app/models/application_record.rb +3 -0
  21. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  22. data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
  23. data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
  24. data/spec/dummy/bin/rails +4 -0
  25. data/spec/dummy/bin/rake +4 -0
  26. data/spec/dummy/bin/setup +33 -0
  27. data/spec/dummy/config.ru +5 -0
  28. data/spec/dummy/config/application.rb +29 -0
  29. data/spec/dummy/config/boot.rb +5 -0
  30. data/spec/dummy/config/cable.yml +10 -0
  31. data/spec/dummy/config/credentials/message-bus-us-east-1.key +1 -0
  32. data/spec/dummy/config/credentials/message-bus-us-east-1.yml.enc +1 -0
  33. data/spec/dummy/config/database.yml +27 -0
  34. data/spec/dummy/config/environment.rb +5 -0
  35. data/spec/dummy/config/environments/development.rb +62 -0
  36. data/spec/dummy/config/environments/production.rb +112 -0
  37. data/spec/dummy/config/environments/test.rb +49 -0
  38. data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
  39. data/spec/dummy/config/initializers/assets.rb +12 -0
  40. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  41. data/spec/dummy/config/initializers/content_security_policy.rb +28 -0
  42. data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
  43. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  44. data/spec/dummy/config/initializers/inflections.rb +16 -0
  45. data/spec/dummy/config/initializers/message_bus_consumer.rb +5 -0
  46. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  47. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  48. data/spec/dummy/config/locales/en.yml +33 -0
  49. data/spec/dummy/config/puma.rb +36 -0
  50. data/spec/dummy/config/routes.rb +3 -0
  51. data/spec/dummy/config/spring.rb +6 -0
  52. data/spec/dummy/config/storage.yml +34 -0
  53. data/spec/dummy/db/schema.rb +21 -0
  54. data/spec/dummy/log/development.log +4 -0
  55. data/spec/dummy/log/production.log +18 -0
  56. data/spec/dummy/log/test.log +7949 -0
  57. data/spec/dummy/public/404.html +67 -0
  58. data/spec/dummy/public/422.html +67 -0
  59. data/spec/dummy/public/500.html +66 -0
  60. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  61. data/spec/dummy/public/apple-touch-icon.png +0 -0
  62. data/spec/dummy/public/favicon.ico +0 -0
  63. data/spec/dummy/tmp/development_secret.txt +1 -0
  64. data/spec/integration/nulogy_message_bus_consumer/auditor_spec.rb +59 -0
  65. data/spec/integration/nulogy_message_bus_consumer/kafka_utils_spec.rb +41 -0
  66. data/spec/integration/nulogy_message_bus_consumer/steps/commit_on_success_spec.rb +131 -0
  67. data/spec/integration/nulogy_message_bus_consumer/steps/connect_to_message_bus_spec.rb +53 -0
  68. data/spec/integration/nulogy_message_bus_consumer/tasks/prune_processed_messages_spec.rb +32 -0
  69. data/spec/integration/nulogy_message_bus_consumer/tasks/supervise_consumer_lag_spec.rb +33 -0
  70. data/spec/integration/test_topic_spec.rb +39 -0
  71. data/spec/spec_helper.rb +50 -0
  72. data/spec/support/kafka.rb +74 -0
  73. data/spec/support/middleware_tap.rb +12 -0
  74. data/spec/support/skip.rb +9 -0
  75. data/spec/support/test_topic.rb +48 -0
  76. data/spec/unit/nulogy_message_bus_consumer/config_spec.rb +20 -0
  77. data/spec/unit/nulogy_message_bus_consumer/lag_tracker.rb +35 -0
  78. data/spec/unit/nulogy_message_bus_consumer/message_spec.rb +84 -0
  79. data/spec/unit/nulogy_message_bus_consumer/pipeline_spec.rb +49 -0
  80. data/spec/unit/nulogy_message_bus_consumer/steps/commit_on_success_spec.rb +58 -0
  81. data/spec/unit/nulogy_message_bus_consumer/steps/deduplicate_messages_spec.rb +56 -0
  82. data/spec/unit/nulogy_message_bus_consumer/steps/log_messages_spec.rb +70 -0
  83. data/spec/unit/nulogy_message_bus_consumer/steps/stream_messages_spec.rb +35 -0
  84. data/spec/unit/nulogy_message_bus_consumer/tasks/calculator_spec.rb +67 -0
  85. data/spec/unit/nulogy_message_bus_consumer_spec.rb +30 -0
  86. metadata +167 -13
  87. data/lib/nulogy_message_bus_consumer/steps/log_consumer_lag.rb +0 -51
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d17557a978d8762a83afcb1a8027296854d516cde57540243d48c80cc46c012
4
- data.tar.gz: 02c45bfab242f35a0143d090a08387f80db0030854a28b161792cb09efded53c
3
+ metadata.gz: '0989adbcf8a099ac67362106e1724097f7266523a290a36d568d2f8d195a085a'
4
+ data.tar.gz: da702aedd0055c0fb3ee4269999bc72009bc6d0823f68264770caa11c10c445c
5
5
  SHA512:
6
- metadata.gz: 8c7ef5e49a3aff7e09e963592fc79f7435a29d3567764d0cec1e98d52fea31d18f1ee2ea4b3d429c2e550307f0fab539733d700716b89b45c05cfee269e87955
7
- data.tar.gz: 0be71a225c18e458d440f80eefadfb5b1a841ad57d619af434e12832c48ff31c1ce2614aaf5786330e2282e418838a223f91c82ebae74cedda3801446620fbb2
6
+ metadata.gz: 690db182773fca7bb41d230eb2a5a2c866f61eae733d1afd63b3f7b4693a747b0d1a82d169f7c09b745d70dd5e945e0c745eebb138e7c2405d608ccb736572ba
7
+ data.tar.gz: 2f5bf0079b7932e15cf9996518d17ec9309882f601cef507a8952ebf22f4c9da01ba5ede9d77674bc491c2dbc6a04b6fb00d095e106075c9d2ad81a001421b49
@@ -16,12 +16,14 @@ require "nulogy_message_bus_consumer/processed_message"
16
16
  require "nulogy_message_bus_consumer/steps/commit_on_success"
17
17
  require "nulogy_message_bus_consumer/steps/connect_to_message_bus"
18
18
  require "nulogy_message_bus_consumer/steps/deduplicate_messages"
19
- require "nulogy_message_bus_consumer/steps/log_consumer_lag"
20
19
  require "nulogy_message_bus_consumer/steps/log_messages"
21
20
  require "nulogy_message_bus_consumer/steps/seek_beginning_of_topic"
22
21
  require "nulogy_message_bus_consumer/steps/stream_messages"
23
22
  require "nulogy_message_bus_consumer/steps/stream_messages_until_none_are_left"
24
- require "nulogy_message_bus_consumer/steps/supervise_consumer_lag"
23
+ require "nulogy_message_bus_consumer/steps/timed_task"
24
+ require "nulogy_message_bus_consumer/tasks/log_consumer_lag"
25
+ require "nulogy_message_bus_consumer/tasks/prune_processed_messages"
26
+ require "nulogy_message_bus_consumer/tasks/supervise_consumer_lag"
25
27
 
26
28
  module NulogyMessageBusConsumer
27
29
  module_function
@@ -49,11 +51,18 @@ module NulogyMessageBusConsumer
49
51
  # Note: that since they are before `StreamMessages`, they will only
50
52
  # be called once, without any messages.
51
53
  Steps::ConnectToMessageBus.new(config, logger),
52
- Steps::LogConsumerLag.new(logger),
53
- Steps::SuperviseConsumerLag.new(
54
- logger,
55
- check_interval_seconds: config.lag_check_interval_seconds,
56
- tracker: LagTracker.new(failing_checks: config.lag_checks)
54
+ Steps::TimedTask.new(
55
+ Tasks::LogConsumerLag.new(logger, config.log_lag_interval_seconds)
56
+ ),
57
+ Steps::TimedTask.new(
58
+ Tasks::PruneProcessedMessages.new(logger, config.prune_interval_seconds, config.prune_max_age)
59
+ ),
60
+ Steps::TimedTask.new(
61
+ Tasks::SuperviseConsumerLag.new(
62
+ logger,
63
+ check_interval_seconds: config.lag_check_interval_seconds,
64
+ tracker: LagTracker.new(failing_checks: config.lag_checks)
65
+ )
57
66
  ),
58
67
  Steps::StreamMessages.new(logger),
59
68
  # Message processing steps start here.
@@ -5,12 +5,18 @@ module NulogyMessageBusConsumer
5
5
  :consumer_group_id,
6
6
  :lag_check_interval_seconds,
7
7
  :lag_checks,
8
+ :log_lag_interval_seconds,
9
+ :prune_interval_seconds,
10
+ :prune_max_age,
8
11
  :topic_name
9
12
 
10
13
  def initialize(options = {})
11
14
  defaults = {
12
15
  lag_check_interval_seconds: 20,
13
- lag_checks: 6
16
+ lag_checks: 6,
17
+ log_lag_interval_seconds: 1.minute.to_i,
18
+ prune_interval_seconds: 1.hour.to_i,
19
+ prune_max_age: 8.days
14
20
  }
15
21
 
16
22
  update(defaults.merge(options))
@@ -1,17 +1,16 @@
1
1
  module NulogyMessageBusConsumer
2
2
  class Message
3
- attr_reader :company_uuid,
4
- :created_at,
5
- :event_data,
6
- :event_data_unparsed,
7
- :id,
8
- :key,
9
- :offset,
10
- :partition,
11
- :subscription_id,
12
- :timestamp,
13
- :topic,
14
- :type
3
+ attr_reader :event_data
4
+ attr_reader :event_data_unparsed
5
+ attr_reader :id
6
+ attr_reader :key
7
+ attr_reader :offset
8
+ attr_reader :partition
9
+ attr_reader :subscription_id
10
+ attr_reader :company_uuid
11
+ attr_reader :timestamp
12
+ attr_reader :topic
13
+ attr_reader :created_at
15
14
 
16
15
  def initialize(attrs = {})
17
16
  attrs.each { |key, value| instance_variable_set("@#{key}", value) }
@@ -37,7 +36,6 @@ module NulogyMessageBusConsumer
37
36
  company_uuid: envelope_data[:company_uuid] || envelope_data[:tenant_id],
38
37
  timestamp: kafka_message.timestamp,
39
38
  topic: kafka_message.topic,
40
- type: envelope_data[:type],
41
39
  created_at: envelope_data[:created_at]
42
40
  )
43
41
  end
@@ -0,0 +1,42 @@
1
+ module NulogyMessageBusConsumer
2
+ module Steps
3
+ # A generic class to run a "task" on a timer (in a separate thread!)
4
+ # This class runs the code, the Task does the work
5
+ #
6
+ # A Task must implement the methods called in this class:
7
+ # - #extract_args(kwargs)
8
+ # Called with the keyword arguments (kwargs) that is passed to this step.
9
+ # This is a chance to pull out references to pipeline variables (e.g. kafka_consumer)
10
+ # - #call
11
+ # The work the Task should perform
12
+ # - #interval
13
+ # The time, in seconds, between invocations of #call
14
+ class TimedTask
15
+ def initialize(task)
16
+ @task = task
17
+ end
18
+
19
+ def call(**kwargs)
20
+ @task.extract_args(**kwargs)
21
+
22
+ # Ensure that the process is terminated if there is a problem getting the consumption lag.
23
+ # This also ensures that the process will terminate on-boot if it cannot connect to Kafka,
24
+ # allowing the container to be terminated by ECS.
25
+ Thread.abort_on_exception = true
26
+ Thread.new { run }
27
+
28
+ yield
29
+ end
30
+
31
+ private
32
+
33
+ def run
34
+ loop do
35
+ @task.call
36
+
37
+ sleep @task.interval
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,45 @@
1
+ module NulogyMessageBusConsumer
2
+ module Tasks
3
+ class LogConsumerLag
4
+ attr_reader :interval
5
+
6
+ def initialize(logger, interval_seconds)
7
+ @logger = logger
8
+ @interval = interval_seconds
9
+ end
10
+
11
+ def extract_args(kafka_consumer:, **_)
12
+ @kafka_consumer = kafka_consumer
13
+ end
14
+
15
+ def call
16
+ # Delayed start. If we attempt to read consumer#committed immediately, it may fail.
17
+ # We suspect this is because the consumer#committed is called before the consumer
18
+ # has finished connecting. There appears to be a race condition.
19
+ KafkaUtils.wait_for_assignment(@kafka_consumer)
20
+
21
+ lag_per_topic = @kafka_consumer.lag(@kafka_consumer.committed)
22
+
23
+ @logger.info(JSON.dump({
24
+ event: "consumer_lag",
25
+ topics: Calculator.add_max_lag(lag_per_topic)
26
+ }))
27
+ $stdout.flush
28
+ end
29
+
30
+ module Calculator
31
+ def self.add_max_lag(lag_by_topic)
32
+ lag_by_topic.each_value do |lag_by_partition|
33
+ lag_by_partition[:_max] = lag_by_partition.values.max || 0
34
+ end
35
+
36
+ lag_by_topic[:_max] = lag_by_topic
37
+ .map { |_topic, lag_by_partition| lag_by_partition[:_max] }
38
+ .max || 0
39
+
40
+ lag_by_topic
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,37 @@
1
+ module NulogyMessageBusConsumer
2
+ module Tasks
3
+ # Deletes any ProcessedMessage rows that are older than max_age
4
+ #
5
+ # interval_seconds: how many seconds between invocations
6
+ # max_age: ActiveSupport::Duration of how old ProcessedMessage are
7
+ # removed. This calls `#ago` on it.
8
+ # For example, setting this to `1.week` will delete records more than
9
+ # `1.week.ago` on each invocation.
10
+ class PruneProcessedMessages
11
+ attr_reader :interval
12
+
13
+ def initialize(logger, interval_seconds, max_age)
14
+ @logger = logger
15
+ @interval = interval_seconds
16
+ @max_age = max_age
17
+ end
18
+
19
+ def extract_args(**_)
20
+ end
21
+
22
+ def call
23
+ deleted = prune_stale
24
+
25
+ @logger.info("Pruned #{deleted} processed messages")
26
+ end
27
+
28
+ private
29
+
30
+ def prune_stale
31
+ ProcessedMessage
32
+ .where(ProcessedMessage.arel_table[:created_at].lt(@max_age.ago))
33
+ .delete_all
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,5 +1,5 @@
1
1
  module NulogyMessageBusConsumer
2
- module Steps
2
+ module Tasks
3
3
  # Supervises the consumer's lag.
4
4
  #
5
5
  # If a partition's lag is non-zero and does not change for an extended period
@@ -19,47 +19,36 @@ module NulogyMessageBusConsumer
19
19
  #
20
20
  # Killing the main thread causes ECS to restart the task.
21
21
  class SuperviseConsumerLag
22
+ attr_reader :interval
23
+
22
24
  def initialize(logger, tracker: NulogyMessageBusConsumer::LagTracker.new(failing_checks: 6), killable: nil, check_interval_seconds: 20)
23
25
  @logger = logger
24
26
  @tracker = tracker
25
27
  @killable = killable
26
- @check_interval_seconds = check_interval_seconds
28
+ @interval = check_interval_seconds
27
29
  end
28
30
 
29
- def call(kafka_consumer:, **_)
31
+ def extract_args(kafka_consumer:, **_)
30
32
  @consumer = kafka_consumer
31
33
  @killable ||= Thread.current
32
-
33
- run
34
-
35
- yield
36
34
  end
37
35
 
38
- private
39
-
40
- def run
41
- Thread.abort_on_exception = true
36
+ def call
37
+ NulogyMessageBusConsumer::KafkaUtils.wait_for_assignment(@consumer)
42
38
 
43
- Thread.new do
44
- NulogyMessageBusConsumer::KafkaUtils.wait_for_assignment(@consumer)
39
+ @tracker.update(@consumer.lag(@consumer.committed))
40
+ if @tracker.failing?
41
+ log_failed_partitions
45
42
 
46
- loop do
47
- @tracker.update(@consumer.lag(@consumer.committed))
48
-
49
- if @tracker.failing?
50
- log_failed_partitions
51
-
52
- @killable.kill
53
- Thread.current.exit
54
- end
55
-
56
- sleep @check_interval_seconds
57
- end
43
+ @killable.kill
44
+ Thread.current.exit
58
45
  end
59
46
  end
60
47
 
48
+ private
49
+
61
50
  def log_failed_partitions
62
- seconds = @check_interval_seconds * @tracker.failing_checks
51
+ seconds = @interval * @tracker.failing_checks
63
52
  failed = @tracker
64
53
  .failed
65
54
  .map { |topic, partitions| "#{topic}: #{partitions.join(",")}" }
@@ -1,3 +1,3 @@
1
1
  module NulogyMessageBusConsumer
2
- VERSION = "1.0.0.alpha".freeze
2
+ VERSION = "1.0.0"
3
3
  end
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require_relative "config/application"
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,3 @@
1
+ //= link_tree ../images
2
+ //= link_directory ../stylesheets .css
3
+ //= link message_bus_manifest.js
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module ApplicationCable
2
+ class Channel < ActionCable::Channel::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ApplicationCable
2
+ class Connection < ActionCable::Connection::Base
3
+ end
4
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require rails-ujs
14
+ //= require activestorage
15
+ //= require_tree .
@@ -0,0 +1,7 @@
1
+ class ApplicationJob < ActiveJob::Base
2
+ # Automatically retry jobs that encountered a deadlock
3
+ # retry_on ActiveRecord::Deadlocked
4
+
5
+ # Most jobs are safe to ignore if the underlying records are no longer available
6
+ # discard_on ActiveJob::DeserializationError
7
+ end
@@ -0,0 +1,4 @@
1
+ class ApplicationMailer < ActionMailer::Base
2
+ default from: "from@example.com"
3
+ layout "mailer"
4
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag 'application', media: 'all' %>
9
+ </head>
10
+
11
+ <body>
12
+ <%= yield %>
13
+ </body>
14
+ </html>
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
+ <style>
6
+ /* Email styles need to be inline */
7
+ </style>
8
+ </head>
9
+
10
+ <body>
11
+ <%= yield %>
12
+ </body>
13
+ </html>
@@ -0,0 +1 @@
1
+ <%= yield %>
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path("../config/application", __dir__)
3
+ require_relative "../config/boot"
4
+ require "rails/commands"