stenotype 0.1.2 → 0.1.8

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
  SHA256:
3
- metadata.gz: 5b998a75c7aef576ca3040ad7cd7a07082fddc9cd937b57e3d3e84d807124aba
4
- data.tar.gz: 29d5bc462c420c881eeeeca851146c74e8f6db1240de777d8e19ca0806274fdd
3
+ metadata.gz: fe2154ff56ce545931546a0f3fd9abb43b5515ff76eaee7c525af6066caff76e
4
+ data.tar.gz: 3abc9503558892fc6a00c23207f3e520ff5b6cf711ac5bc355d36a010f89acb5
5
5
  SHA512:
6
- metadata.gz: 71094a00ecce450e4e2edbbda1327cf2f794c52d5d64bd304b67ec5f2498e49020c0ed4f2a07bc9425d5df8929ebe2d809447ea4b52baca0eb6569af27fcf22a
7
- data.tar.gz: 1874d642a66cc7a2771fd78e92a2dc3e7aaea9ea520055cc2b7abe5bb2f9bf51dce5989e1498352411d134a371ec8b546ded1e8cb8e3d75b7408eaa340d06cbd
6
+ metadata.gz: e9a152c55f8e5e7db574bf2b66adb0f3cb046a4d1549f1c370bc02235f184663a0d2c691378a7ce818da0e20382b22694834b12d8fdab1e39d3126e7e2965f53
7
+ data.tar.gz: 9ffbd36708152e9006d335e6f76ed46db162dbcba8a8c23ab388f84fce2ed47507b8c39c0966ab5558955bb47490c90f988cf2f1fd7b346dc0bf17ddd6ffb4de
data/.gitignore CHANGED
@@ -11,3 +11,6 @@
11
11
  .rspec_status
12
12
  /.rakeTasks
13
13
  /.idea
14
+
15
+ # rbenv
16
+ .ruby-version
data/CHANGELOG.md CHANGED
@@ -1,6 +1,25 @@
1
1
  # Changelog
2
2
 
3
- *Release Date*: 2019/11/21
3
+ *Release Date*: 2020/01/13
4
+
5
+ ### 0.1.7 2020/01/20
6
+ * Adds a config option to allow explicit adapter setup.
7
+ * Adds an `after_initialize` rails hook to setup adapters.
8
+ * Remove topic from the list of Google Cloud adapter arguments.
9
+
10
+ ### 0.1.6 2020/01/17
11
+ * Allow topic to be explicitly set upon Google Cloud adapter initialization
12
+
13
+ ### 0.1.5 2020/01/13
14
+ * In case `graceful_error_handling` is set to off raise a generic `Stenotype::Error` on any exception in order to intercept a single error type in the client code.
15
+ * Adds an `at_exit` hook to flush the async message queue when using the library in async mode.
16
+
17
+ ### 0.1.4 2020/01/10
18
+ * Adds a new configuration option `graceful_error_handling` to suppress errors raised from the gem's internals yet logging the error to specified `config.logger`
19
+
20
+ ### 0.1.3: 2020/01/10
21
+ * Adds a new configuration option `logger` to use during error handling
22
+ * Adds a new config option `Stenotype.config.enabled`. If the option is set to false then event is not going to be published. The option is `true` by default.
4
23
 
5
24
  ### 0.1.2: 2019/12/10
6
25
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stenotype (0.1.2)
4
+ stenotype (0.1.7)
5
5
  activesupport (>= 5.0.0)
6
6
  google-cloud-pubsub (~> 1.0.0)
7
7
  spicery (>= 0.19.0, < 1.0)
@@ -75,7 +75,7 @@ GEM
75
75
  tablesalt (= 0.19.2)
76
76
  faker (1.9.6)
77
77
  i18n (>= 0.7)
78
- faraday (0.17.1)
78
+ faraday (0.17.3)
79
79
  multipart-post (>= 1.2, < 3)
80
80
  github-markup (3.0.4)
81
81
  globalid (0.4.2)
@@ -110,7 +110,7 @@ GEM
110
110
  multi_json (~> 1.11)
111
111
  os (>= 0.9, < 2.0)
112
112
  signet (~> 0.12)
113
- grpc (1.25.0-universal-darwin)
113
+ grpc (1.26.0-universal-darwin)
114
114
  google-protobuf (~> 3.8)
115
115
  googleapis-common-protos-types (~> 1.0)
116
116
  grpc-google-iam-v1 (0.6.9)
@@ -147,7 +147,7 @@ GEM
147
147
  coderay (~> 1.1.0)
148
148
  method_source (~> 0.9.0)
149
149
  public_suffix (4.0.1)
150
- rack (2.0.7)
150
+ rack (2.0.8)
151
151
  rack-test (1.1.0)
152
152
  rack (>= 1.0, < 3)
153
153
  rails (5.2.4)
data/README.md CHANGED
@@ -25,13 +25,17 @@ Or install it yourself as:
25
25
  Configuring the library is as simple as:
26
26
  ```ruby
27
27
  Stenotype.configure do |config|
28
+ config.enabled = true
28
29
  config.targets = [ # Supported targets
29
30
  Stenotype::Adapters::StdoutAdapter.new,
30
31
  Stenotype::Adapters::GoogleCloud.new
31
32
  ]
32
33
 
33
- config.uuid_generator = SecureRandom
34
- config.dispatcher = Stenotype::Dispatcher.new
34
+ config.uuid_generator = SecureRandom
35
+ config.dispatcher = Stenotype::Dispatcher.new
36
+ config.logger = Logger.new(STDOUT)
37
+ config.graceful_error_handling = true
38
+ config.auto_adapter_initialization = true
35
39
 
36
40
  config.google_cloud do |gc_config|
37
41
  gc_config.project_id = "google_cloud_project_id"
@@ -47,10 +51,22 @@ Stenotype.configure do |config|
47
51
  end
48
52
  ```
49
53
 
54
+ #### config.enabled
55
+
56
+ A flag checked upon emission of an event. Will prevent event emission if set to false. An event is emitted if set to true.
57
+
50
58
  #### config.targets
51
59
 
52
60
  Contain an array of targets for the events to be published to. Targets must implement method `#publish(event_data, **additional_arguments)`.
53
61
 
62
+ #### config.logger
63
+
64
+ Specifies a logger for messages and exceptions to be output to. If not set defaults to `Logger.new(STDOUT)`, otherwise a manually set logger is used.
65
+
66
+ #### config.graceful_error_handling
67
+
68
+ This flag if set to `true` is going to suppress all `StandardError`'s raised within a gem. Raises the error to the caller if set to `false`
69
+
54
70
  #### config.uuid_generator
55
71
 
56
72
  An object that must implement method `#uuid`. Used when an event is emitted to generate a unique id for each event.
@@ -83,6 +99,10 @@ Allows to enable/disable Rails ActionController extension
83
99
 
84
100
  Allows to enable/disable Rails ActiveJob extension
85
101
 
102
+ #### config.rails.auto_adapter_initialization
103
+
104
+ Controls whether the hook `auto_initialize!` is run for each adapter. If set to true `auto_initialize!` is invoked for every adapter. If false `auto_initialize!` is not run. For example for google cloud adapter this will instantiate `client` and `topic` objects before first publish. If set to false `client` and `topic` are lazy initialized.
105
+
86
106
  #### Configuring context handlers
87
107
 
88
108
  Each event is emitted in a context which might be an ActionController instance or an ActiveJob instance or potentially any other place. Context handlers are implemented as plain ruby classes, so before using them you must register them. By default a plain `Class` handler is registered when not used with any framework. In case Ruby on Rails is used, then there are two additional context handlers for `ActionController` and `ActiveJob` instances.
@@ -213,6 +233,14 @@ class CustomAdapter < Stenotype::Adapters::Base
213
233
  def publish(event_data, **additional_arguments)
214
234
  # custom publishing logic
215
235
  end
236
+
237
+ def flush!
238
+ # actions to be taken to flush the messages
239
+ end
240
+
241
+ def auto_initialize!
242
+ # actions to be taken to setup internal adapter state (client, endpoint, whatsoever)
243
+ end
216
244
  end
217
245
  ```
218
246
 
@@ -18,6 +18,9 @@ Rails.application.configure do
18
18
  # rails_modules.enable_active_job_ext = true
19
19
  # end
20
20
  #
21
+ # To enable or disable the library use the following config option:
22
+ #
23
+ # config.enabled = true # or false
21
24
  #
22
25
  # To make publishing possible one must specify a list of targets. You could use
23
26
  # StdoutAdapter for debug purposes before switching to a production publisher.
@@ -60,6 +63,14 @@ Rails.application.configure do
60
63
  #
61
64
  # config.dispatcher = Stenotype::Dispatcher
62
65
  #
66
+ # An option to suppress exception within a gem is available:
67
+ #
68
+ # config.graceful_error_handling = true
69
+ #
70
+ # To log errors a logger config option is available. Logger.new(STDOUT) is used by default.
71
+ #
72
+ # config.logger = Logger.new(STDOUT)
73
+ #
63
74
  # Add your own context handlers
64
75
  #
65
76
  # Stenotype::ContextHandlers.register Your::Custom::HandlerClass
@@ -35,12 +35,33 @@ module Stenotype
35
35
  #
36
36
  # This method is expected to be implemented by subclasses
37
37
  # @abstract
38
- # @raise [NotImplementedError] unless implemented in a subclass
38
+ # @raise {NotImplementedError} unless implemented in a subclass
39
39
  #
40
40
  def publish(_event_data, **_additional_attrs)
41
41
  raise NotImplementedError,
42
42
  "#{self.class.name} must implement method #publish"
43
43
  end
44
+
45
+ #
46
+ # Allows custom setup of the adapter. Noop by default
47
+ # @abstract
48
+ #
49
+ def auto_initialize!
50
+ # noop by default
51
+ end
52
+
53
+ #
54
+ # This method is expected to be implemented by subclasses. In case async
55
+ # publisher is used the process might end before the async queue of
56
+ # messages is processed, so this method is going to be used in a
57
+ # `at_exit` hook to flush the queue.
58
+ # @abstract
59
+ # @raise {NotImplementedError} unless implemented in a subclass
60
+ #
61
+ def flush!
62
+ raise NotImplementedError,
63
+ "#{self.class.name} must implement method #flush"
64
+ end
44
65
  end
45
66
  end
46
67
  end
@@ -56,6 +56,26 @@ module Stenotype
56
56
  end
57
57
  end
58
58
 
59
+ #
60
+ # Flushes the topic's async queue
61
+ #
62
+ def flush!
63
+ # a publisher might be uninitialized until the first event is published
64
+ return unless topic.async_publisher
65
+
66
+ topic.async_publisher.stop.wait!
67
+ end
68
+
69
+ #
70
+ # If not called both client and topic are lazy initialized on first call (if not
71
+ # passed to #initialize). #auto_initialize! is going to explicitly initialize
72
+ # both client and topic.
73
+ #
74
+ def auto_initialize!
75
+ client
76
+ topic
77
+ end
78
+
59
79
  private
60
80
 
61
81
  def publish_sync(event_data, **additional_attrs)
@@ -40,6 +40,13 @@ module Stenotype
40
40
  end
41
41
  end
42
42
 
43
+ #
44
+ # Does nothing
45
+ #
46
+ def flush!
47
+ # noop
48
+ end
49
+
43
50
  private
44
51
 
45
52
  def client
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nocov:
4
+ require "stenotype"
5
+
6
+ at_exit do
7
+ targets = Stenotype.config.targets
8
+ targets.each(&:flush!)
9
+ end
10
+ # :nocov:
@@ -6,6 +6,7 @@ module Stenotype
6
6
  #
7
7
  # @example Configuring the library
8
8
  # Stenotype.configure do |config|
9
+ # config.enabled = true
9
10
  # config.targets = [Target1.new, Target2.new]
10
11
  # config.uuid_generator = SecureRandom
11
12
  #
@@ -25,6 +26,18 @@ module Stenotype
25
26
  module Configuration
26
27
  extend Spicerack::Configurable
27
28
 
29
+ # @!attribute graceful_error_handling
30
+ # @return {true, false} a flag for suppressing error raised withing the gem
31
+
32
+ # @!attribute logger
33
+ # @return {Logger} a logger with default severity methods to output gem level messages
34
+
35
+ # @!attribute enabled
36
+ # @return {true, false} a flag indicating whether event emission is enabled
37
+
38
+ # @!attribute auto_adapter_initialization
39
+ # @return {true, false} enables/disables lazy initialization of adapters' clients
40
+
28
41
  # @!attribute targets
29
42
  # @return {Array<#publish>} a list of targets responding to method [#publish]
30
43
 
@@ -49,7 +62,6 @@ module Stenotype
49
62
  # @!attribute [rw] google_cloud.async
50
63
  # @return [true, false] GC publish mode, either async if true, sync if false
51
64
 
52
-
53
65
  # @!attribute [rw] rails
54
66
  # @return [NestedConfiguration] Rails configuration.
55
67
 
@@ -60,9 +72,12 @@ module Stenotype
60
72
  # @return [true, false] A flag of whether ActiveJob ext is enabled
61
73
 
62
74
  configuration_options do
75
+ option :graceful_error_handling, default: true
76
+ option :enabled, default: true
63
77
  option :targets, default: []
64
78
  option :dispatcher, default: Stenotype::Dispatcher
65
79
  option :uuid_generator, default: SecureRandom
80
+ option :logger
66
81
 
67
82
  nested :google_cloud do
68
83
  option :credentials, default: nil
@@ -74,11 +89,34 @@ module Stenotype
74
89
  nested :rails do
75
90
  option :enable_action_controller_ext, default: true
76
91
  option :enable_active_job_ext, default: true
92
+ option :auto_adapter_initialization, default: false
77
93
  end
78
94
  end
79
95
 
80
96
  module_function
81
97
 
98
+ #
99
+ # @example With default logger
100
+ # Stenotype.configure do |config|
101
+ # # config.logger = nil # logger not set manually
102
+ # end
103
+ # Stenotype.config.logger #=> `Logger.new(STDOUT)` instance
104
+ #
105
+ # @example With custom logger
106
+ # Stenotype.configure do |config|
107
+ # config.logger = custom_logger_instance
108
+ # end
109
+ # Stenotype.config.logger #=> custom_logger_instance
110
+ #
111
+ # @return [{Logger, CustomLogger}] a logger object. Logger.new(STDOUT) by
112
+ # default if another is not set during configuration
113
+ #
114
+ def logger
115
+ return config.logger if config.logger
116
+
117
+ config.logger || Logger.new(STDOUT)
118
+ end
119
+
82
120
  #
83
121
  # @example When at least one target is present
84
122
  # Stenotype.configure do |config|
@@ -93,6 +131,8 @@ module Stenotype
93
131
  # @raise {Stenotype::NoTargetsSpecifiedError} in case no targets are configured
94
132
  # @return {Array<#publish>} An array of targets implementing method [#publish]
95
133
  #
134
+ # @todo THIS NEVER GETS CALLED, needs a fix
135
+ #
96
136
  def targets
97
137
  return config.targets unless config.targets.empty?
98
138
 
@@ -18,9 +18,21 @@ module Stenotype
18
18
  # @return {Stenotype::Event} An instance of {Stenotype::Event}
19
19
  #
20
20
  def self.emit!(name, attributes = {}, eval_context: {}, dispatcher: Stenotype.config.dispatcher)
21
- event = new(name, attributes, eval_context: eval_context, dispatcher: dispatcher)
22
- event.emit!
23
- event
21
+ return unless Stenotype.config.enabled
22
+
23
+ begin
24
+ event = new(name, attributes, eval_context: eval_context, dispatcher: dispatcher)
25
+ event.emit!
26
+ event
27
+ rescue StandardError => exception
28
+ #
29
+ # @todo This is a temporary solution to enable conditional logger fetching
30
+ # needs a fix to use default Spicerack::Configuration functionality
31
+ #
32
+ Stenotype::Configuration.logger.error(exception)
33
+
34
+ raise Stenotype::Error unless Stenotype.config.graceful_error_handling
35
+ end
24
36
  end
25
37
 
26
38
  attr_reader :name, :attributes, :eval_context, :dispatcher
@@ -52,7 +64,19 @@ module Stenotype
52
64
  # event.emit! #=> Publishes the event to targets
53
65
  #
54
66
  def emit!
55
- dispatcher.publish(self)
67
+ return unless Stenotype.config.enabled
68
+
69
+ begin
70
+ dispatcher.publish(self)
71
+ rescue StandardError => exception
72
+ #
73
+ # @todo This is a temporary solution to enable conditional logger fetching
74
+ # needs a fix to use default Spicerack::Configuration functionality
75
+ #
76
+ Stenotype::Configuration.logger.error(exception)
77
+
78
+ raise Stenotype::Error unless Stenotype.config.graceful_error_handling
79
+ end
56
80
  end
57
81
  end
58
82
  end
@@ -18,6 +18,8 @@ module Stenotype
18
18
 
19
19
  config.stenotype = Stenotype.config
20
20
 
21
+ config.after_initialize { config.stenotype.targets.each(&:auto_initialize!) } if config.stenotype.rails.auto_adapter_initialization
22
+
21
23
  if config.stenotype.rails.enable_action_controller_ext
22
24
  ActiveSupport.on_load(:action_controller) do
23
25
  Stenotype::ContextHandlers.register Stenotype::ContextHandlers::Rails::Controller
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Stenotype
4
4
  # :nodoc:
5
- VERSION = "0.1.2"
5
+ VERSION = "0.1.8"
6
6
  # :nodoc:
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stenotype
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Kapitonov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-11 00:00:00.000000000 Z
11
+ date: 2020-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -267,6 +267,7 @@ files:
267
267
  - lib/stenotype/adapters/base.rb
268
268
  - lib/stenotype/adapters/google_cloud.rb
269
269
  - lib/stenotype/adapters/stdout_adapter.rb
270
+ - lib/stenotype/at_exit.rb
270
271
  - lib/stenotype/configuration.rb
271
272
  - lib/stenotype/context_handlers.rb
272
273
  - lib/stenotype/context_handlers/base.rb