sidekiq-superworker 0.1.6 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -68,6 +68,28 @@ Install and run the migration:
|
|
68
68
|
Usage
|
69
69
|
-----
|
70
70
|
|
71
|
+
First, define a superworker in a file that's included during the initialization of the app. If you're using Rails, you might do this in an initializer:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
# config/initializers/superworkers.rb
|
75
|
+
|
76
|
+
Superworker.create(:MySuperworker, :user_id, :comment_id) do
|
77
|
+
Worker1 :user_id, :comment_id
|
78
|
+
Worker2 :comment_id
|
79
|
+
end
|
80
|
+
|
81
|
+
Superworker.create(:MyOtherSuperworker, :comment_id) do
|
82
|
+
Worker2 :comment_id
|
83
|
+
Worker3 :comment_id
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
To run a superworker, call perform_async:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
MySuperworker.perform_async(23, 852)
|
91
|
+
```
|
92
|
+
|
71
93
|
### Arguments
|
72
94
|
|
73
95
|
You can define any number of arguments for the superworker and pass them to different subworkers as you see fit:
|
data/lib/sidekiq/superworker.rb
CHANGED
@@ -53,7 +53,9 @@ module Sidekiq
|
|
53
53
|
parent_id: parent_id
|
54
54
|
}
|
55
55
|
if value[:subworker_class] == :batch
|
56
|
-
@
|
56
|
+
@arg_keys_to_arg_keys ||= Hash[@args.keys.map { |key| [key, key] }]
|
57
|
+
batch_keys_to_iteration_keys = @arg_keys_to_arg_keys.merge(arg_values[0])
|
58
|
+
@records[id][:children_ids] = children_ids_for_batch(value[:children], batch_keys_to_iteration_keys)
|
57
59
|
end
|
58
60
|
|
59
61
|
@records[last_id][:next_id] = id if @records[last_id]
|
@@ -120,11 +122,8 @@ module Sidekiq
|
|
120
122
|
def get_batch_iteration_arg_value_arrays(batch_keys_to_iteration_keys)
|
121
123
|
batch_keys = batch_keys_to_iteration_keys.keys
|
122
124
|
batch_keys_to_batch_values = @args.slice(*(batch_keys))
|
123
|
-
|
124
125
|
batch_values = batch_keys_to_batch_values.values
|
125
|
-
|
126
|
-
batch_values = first_batch_value.zip(*batch_values)
|
127
|
-
batch_values
|
126
|
+
batch_values_to_batch_arrays(batch_values)
|
128
127
|
end
|
129
128
|
|
130
129
|
def rewrite_ids_of_nested_hash(nested_hash)
|
@@ -139,6 +138,20 @@ module Sidekiq
|
|
139
138
|
end
|
140
139
|
new_hash
|
141
140
|
end
|
141
|
+
|
142
|
+
def batch_values_to_batch_arrays(values)
|
143
|
+
arrays = values.map { |value| value.is_a?(Array) ? value : [value] }
|
144
|
+
max_length = arrays.map(&:length).max
|
145
|
+
arrays.map! do |array|
|
146
|
+
if array.length < max_length
|
147
|
+
repetition_count = (max_length / array.length).ceil
|
148
|
+
array = (array * repetition_count).take(max_length)
|
149
|
+
end
|
150
|
+
array
|
151
|
+
end
|
152
|
+
first_array = arrays.shift
|
153
|
+
first_array.zip(*arrays)
|
154
|
+
end
|
142
155
|
end
|
143
156
|
end
|
144
157
|
end
|
@@ -7,14 +7,16 @@ module Sidekiq
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def call(worker, item, queue)
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
ActiveRecord::Base.connection_pool.with_connection do
|
11
|
+
begin
|
12
|
+
return_value = yield
|
13
|
+
rescue Exception => exception
|
14
|
+
@processor.error(worker, item, queue, exception)
|
15
|
+
raise exception
|
16
|
+
end
|
17
|
+
@processor.complete(item)
|
18
|
+
return_value
|
15
19
|
end
|
16
|
-
@processor.complete(item)
|
17
|
-
return_value
|
18
20
|
end
|
19
21
|
end
|
20
22
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sidekiq-superworker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-05-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sidekiq
|