pub_sub_model_sync 1.6.4 → 1.7.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
  SHA256:
3
- metadata.gz: 79bf4750adcd186e0c14bd8c092012a6a9455dd6affcabf603e72e9a011f9bb1
4
- data.tar.gz: 9d8efba465a451c7ffe3bd04400cd3efa2e6dfee31fa45885035da744ce88104
3
+ metadata.gz: 4dbf0fae81e981722c2a5dc1023dc3db9c369cfcdc5558ef727e59b1d6c87a8a
4
+ data.tar.gz: 340032eb2b9d47de47aa4e57c3db389c5571280df75a92fee96b52de2cc8c19e
5
5
  SHA512:
6
- metadata.gz: f5ab4a0ffdc1b4e2eea91f6889211b04150f0a1857e4d0bc50ab6f1a9894811c0afedb22f3bbce84ee14bf668d24801e98cf512ef657b56a2c2bb4333deb78e7
7
- data.tar.gz: 28bf0020235c4b7e6a6b8d54758daa0e72d2a61a55014bfa0646f028cac8227fed6c68df2f4a46effa447854910c2010258280c325f4aa8ea17bfe6cd2d50072
6
+ metadata.gz: f65eac76d6e247c89d863f75476539bf75324b12c510bdce2eaedc9667168381df82a39f3a2ee24c6da1c39b447159981512d9c744233b835c7000a95b514ac1
7
+ data.tar.gz: 1e549a6a809ecb162eaf6c4101a9a379f2ea2a1cccacb975854de02c1ddf928a54a05b9c432cdd26ac31863fcf5cffbefaa3baf0753f4301054fc509167d3316
@@ -1,43 +1,11 @@
1
+ name: Create release from tag
1
2
  on:
2
3
  push:
3
- tags: # triggered once a git tag is published
4
+ tags: # Create a release once a git tag is published
4
5
  - '*'
5
6
 
6
- name: Create Release
7
-
8
7
  jobs:
9
- build:
10
- name: Create Release
11
- runs-on: ubuntu-latest
12
- steps:
13
- - name: Checkout code
14
- uses: actions/checkout@v2
15
- with:
16
- fetch-depth: 0
17
-
18
- # Changelog action adaptations
19
- - name: Create required package.json
20
- run: test -f package.json || echo '{}' >package.json
21
- - name: Detect Previous Tag (action not detecting very well)
22
- run: echo "::set-output name=previous_tag::$(git describe --abbrev=0 --tags `git rev-list --tags --skip=1 --max-count=1`)"
23
- id: tag_checker
24
-
25
- - name: Generate Changelog
26
- uses: scottbrenner/generate-changelog-action@master
27
- id: Changelog
28
- with:
29
- from-tag: ${{steps.tag_checker.outputs.previous_tag}}
30
- to-tag: HEAD
31
-
32
- - name: Create Release
33
- id: create_release
34
- uses: actions/create-release@latest
35
- env:
36
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
37
- with:
38
- tag_name: ${{ github.ref }}
39
- release_name: Release ${{ github.ref }}
40
- body: |
41
- ${{ steps.Changelog.outputs.changelog }}
42
- draft: false
43
- prerelease: false
8
+ release-builder:
9
+ uses: owen2345/reusable-ci-cd-actions/.github/workflows/release_builder_from_tag.yml@main
10
+ with:
11
+ commit_mode: true # If true, then fetches all commit titles from all changes. If false, then fetches only the PR titles from all changes.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pub_sub_model_sync (1.6.4)
4
+ pub_sub_model_sync (1.7.0)
5
5
  rails
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -335,7 +335,8 @@ Note: To reduce Payload size, some header info are not delivered (Enable debug m
335
335
  payload.publish! # publishes notification data. It raises exception if fails and does not call ```:on_error_publishing``` callback
336
336
  payload.publish # publishes notification data. On error does not raise exception but calls ```:on_error_publishing``` callback
337
337
  payload.process! # process a notification data. It raises exception if fails and does not call ```.on_error_processing``` callback
338
- payload.publish # process a notification data. It does not raise exception if fails but calls ```.on_error_processing``` callback
338
+ payload.process # process a notification data. It does not raise exception if fails but calls ```.on_error_processing``` callback
339
+ payload.retry_publish! # allows to retry publishing a failed payload (All callbacks are ignored)
339
340
  ```
340
341
 
341
342
  ## **Transactions**
@@ -538,7 +539,7 @@ config.debug = true
538
539
  (Proc) => called before publishing a notification (:cancel can be returned to skip publishing)
539
540
  - ```.on_after_publish = ->(payload) { puts payload }```
540
541
  (Proc) => called after publishing a notification
541
- - ```.on_error_publish = ->(exception, {payload:}) { payload.delay(...).publish! }```
542
+ - ```.on_error_publish = ->(exception, {payload:}) { payload.delay(...).retry_publish! }```
542
543
  (Proc) => called when failed publishing a notification (delayed_job or similar can be used for retrying)
543
544
  - ```.skip_cache = false```
544
545
  (true/false*) => Allow to skip payload optimization (cache settings)
@@ -581,7 +582,7 @@ config.debug = true
581
582
  end
582
583
 
583
584
  PubSubModelSync::Config.on_error_publish = lambda do |_e, data|
584
- PubSubRecovery.perform_async(data[:payload].to_h, :publish!)
585
+ PubSubRecovery.perform_async(data[:payload].to_h, :retry_publish!)
585
586
  end
586
587
  PubSubModelSync::Config.on_error_processing = lambda do |_e, data|
587
588
  PubSubRecovery.perform_async(data[:payload].to_h, :process!)
@@ -21,12 +21,14 @@ module PubSubModelSync
21
21
  log("No subscribers found for #{payload_info}", :warn) if config.debug && subscribers.empty?
22
22
  subscribers.each(&method(:run_subscriber))
23
23
  rescue => e
24
- notify_error(e)
24
+ print_error(e)
25
25
  raise
26
26
  end
27
27
 
28
28
  def process
29
- process! rescue nil # rubocop:disable Style/RescueModifier
29
+ process!
30
+ rescue => e
31
+ config.on_error_processing.call(e, { payload: payload })
30
32
  end
31
33
 
32
34
  private
@@ -51,14 +53,10 @@ module PubSubModelSync
51
53
  end
52
54
 
53
55
  # @param error (StandardError, Exception)
54
- def notify_error(error)
55
- error_msg = 'Error processing message: '
56
+ def print_error(error)
57
+ error_msg = 'Error processing message:'
56
58
  error_details = [payload, error.message, error.backtrace]
57
- res = config.on_error_processing.call(error, { payload: payload })
58
- log("#{error_msg} #{error_details}", :error) if res != :skip_log
59
- rescue => e
60
59
  log("#{error_msg} #{error_details}", :error)
61
- raise(e)
62
60
  end
63
61
 
64
62
  # @param error [StandardError]
@@ -80,6 +80,17 @@ module PubSubModelSync
80
80
  current_transaction ? current_transaction.add_payload(payload) : connector_publish(payload)
81
81
  block&.call
82
82
  payload
83
+ rescue => e
84
+ print_error(e, payload)
85
+ raise
86
+ end
87
+
88
+ # Similar to :publish! method but ignores the error if failed
89
+ # @return Payload
90
+ def publish(payload, &block)
91
+ publish!(payload, &block)
92
+ rescue => e
93
+ config.on_error_publish.call(e, { payload: payload })
83
94
  end
84
95
 
85
96
  def connector_publish(payload)
@@ -89,22 +100,13 @@ module PubSubModelSync
89
100
  config.on_after_publish.call(payload)
90
101
  end
91
102
 
92
- # Similar to :publish! method
93
- # Notifies error via :on_error_publish instead of raising error
94
- # @return Payload
95
- def publish(payload, &block)
96
- publish!(payload, &block)
97
- rescue => e
98
- notify_error(e, payload)
99
- end
100
-
101
103
  private
102
104
 
103
105
  def ensure_publish(payload)
104
106
  cache_klass = PubSubModelSync::PayloadCacheOptimizer
105
- cancelled = payload.cache_settings ? cache_klass.new(payload).call == :already_sent : false
106
- cancelled ||= config.on_before_publish.call(payload) == :cancel
107
- log_msg = "Publish cancelled by config.on_before_publish or cache checker: #{[payload]}"
107
+ cancelled_cache = payload.cache_settings ? cache_klass.new(payload).call == :already_sent : false
108
+ cancelled = cancelled_cache || config.on_before_publish.call(payload) == :cancel
109
+ log_msg = "Publish cancelled by #{cancelled_cache ? 'cache checker' : 'config.on_before_publish'}: #{[payload]}"
108
110
  log(log_msg) if config.debug && cancelled
109
111
  !cancelled
110
112
  end
@@ -124,10 +126,11 @@ module PubSubModelSync
124
126
  !cancelled
125
127
  end
126
128
 
127
- def notify_error(exception, payload)
128
- info = [payload, exception.message, exception.backtrace]
129
- res = config.on_error_publish.call(exception, { payload: payload })
130
- log("Error publishing: #{info}", :error) if res != :skip_log
129
+ # @param error (StandardError, Exception)
130
+ def print_error(error, payload)
131
+ error_msg = 'Error publishing:'
132
+ error_details = [payload, error.message, error.backtrace]
133
+ log("#{error_msg} #{error_details}", :error)
131
134
  end
132
135
 
133
136
  def define_transaction_key(payload)
@@ -43,6 +43,10 @@ module PubSubModelSync
43
43
  yield(OpenStruct.new(succeeded?: true)) if block_given?
44
44
  end
45
45
 
46
+ def resume_publish(_ordering_key)
47
+ true
48
+ end
49
+
46
50
  def enable_message_ordering!
47
51
  true
48
52
  end
@@ -72,15 +72,18 @@ module PubSubModelSync
72
72
  # Publish payload to pubsub
73
73
  # (If error will raise exception and wont call on_error_publish callback)
74
74
  def publish!
75
- klass = PubSubModelSync::MessagePublisher
76
- klass.publish!(self)
75
+ PubSubModelSync::MessagePublisher.publish!(self)
77
76
  end
78
77
 
79
78
  # Publish payload to pubsub
80
79
  # (If error will call on_error_publish callback)
81
80
  def publish
82
- klass = PubSubModelSync::MessagePublisher
83
- klass.publish(self)
81
+ PubSubModelSync::MessagePublisher.publish(self)
82
+ end
83
+
84
+ # allows to retry publishing a failed payload
85
+ def retry_publish!
86
+ PubSubModelSync::MessagePublisher.connector_publish(self)
84
87
  end
85
88
 
86
89
  # @param attr_keys (Array<Symbol>) List of attributes to be excluded from payload
@@ -19,14 +19,14 @@ module PubSubModelSync
19
19
  return payload if cache_disabled?
20
20
  return :already_sent if previous_payload_data == payload.data
21
21
 
22
- optimize_payload if optimization_enabled?
22
+ optimize_payload if optimization_allowed?
23
23
  Rails.cache.write(cache_key, backup_data, expires_in: 1.week)
24
24
  payload
25
25
  end
26
26
 
27
27
  private
28
28
 
29
- def optimization_enabled?
29
+ def optimization_allowed?
30
30
  previous_payload_data && payload.cache_settings.is_a?(Hash)
31
31
  end
32
32
 
@@ -35,11 +35,7 @@ module PubSubModelSync
35
35
  def publish(payload)
36
36
  p_topic_names = Array(payload.headers[:topic_name] || config.default_topic_name)
37
37
  message_topics = p_topic_names.map(&method(:find_topic))
38
- message_topics.each do |topic|
39
- topic.publish_async(encode_payload(payload), message_headers(payload)) do |res|
40
- raise StandardError, 'Failed to publish the message.' unless res.succeeded?
41
- end
42
- end
38
+ message_topics.each { |topic| publish_to_topic(topic, payload) }
43
39
  end
44
40
 
45
41
  def stop
@@ -56,6 +52,19 @@ module PubSubModelSync
56
52
  topics[topic_name] || publish_topics[topic_name] || init_topic(topic_name, only_publish: true)
57
53
  end
58
54
 
55
+ def publish_to_topic(topic, payload)
56
+ retries ||= 0
57
+ topic.publish_async(encode_payload(payload), message_headers(payload)) do |res|
58
+ raise StandardError, "Failed to publish the message. #{res.error}" unless res.succeeded?
59
+ end
60
+ rescue Google::Cloud::PubSub::OrderingKeyError => e
61
+ raise if (retries += 1) > 1
62
+
63
+ log("Resuming ordering_key and retrying OrderingKeyError for #{payload.headers[:uuid]}: #{e.message}")
64
+ topic.resume_publish(message_headers(payload)[:ordering_key])
65
+ retry
66
+ end
67
+
59
68
  # @param only_publish (Boolean): if false is used to listen and publish messages
60
69
  # @return (Topic): returns created or loaded topic
61
70
  def init_topic(topic_name, only_publish: false)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PubSubModelSync
4
- VERSION = '1.6.4'
4
+ VERSION = '1.7.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pub_sub_model_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.4
4
+ version: 1.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Owen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-11-29 00:00:00.000000000 Z
11
+ date: 2022-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails