dirty_pipeline 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
  SHA256:
3
- metadata.gz: e4eef96b779d46b3c749e665cf467f69577b8b1dc1bac3dd1e107be269619a3d
4
- data.tar.gz: 0a42107025850c20d9d2f4bfbeb5fb4b3fd42610c9eae3aacc3ba80e3600ad6d
3
+ metadata.gz: 52d7e43f1786f53222062344f5ba1c21473d384981a7cf6ffddaa1980d707252
4
+ data.tar.gz: 7453aa7396775663b8f040fe507ec251dabefbc8e1d5ac2255eee919c65d2943
5
5
  SHA512:
6
- metadata.gz: 9fcb6bcf6655ab497f97fcf0f72d54a46b4a12407d5d2567d6027442e2652d4c901f0ef1a5031de184c0714821c27f466511c0db803e072082bd0d121968cbf3
7
- data.tar.gz: '085965b95df37be6654977fb820b3d8937abd2097bc01a340119a8e99170206ce3670ffdcc2698723af7a484403fe76359b9b0e55548d6d11f1e2573c2ade88b'
6
+ metadata.gz: ea9d73885acedf923e33f1ace3e3649142a2ca1615a402a78ff93b216a412b369668f14139fd688fa225eea26120d6fd1e668cf1cb18cd8c3fd76f79e974436f
7
+ data.tar.gz: ba2e70358c97d8e4fbd5af668f1db770b1d99cf292b7d8d42d661420bff553978d9961b676cdc1b1083a6ce70229c1c572cc1f3a10db9ec212287cc6f20a01b2
@@ -24,7 +24,6 @@ Gem::Specification.new do |spec|
24
24
  # temporary dependency
25
25
  spec.add_runtime_dependency "sidekiq", "~> 5.0"
26
26
  spec.add_runtime_dependency "redis", "~> 4.0"
27
- spec.add_runtime_dependency "nanoid", "~> 0.2.0"
28
27
 
29
28
  spec.add_development_dependency "bundler", "~> 1.16"
30
29
  spec.add_development_dependency "rake", "~> 10.0"
@@ -35,7 +35,7 @@ module DirtyPipeline
35
35
 
36
36
  attr_reader :subject, :storage, :status, :uuid, :queue, :railway
37
37
  def initialize(subject, uuid: nil)
38
- @uuid = uuid || Nanoid.generate
38
+ @uuid = uuid || SecureRandom.uuid
39
39
  @subject = subject
40
40
  @storage = Storage.new(subject, self.class.pipeline_storage)
41
41
  @railway = Railway.new(subject, @uuid)
@@ -60,34 +60,29 @@ module DirtyPipeline
60
60
  reset!
61
61
  end
62
62
 
63
+ # FIXME operation :call - argument
63
64
  def chain(*args)
64
65
  railway[:call] << Event.create(*args, tx_id: @uuid)
65
66
  self
66
67
  end
67
68
 
68
69
  def call
69
- return self if (serialized_event = railway.next).nil?
70
- execute(load_event(serialized_event), tx_method: :call)
70
+ return self if (enqueued_event = railway.next).nil?
71
+ execute(load_event(enqueued_event))
71
72
  end
72
73
  alias :call_next :call
73
74
 
74
75
  def clean
76
+ finished = railway.queue.to_a.empty?
77
+ finished &&= railway.queue.processing_event.nil?
78
+ return self if finished
75
79
  railway.switch_to(:undo)
76
- call_next
77
- self
80
+ call
78
81
  end
79
82
 
80
83
  def retry
81
- return unless (event = load_event(railway.queue.processing_event))
82
- execute(event, tx_id: :retry)
83
- end
84
-
85
- def schedule_cleanup
86
- schedule("cleanup", cleanup_delay)
87
- end
88
-
89
- def schedule_retry
90
- schedule("retry", retry_delay)
84
+ return self if (enqueued_event = railway.queue.processing_event).nil?
85
+ execute(load_event(enqueued_event), attempt_retry: true)
91
86
  end
92
87
 
93
88
  def schedule(operation, delay = nil)
@@ -105,6 +100,12 @@ module DirtyPipeline
105
100
  end
106
101
  end
107
102
 
103
+ def cleanup_delay; self.class.cleanup_delay || DEFAULT_CLEANUP_DELAY; end
104
+ def schedule_cleanup; schedule("cleanup", cleanup_delay); end
105
+
106
+ def retry_delay; self.class.retry_delay || DEFAULT_RETRY_DELAY; end
107
+ def schedule_retry; schedule("retry", retry_delay); end
108
+
108
109
  def when_success
109
110
  yield(status.data, self) if status.success?
110
111
  self
@@ -117,8 +118,11 @@ module DirtyPipeline
117
118
 
118
119
  private
119
120
 
120
- def execute(event, tx_method:)
121
- transaction(event).public_send(tx_method) do |destination, action, *args|
121
+
122
+ def execute(event, attempt_retry: false)
123
+ attempt_retry ? event.attempt_retry! : event.start!
124
+
125
+ Transaction.new(self, event).call do |destination, action, *args|
122
126
  state_changes = process_action(action, event, *args)
123
127
  next if status.failure?
124
128
  Success(event, state_changes, destination)
@@ -140,45 +144,28 @@ module DirtyPipeline
140
144
  nil
141
145
  end
142
146
  rescue => exception
143
- @status = Status.failure(exception, tag: :exception)
147
+ Failure(event, exception, type: :exception)
144
148
  raise
145
149
  end
146
150
 
147
151
  def run_operation(action, event, *args)
148
- return unless action.respond_to?(operation = railway.operation)
152
+ return unless action.respond_to?(operation = railway.active)
149
153
  action.public_send(operation, event, self, *args)
150
154
  end
151
155
 
152
156
  def interupt_on_error(event)
153
- return unless (fail_cause = catch(:fail_operation) { yield; nil })
154
- Failure(event, fail_cause)
157
+ return unless (fail_cause = catch(:fail_transition) { yield; nil })
158
+ Failure(event, fail_cause, type: :error)
155
159
  end
156
160
 
157
161
  def find_subject_args
158
162
  subject.id
159
163
  end
160
164
 
161
- def retry_delay
162
- self.class.retry_delay || DEFAULT_RETRY_DELAY
163
- end
164
-
165
- def cleanup_delay
166
- self.class.cleanup_delay || DEFAULT_CLEANUP_DELAY
167
- end
168
-
169
- def transaction(event)
170
- Transaction.new(self, event)
171
- end
172
-
173
- def Failure(event, cause)
165
+ def Failure(event, cause, type:)
174
166
  railway.switch_to(:undo)
175
- if cause.eql?(:abort)
176
- event.abort!
177
- @status = Status.failure(subject, tag: :aborted)
178
- else
179
- event.failure!
180
- @status = Status.failure(cause, tag: :error)
181
- end
167
+ event.failure!
168
+ @status = Status.failure(cause, tag: type)
182
169
  throw :abort_transaction, true
183
170
  end
184
171
 
@@ -12,7 +12,8 @@ module DirtyPipeline
12
12
  def self.create(transition, *args, tx_id:)
13
13
  new(
14
14
  data: {
15
- "uuid" => Nanoid.generate,
15
+ # FIXME: SecureRandom
16
+ "uuid" => SecureRandom.uuid,
16
17
  "transaction_uuid" => tx_id,
17
18
  "transition" => transition,
18
19
  "args" => args,
@@ -65,6 +65,7 @@ module DirtyPipeline
65
65
  (store.keys & %w(status events errors state)).size.eql?(4)
66
66
  end
67
67
 
68
+ # FIXME: save! - configurable method
68
69
  def save!
69
70
  subject.send("#{field}=", store)
70
71
  subject.save!
@@ -9,18 +9,6 @@ module DirtyPipeline
9
9
  end
10
10
 
11
11
  def call
12
- event.start!
13
- with_transaction { |*targs| yield(*targs) }
14
- end
15
-
16
- def retry
17
- event.attempt_retry!
18
- with_transaction { |*targs| yield(*targs) }
19
- end
20
-
21
- private
22
-
23
- def with_transaction
24
12
  pipeline.schedule_cleanup
25
13
 
26
14
  destination, action, max_attempts_count =
@@ -29,6 +17,7 @@ module DirtyPipeline
29
17
 
30
18
  storage.commit!(event)
31
19
 
20
+ # FIXME: make configurable, now - hardcoded to AR API
32
21
  subject.transaction(requires_new: true) do
33
22
  with_abort_handling { yield(destination, action, *event.args) }
34
23
  end
@@ -43,6 +32,8 @@ module DirtyPipeline
43
32
  storage.commit!(event)
44
33
  end
45
34
 
35
+ private
36
+
46
37
  def with_abort_handling
47
38
  return unless catch(:abort_transaction) { yield; nil }
48
39
  event.abort! unless event.abort?
@@ -1,11 +1,7 @@
1
1
  module DirtyPipeline
2
2
  class Transition
3
- def Abort()
4
- throw :fail_operation, :abort
5
- end
6
-
7
3
  def Error(error)
8
- throw :fail_operation, error
4
+ throw :fail_transition, error
9
5
  end
10
6
 
11
7
  def Success(changes = nil)
@@ -1,3 +1,3 @@
1
1
  module DirtyPipeline
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
3
3
  end
@@ -1,3 +1,4 @@
1
+ # FIXME: Abstract worker (see ActiveJob)
1
2
  require 'sidekiq'
2
3
  module DirtyPipeline
3
4
  class Worker
@@ -17,7 +18,7 @@ module DirtyPipeline
17
18
 
18
19
  case operation
19
20
  when "cleanup"
20
- pipeline.clean
21
+ return pipeline.clean
21
22
  when "retry"
22
23
  return pipeline.retry
23
24
  end
@@ -1,5 +1,5 @@
1
1
  require "dirty_pipeline/version"
2
- require "nanoid"
2
+ require "securerandom"
3
3
 
4
4
  module DirtyPipeline
5
5
  require_relative "dirty_pipeline/ext/camelcase.rb"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dirty_pipeline
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
  - Sergey Dolganov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-03 00:00:00.000000000 Z
11
+ date: 2018-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '4.0'
41
- - !ruby/object:Gem::Dependency
42
- name: nanoid
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: 0.2.0
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: 0.2.0
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: bundler
57
43
  requirement: !ruby/object:Gem::Requirement