transaction 0.1.7 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +17 -4
- data/CHANGELOG.MD +11 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +16 -2
- data/README.md +66 -14
- data/lib/transaction.rb +35 -14
- data/lib/transaction/helper.rb +13 -0
- data/lib/transaction/version.rb +1 -1
- data/transaction.gemspec +9 -2
- metadata +12 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b67f5a6260713f92024cc80f578c76ae953116972133664e12716eb52ce2cc28
|
4
|
+
data.tar.gz: d2c60611c7a5ed2d4f9f3e7b5565bcb1582f27985f7fa6e669569ad3320f3be5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea91a9508404c5408e8896b4810bdd428e60e71a94eba03dc41aa2158fa0429ac9672ae35370af55d1a42d6e7f41319c6ff7d5969baa5d76ec3bb82565e38b6f
|
7
|
+
data.tar.gz: 4c1953a76f3c4895068a9126bd1f2f084769090c4bb5ba0b9f4f3d909f8e06477cbcbc0a1f90ab887287d7e770b03f0e99211f1e09891feba5e899a8e7f483d5
|
data/.gitignore
CHANGED
@@ -1,11 +1,24 @@
|
|
1
|
-
|
2
|
-
/.
|
1
|
+
# Ignore bundler config.
|
2
|
+
/.bundle
|
3
|
+
|
4
|
+
# Ignore Mac of tempfiles
|
5
|
+
.DS_Store
|
6
|
+
._.DS_Store
|
7
|
+
|
8
|
+
# Ignore yard docs
|
9
|
+
doc
|
10
|
+
.yardoc/*
|
3
11
|
/_yardoc/
|
12
|
+
|
13
|
+
# Ignore all logfiles and tempfiles.
|
14
|
+
/log/*
|
15
|
+
/tmp/*
|
16
|
+
!/log/.keep
|
17
|
+
!/tmp/.keep
|
18
|
+
|
4
19
|
/coverage/
|
5
|
-
/doc/
|
6
20
|
/pkg/
|
7
21
|
/spec/reports/
|
8
|
-
/tmp/
|
9
22
|
|
10
23
|
# rspec failure tracking
|
11
24
|
.rspec_status
|
data/CHANGELOG.MD
CHANGED
@@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
5
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
+
## [1.0.0] - 2019-09-11
|
8
|
+
|
9
|
+
### Added
|
10
|
+
- Support for PubSub.
|
11
|
+
- Setter/getter for pubsub client.
|
12
|
+
- `trigger_event!` to push messages to pubsub client.
|
13
|
+
- Unit tests for pubsub integration.
|
14
|
+
|
15
|
+
### Changed
|
16
|
+
- `finish!` params. (Takes status, clear and data)
|
17
|
+
|
7
18
|
## [0.1.7] - 2019-07-19
|
8
19
|
|
9
20
|
### Added
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,16 +1,28 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
transaction (0.
|
4
|
+
transaction (1.0.0)
|
5
5
|
redis (>= 4.0.2)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
+
coderay (1.1.2)
|
10
11
|
diff-lcs (1.3)
|
11
12
|
docile (1.3.2)
|
13
|
+
httpclient (2.8.3)
|
12
14
|
json (2.2.0)
|
13
|
-
|
15
|
+
method_source (0.9.2)
|
16
|
+
multi_json (1.13.1)
|
17
|
+
pry (0.12.2)
|
18
|
+
coderay (~> 1.1.0)
|
19
|
+
method_source (~> 0.9.0)
|
20
|
+
pusher (1.3.2)
|
21
|
+
httpclient (~> 2.7)
|
22
|
+
multi_json (~> 1.0)
|
23
|
+
pusher-signature (~> 0.1.8)
|
24
|
+
pusher-signature (0.1.8)
|
25
|
+
rake (12.3.3)
|
14
26
|
redis (4.1.2)
|
15
27
|
rspec (3.8.0)
|
16
28
|
rspec-core (~> 3.8.0)
|
@@ -36,6 +48,8 @@ PLATFORMS
|
|
36
48
|
|
37
49
|
DEPENDENCIES
|
38
50
|
bundler (~> 2.0)
|
51
|
+
pry
|
52
|
+
pusher (= 1.3.2)
|
39
53
|
rake (~> 12.3)
|
40
54
|
rspec (~> 3.0)
|
41
55
|
simplecov
|
data/README.md
CHANGED
@@ -7,10 +7,18 @@
|
|
7
7
|
|
8
8
|
# Transaction
|
9
9
|
|
10
|
-
Transaction is a small library which helps track status of running/upcoming tasks. These tasks can be a cron job, background jobs or a simple method. Any task can be plugged into a transaction block.
|
10
|
+
Transaction is a small library which helps track the status of running/upcoming tasks. These tasks can be a cron job, background jobs or a simple method. Any task can be plugged into a transaction block.
|
11
11
|
|
12
|
-
|
12
|
+
Transaction uses ***Redis*** to store the current status along with the additional attributes(provided during the initialization or transaction update.)
|
13
13
|
|
14
|
+
The events within the transaction block can be published via ***Pubsub client(ex. Pusher, PubNub or any valid pubsub client)***. These events can be subscribed in the client app for the live status of the transaction.
|
15
|
+
|
16
|
+
|
17
|
+
## Requirements
|
18
|
+
|
19
|
+
Redis(>=3.0)
|
20
|
+
|
21
|
+
Valid Pubsub client if messages need to be published.
|
14
22
|
|
15
23
|
## Installation
|
16
24
|
|
@@ -32,20 +40,43 @@ Or install it yourself as:
|
|
32
40
|
|
33
41
|
### Initializing Transaction gem -
|
34
42
|
|
35
|
-
|
43
|
+
In your rails app - create `config/transaction.rb`
|
36
44
|
```ruby
|
45
|
+
|
46
|
+
# Ex. pusher looks like | any pubsub client can be used.
|
47
|
+
require 'pusher'
|
48
|
+
PubsubClient = Pusher::Client.new(
|
49
|
+
app_id:"app_id",
|
50
|
+
key: "app_key",
|
51
|
+
secret: "app_secret",
|
52
|
+
cluster: "cluster_info",
|
53
|
+
use_tls: true
|
54
|
+
)
|
55
|
+
|
37
56
|
Transaction.configure do |config|
|
38
|
-
config.redis = Redis.new
|
57
|
+
config.redis = Redis.new # defaults to localhost
|
58
|
+
config.pubsub_client = {client: PubsubClient, trigger: 'trigger'}
|
39
59
|
end
|
40
60
|
```
|
41
61
|
|
42
|
-
|
62
|
+
There's an option to directly connect as well.
|
43
63
|
```ruby
|
44
64
|
Transaction.redis = Redis.new(url: 'redis://redis_url:6379')
|
65
|
+
Transaction.pubsub_client = {client: PubsubClient, trigger: 'trigger'}
|
45
66
|
```
|
46
67
|
|
68
|
+
The pubsub client accepts the following options -
|
69
|
+
|
70
|
+
param | description
|
71
|
+
|:-:|---
|
72
|
+
client | Any valid pubsub client. Ex. Pusher, PubNub.
|
73
|
+
trigger | The method name which sends a message to a pubsub client. Ex. `'trigger'` for Pusher. `'publish'` for PubNub.
|
74
|
+
channel_name | (Optional) Channel in which the message be published. Defaults to the value of `transaction_id`.
|
75
|
+
event | (Optional) Event for which message will be published. Defaults to `'status'`
|
76
|
+
|
77
|
+
|
47
78
|
### Initializing a transaction
|
48
|
-
A transaction can be newly initialized or be found with the given transaction_id. If no transaction is found then a new transaction
|
79
|
+
A transaction can be newly initialized or be found with the given transaction_id. If no transaction is found then a new transaction is created.
|
49
80
|
```ruby
|
50
81
|
attributes = {created_at: Time.now, count: 0 }
|
51
82
|
transaction = Transaction::Client.new(options: attributes) # creates a new instance of transaction
|
@@ -58,12 +89,13 @@ Accepted statuses: **['queued', 'processing', 'success', 'error']**. Any transac
|
|
58
89
|
|
59
90
|
method | params | description
|
60
91
|
|:-:|---|---
|
61
|
-
start! | - | moves to status `processing` from `queued`.
|
62
|
-
finish! | (status
|
63
|
-
clear! | - | destroys the current entry from
|
64
|
-
refresh! | - | sync current instance transaction with latest cache (Other instance of transaction initiated with same transaction id can update the attributes. Hence `refresh!` is required.).
|
65
|
-
update_status | status | moves to the passed status. Raises `'Invalid Status'` error if status is not in one of the **Accepted statuses** defined above.
|
66
|
-
update_attributes | options = {} | merges the passed options hash to the current attributes object.
|
92
|
+
start! | - | moves to status `processing` from `queued`. Publishes `{message: Processing}` to pubsub client if enabled.
|
93
|
+
finish! | (status: 'success', clear: false, data: {}) | moves to the passed status (default: `success`). Any additional data passed is merged with default `{message: 'Done}'`. `clear = true` destroy the transaction entry from Redis cache.
|
94
|
+
clear! | - | destroys the current entry from Redis cache.
|
95
|
+
refresh! | - | sync current instance transaction with the latest cache (Other instance of a transaction initiated with same transaction id can update the attributes. Hence `refresh!` is required.).
|
96
|
+
update_status | status | moves to the passed status. Raises `'Invalid Status'` error if the status is not in one of the **Accepted statuses** defined above.
|
97
|
+
update_attributes | options = {} | merges the passed options hash to the current attributes object.
|
98
|
+
trigger_event! | data = {} | publishes the data via pubsub. Current `status` along with-param data is published(Note: Pubsub client needs to be configured.)
|
67
99
|
|
68
100
|
|
69
101
|
### Ex 1: Simple transaction
|
@@ -111,7 +143,7 @@ update_attributes | options = {} | merges the passed options hash to the current
|
|
111
143
|
```
|
112
144
|
|
113
145
|
### Keeping transactions in sync.
|
114
|
-
Let's say we have 2 transactions `t1` and `t2` both initialized with same transaction id. If `t2` updates the transaction, then `t1` can
|
146
|
+
Let's say we have 2 transactions `t1` and `t2` both initialized with the same transaction id. If `t2` updates the transaction, then `t1` can simply refresh the transaction to get in sync with `t2`. Note: the transaction will be refreshed with the most recent values. (Versioning transaction updates ??? => Woah that's a nice PR idea.)
|
115
147
|
```ruby
|
116
148
|
def task1
|
117
149
|
transaction = Transaction::Client.new
|
@@ -130,6 +162,26 @@ Let's say we have 2 transactions `t1` and `t2` both initialized with same transa
|
|
130
162
|
end
|
131
163
|
```
|
132
164
|
|
165
|
+
### Pushing messages to client via pubsub
|
166
|
+
If a task is running in the background and the client needs to know the status. PubSub can be configured to do so.
|
167
|
+
```ruby
|
168
|
+
# configure pubsub client as defined above in Initializing Transaction gem
|
169
|
+
|
170
|
+
def long_task
|
171
|
+
transaction = Transaction::Client.new
|
172
|
+
transaction.start!
|
173
|
+
|
174
|
+
items = (0..10_000).to_a
|
175
|
+
|
176
|
+
items.each do |index|
|
177
|
+
transaction.trigger_event!(count: index, total: items.count)
|
178
|
+
# do something
|
179
|
+
end
|
180
|
+
|
181
|
+
transaction.finish!(data: {count: items.count, total: items.count})
|
182
|
+
end
|
183
|
+
```
|
184
|
+
|
133
185
|
## Development
|
134
186
|
|
135
187
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -142,7 +194,7 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/t2013a
|
|
142
194
|
|
143
195
|
## License
|
144
196
|
|
145
|
-
The gem is available as open
|
197
|
+
The gem is available as open-source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
146
198
|
|
147
199
|
## Code of Conduct
|
148
200
|
|
data/lib/transaction.rb
CHANGED
@@ -4,6 +4,7 @@ require 'transaction/version'
|
|
4
4
|
require 'securerandom'
|
5
5
|
require 'redis'
|
6
6
|
require 'json'
|
7
|
+
require 'transaction/helper'
|
7
8
|
|
8
9
|
module Transaction
|
9
10
|
STATUSES = %i[queued processing success error].freeze
|
@@ -29,6 +30,24 @@ module Transaction
|
|
29
30
|
@redis ||= Redis.new
|
30
31
|
end
|
31
32
|
|
33
|
+
def self.pubsub_client=(client:, trigger:, channel_name: nil, event: 'status')
|
34
|
+
@client = client
|
35
|
+
@trigger = trigger
|
36
|
+
@event = event
|
37
|
+
@channel_name = channel_name
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.pubsub_client
|
41
|
+
return if @client.nil? || @trigger.nil?
|
42
|
+
|
43
|
+
{
|
44
|
+
client: @client,
|
45
|
+
trigger: @trigger,
|
46
|
+
event: @event,
|
47
|
+
channel_name: @channel_name
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
32
51
|
class Client
|
33
52
|
attr_reader :transaction_id, :status, :attributes
|
34
53
|
|
@@ -36,6 +55,7 @@ module Transaction
|
|
36
55
|
@transaction_id = transaction_id ||
|
37
56
|
"transact-#{SecureRandom.urlsafe_base64(16)}"
|
38
57
|
@redis_client = Transaction.redis
|
58
|
+
@pubsub_client = Transaction.pubsub_client
|
39
59
|
|
40
60
|
options = DEFAULT_ATTRIBUTES.merge(options)
|
41
61
|
|
@@ -64,11 +84,12 @@ module Transaction
|
|
64
84
|
|
65
85
|
def start!
|
66
86
|
update_status(:processing)
|
87
|
+
trigger_event!(message: 'Processing')
|
67
88
|
end
|
68
89
|
|
69
|
-
def finish!(status
|
90
|
+
def finish!(status: 'success', clear: false, data: {})
|
70
91
|
update_status(status)
|
71
|
-
|
92
|
+
trigger_event!({ message: 'Done' }.merge(data))
|
72
93
|
redis_delete if clear
|
73
94
|
end
|
74
95
|
|
@@ -84,6 +105,18 @@ module Transaction
|
|
84
105
|
@status = @attributes[:status].to_s
|
85
106
|
end
|
86
107
|
|
108
|
+
def trigger_event!(data = {})
|
109
|
+
return if @pubsub_client.nil?
|
110
|
+
|
111
|
+
data[:status] = @status
|
112
|
+
channel_name = @pubsub_client[:channel_name] || @transaction_id
|
113
|
+
client = @pubsub_client[:client]
|
114
|
+
trigger = @pubsub_client[:trigger]
|
115
|
+
event = @pubsub_client[:event]
|
116
|
+
|
117
|
+
client.send(trigger, channel_name, event, data)
|
118
|
+
end
|
119
|
+
|
87
120
|
private
|
88
121
|
|
89
122
|
def parsed_attributes
|
@@ -102,18 +135,6 @@ module Transaction
|
|
102
135
|
response
|
103
136
|
end
|
104
137
|
|
105
|
-
def symbolize_keys!(response = @attributes)
|
106
|
-
response.keys.each do |key|
|
107
|
-
response[(begin
|
108
|
-
key.to_sym
|
109
|
-
rescue StandardError
|
110
|
-
key
|
111
|
-
end) || key] = response.delete(key)
|
112
|
-
end
|
113
|
-
|
114
|
-
response
|
115
|
-
end
|
116
|
-
|
117
138
|
# redis methods
|
118
139
|
def redis_get
|
119
140
|
@redis_client.get(@transaction_id)
|
data/lib/transaction/version.rb
CHANGED
data/transaction.gemspec
CHANGED
@@ -10,8 +10,15 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.authors = ['Anurag Tiwari']
|
11
11
|
spec.email = ['tiwari.anurag126@gmail.com']
|
12
12
|
|
13
|
-
spec.summary = '
|
14
|
-
spec.description = 'Record status along with other relevant information of
|
13
|
+
spec.summary = 'Manage any task | transaction efficiently'
|
14
|
+
spec.description = 'Record status along with other relevant information of
|
15
|
+
transactions or tasks. These tasks can be a cron job, large background jobs or
|
16
|
+
a simple method. Any task can be plugged into a transaction block. Transaction
|
17
|
+
uses Redis to store the current status along with other information.
|
18
|
+
The events within the transaction block can be published via Pubsub client
|
19
|
+
(ex. Pusher, PubNub or any valid pubsub client).These events can be
|
20
|
+
subscribed in the client app for the live status of the transaction.'
|
21
|
+
|
15
22
|
spec.homepage = 'https://github.com/t2013anurag/transaction'
|
16
23
|
spec.license = 'MIT'
|
17
24
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: transaction
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anurag Tiwari
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-08-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -80,7 +80,14 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
-
description:
|
83
|
+
description: |-
|
84
|
+
Record status along with other relevant information of
|
85
|
+
transactions or tasks. These tasks can be a cron job, large background jobs or
|
86
|
+
a simple method. Any task can be plugged into a transaction block. Transaction
|
87
|
+
uses Redis to store the current status along with other information.
|
88
|
+
The events within the transaction block can be published via Pubsub client
|
89
|
+
(ex. Pusher, PubNub or any valid pubsub client).These events can be
|
90
|
+
subscribed in the client app for the live status of the transaction.
|
84
91
|
email:
|
85
92
|
- tiwari.anurag126@gmail.com
|
86
93
|
executables: []
|
@@ -103,6 +110,7 @@ files:
|
|
103
110
|
- bin/console
|
104
111
|
- bin/setup
|
105
112
|
- lib/transaction.rb
|
113
|
+
- lib/transaction/helper.rb
|
106
114
|
- lib/transaction/version.rb
|
107
115
|
- transaction.gemspec
|
108
116
|
homepage: https://github.com/t2013anurag/transaction
|
@@ -128,5 +136,5 @@ rubyforge_project:
|
|
128
136
|
rubygems_version: 2.7.6
|
129
137
|
signing_key:
|
130
138
|
specification_version: 4
|
131
|
-
summary:
|
139
|
+
summary: Manage any task | transaction efficiently
|
132
140
|
test_files: []
|