dirty_pipeline 0.8.1 → 0.8.2

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: 9f3fd5ee73f6187ea39daa3d7b88371c84f3c6ca848b7ba65f58b7c8876c55b5
4
- data.tar.gz: ab18cf256c02e3851a87c093523bffe204bc25bba6a31be8027a923ddbebaa69
3
+ metadata.gz: 30736e6846e314622b7be4b807e6d7c6c4adddec24619d4d6cc13c5e88dc38d5
4
+ data.tar.gz: 30fde8fe371d9140d8b12f937205cf099df36399fbcc2f4d7b17a5bc8faa5385
5
5
  SHA512:
6
- metadata.gz: a818876d704b6dbb40ec6a0bad6ac45fb38a40ad87fce41859ea5cc2a29ddb8d72067aefdb63d16d888c6cef0fcd840f3b65ca00b0fc810133b081ed10bd25f7
7
- data.tar.gz: e6e014a6f8b37f55a688c5797c49287642531d4cc3dc2373688936b1e9bc0c2e7263947e9cbdcf6e9e152294cc0cf6c34f24e5835bad782c5bd497c7960f897e
6
+ metadata.gz: fccab6567a59465c945f3d2ecf82dd960e813d24defb690fee1da40d35ea0935ce3d3f47d6fb4838581c45ce527ce9502c17c812cb01d3bdf357dd9e2cdb5361
7
+ data.tar.gz: 9349d42d38f1c3c7e92b523901466859d0fd1d717e9c40d37f29237a4aa52024102d128aac83fbb8745f6a534fff38ea8409d95ca81caea251d3d211a179cd4b
@@ -32,6 +32,14 @@ module DirtyPipeline
32
32
  fail NotImplementedError
33
33
  end
34
34
 
35
+ def self.with_postgres_transaction
36
+ with_postgres do |conn|
37
+ conn.transaction do |transaction_conn|
38
+ yield transaction_conn
39
+ end
40
+ end
41
+ end
42
+
35
43
  # def self.with_postgres
36
44
  # yield(ActiveRecord::Base.connection.raw_connection)
37
45
  # ensure
@@ -47,9 +47,9 @@ module DirtyPipeline
47
47
 
48
48
  def clear!
49
49
  with_postgres do |c|
50
- c.transaction do
51
- c.exec(DELETE_ACTIVE, [active_event_key])
52
- c.exec(DELETE_EVENTS, [events_queue_key])
50
+ c.transaction do |tc|
51
+ tc.exec(DELETE_ACTIVE, [active_event_key])
52
+ tc.exec(DELETE_EVENTS, [events_queue_key])
53
53
  end
54
54
  end
55
55
  end
@@ -71,9 +71,7 @@ module DirtyPipeline
71
71
  SQL
72
72
  def push(event)
73
73
  with_postgres do |c|
74
- c.transaction do
75
- c.exec(PUSH_EVENT, [events_queue_key, pack(event)])
76
- end
74
+ c.exec(PUSH_EVENT, [events_queue_key, pack(event)])
77
75
  end
78
76
 
79
77
  self
@@ -85,9 +83,7 @@ module DirtyPipeline
85
83
  SQL
86
84
  def unshift(event)
87
85
  with_postgres do |c|
88
- c.transaction do
89
- c.exec(UNSHIFT_EVENT, [events_queue_key, pack(event)])
90
- end
86
+ c.exec(UNSHIFT_EVENT, [events_queue_key, pack(event)])
91
87
  end
92
88
  self
93
89
  end
@@ -109,14 +105,14 @@ module DirtyPipeline
109
105
  SQL
110
106
  def pop
111
107
  with_postgres do |c|
112
- c.transaction do
108
+ c.transaction do |tc|
113
109
  event_id, raw_event =
114
- PG.multi(c.exec(SELECT_LAST_EVENT, [events_queue_key]))
110
+ PG.multi(tc.exec(SELECT_LAST_EVENT, [events_queue_key]))
115
111
  if raw_event.nil?
116
- c.exec(DELETE_ACTIVE_EVENT, [active_event_key])
112
+ tc.exec(DELETE_ACTIVE_EVENT, [active_event_key])
117
113
  else
118
- c.exec(DELETE_EVENT, [events_queue_key, event_id])
119
- c.exec(SET_EVENT_ACTIVE, [active_event_key, raw_event])
114
+ tc.exec(DELETE_EVENT, [events_queue_key, event_id])
115
+ tc.exec(SET_EVENT_ACTIVE, [active_event_key, raw_event])
120
116
  end
121
117
  unpack(raw_event)
122
118
  end
@@ -37,6 +37,10 @@ module DirtyPipeline
37
37
  ]
38
38
  end
39
39
 
40
+ def with_postgres(&block)
41
+ DirtyPipeline.with_postgres(&block)
42
+ end
43
+
40
44
  DELETE_OPERATION = <<~SQL
41
45
  DELETE FROM dp_active_operations WHERE key = $1;
42
46
  SQL
@@ -45,10 +49,10 @@ module DirtyPipeline
45
49
  SQL
46
50
  def clear!
47
51
  @queues.values.each(&:clear!)
48
- DirtyPipeline.with_postgres do |c|
49
- c.transaction do
50
- c.exec DELETE_OPERATION, [active_operation_key]
51
- c.exec DELETE_TRANSACTION, [active_transaction_key]
52
+ with_postgres do |c|
53
+ c.transaction do |tc|
54
+ tc.exec DELETE_OPERATION, [active_operation_key]
55
+ tc.exec DELETE_TRANSACTION, [active_transaction_key]
52
56
  end
53
57
  end
54
58
  end
@@ -76,10 +80,10 @@ module DirtyPipeline
76
80
  raise ArgumentError unless DEFAULT_OPERATIONS.include?(name.to_s)
77
81
  return if name.to_s == active
78
82
 
79
- DirtyPipeline.with_postgres do |c|
80
- c.transaction do
81
- c.exec(SWITCH_OPERATION, [active_operation_key, name])
82
- end
83
+ with_postgres do |c|
84
+ c.exec('START TRANSACTION;')
85
+ c.exec(SWITCH_OPERATION, [active_operation_key, name])
86
+ c.exec('COMMIT;')
83
87
  end
84
88
  end
85
89
 
@@ -87,7 +91,7 @@ module DirtyPipeline
87
91
  SELECT name FROM dp_active_operations WHERE key = $1;
88
92
  SQL
89
93
  def active
90
- DirtyPipeline.with_postgres do |c|
94
+ with_postgres do |c|
91
95
  PG.single c.exec(SELECT_OPERATION, [active_operation_key])
92
96
  end
93
97
  end
@@ -97,7 +101,7 @@ module DirtyPipeline
97
101
  SELECT name FROM dp_active_transactions WHERE key = $1;
98
102
  SQL
99
103
  def running_transaction
100
- DirtyPipeline.with_postgres do |c|
104
+ with_postgres do |c|
101
105
  PG.single c.exec(SELECT_TRANSACTION, [active_transaction_key])
102
106
  end
103
107
  end
@@ -128,10 +132,10 @@ module DirtyPipeline
128
132
  SQL
129
133
  def start_transaction!
130
134
  switch_to(DEFAULT_OPERATIONS.first) unless active
131
- DirtyPipeline.with_postgres do |c|
132
- c.transaction do
133
- c.exec(SWITCH_TRANSACTION, [active_transaction_key, @tx_id])
134
- end
135
+ with_postgres do |c|
136
+ c.exec('START TRANSACTION;')
137
+ c.exec(SWITCH_TRANSACTION, [active_transaction_key, @tx_id])
138
+ c.exec('COMMIT;')
135
139
  end
136
140
  end
137
141
 
@@ -39,6 +39,10 @@ module DirtyPipeline
39
39
  raise InvalidPipelineStorage, store unless valid_store?
40
40
  end
41
41
 
42
+ def with_postgres(&block)
43
+ DirtyPipeline.with_postgres(&block)
44
+ end
45
+
42
46
  def reset!
43
47
  reset
44
48
  save!
@@ -57,16 +61,14 @@ module DirtyPipeline
57
61
  def commit!(event)
58
62
  store["status"] = event.destination if event.destination
59
63
  store["state"].merge!(event.changes) unless event.changes.to_h.empty?
60
- DirtyPipeline.with_postgres do |c|
61
- data, error = {}, {}
62
- data = event.data.to_h if event.data.respond_to?(:to_h)
63
- error = event.error.to_h if event.error.respond_to?(:to_h)
64
- c.transaction do
65
- c.exec(
66
- SAVE_EVENT,
67
- [event.id, subject_key, JSON.dump(data), JSON.dump(error)]
68
- )
69
- end
64
+ data, error = {}, {}
65
+ data = event.data.to_h if event.data.respond_to?(:to_h)
66
+ error = event.error.to_h if event.error.respond_to?(:to_h)
67
+ with_postgres do |c|
68
+ c.exec(
69
+ SAVE_EVENT,
70
+ [event.id, subject_key, JSON.dump(data), JSON.dump(error)]
71
+ )
70
72
  end
71
73
  save!
72
74
  end
@@ -76,7 +78,7 @@ module DirtyPipeline
76
78
  WHERE uuid = $1 AND context = $2;
77
79
  SQL
78
80
  def find_event(event_id)
79
- DirtyPipeline.with_postgres do |c|
81
+ with_postgres do |c|
80
82
  found_event, found_error =
81
83
  PG.multi(c.exec(FIND_EVENT, [event_id, subject_key]))
82
84
  return unless found_event
@@ -1,3 +1,3 @@
1
1
  module DirtyPipeline
2
- VERSION = "0.8.1"
2
+ VERSION = "0.8.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dirty_pipeline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Dolganov